Skip to content

Instantly share code, notes, and snippets.

@robgon-art
Last active January 8, 2022 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robgon-art/ba854bc3c986297e57b71bfab541cf86 to your computer and use it in GitHub Desktop.
Save robgon-art/ba854bc3c986297e57b71bfab541cf86 to your computer and use it in GitHub Desktop.
# This is a modified version of the find_faces code in DLIB, by Davis King
# The original version is here:
# https://github.com/davisking/dlib/blob/master/python_examples/face_recognition.py
import dlib
from PIL import Image
def find_faces(face_file_path):
# Load all the models we need: a detector to find the faces, a shape predictor
# to find face landmarks so we can precisely localize the face
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor("/content/shape_predictor_5_face_landmarks.dat")
# Load the image using Dlib
img = dlib.load_rgb_image(face_file_path)
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
images = []
num_faces = len(dets)
if num_faces > 0:
# Find the 5 face landmarks we need to do the alignment.
faces = dlib.full_object_detections()
# print("len(faces)", len(faces))
for detection in dets:
area = detection.area()
if detection.area() > min_area:
faces.append(sp(img, detection))
# Get the aligned face images
if len(faces) > 0:
images = dlib.get_face_chips(img, faces, size=512, padding=1.25)
return images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment