Last active
October 11, 2017 10:23
-
-
Save trygvea/31125217cd7220359e8f0ea392712bef to your computer and use it in GitHub Desktop.
Face detection using dlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cut and paste from: | |
# http://dlib.net/face_recognition.py.html | |
# https://github.com/ageitgey/face_recognition/blob/master/face_recognition/api.py | |
# https://medium.com/towards-data-science/facial-recognition-using-deep-learning-a74e9059a150 | |
# | |
# Install dlib: See https://www.pyimagesearch.com/2017/03/27/how-to-install-dlib/ | |
# Download dlib models: http://dlib.net/files/ | |
import os | |
import dlib | |
import numpy as np | |
from skimage import io | |
import cv2 | |
data_dir = os.path.expanduser('~/data') | |
faces_folder_path = data_dir + '/kodemaker/' | |
# Globals | |
face_detector = dlib.get_frontal_face_detector() | |
shape_predictor = dlib.shape_predictor(data_dir + '/dlib/shape_predictor_68_face_landmarks.dat') | |
face_recognition_model = dlib.face_recognition_model_v1(data_dir + '/dlib/dlib_face_recognition_resnet_model_v1.dat') | |
def get_face_encodings(face): | |
bounds = face_detector(face, 1) | |
faces_landmarks = [shape_predictor(face, face_bounds) for face_bounds in bounds] | |
return [np.array(face_recognition_model.compute_face_descriptor(face, face_pose, 1)) for face_pose in faces_landmarks] | |
def compare_face_encodings(known_faces, face): | |
return np.linalg.norm(known_faces - face, axis=1) <= 0.6 | |
def find_match(known_faces, person_names, face): | |
matches = compare_face_encodings(known_faces, face) # get a list of True/False | |
count = 0 | |
for match in matches: | |
if match: | |
return person_names[count] | |
count += 1 | |
return 'Not Found' | |
def load_face_encodings(faces_folder_path): | |
image_filenames = filter(lambda x: x.endswith('.jpg'), os.listdir(faces_folder_path)) | |
image_filenames = sorted(image_filenames) | |
person_names = [x[:-4] for x in image_filenames] | |
full_paths_to_images = [faces_folder_path + x for x in image_filenames] | |
face_encodings = [] | |
win = dlib.image_window() | |
for path_to_image in full_paths_to_images: | |
face = io.imread(path_to_image) | |
faces_bounds = face_detector(face, 1) | |
if len(faces_bounds) != 1: | |
print("Expected one and only one face per image: " + path_to_image + " - it has " + str(len(faces_bounds))) | |
exit() | |
face_bounds = faces_bounds[0] | |
face_landmarks = shape_predictor(face, face_bounds) | |
face_encoding = np.array(face_recognition_model.compute_face_descriptor(face, face_landmarks, 1)) | |
win.clear_overlay() | |
win.set_image(face) | |
win.add_overlay(face_bounds) | |
win.add_overlay(face_landmarks) | |
face_encodings.append(face_encoding) | |
print(face_encoding) | |
dlib.hit_enter_to_continue() | |
return face_encodings, person_names | |
def recognize_faces_in_video(face_encodings, person_names): | |
faceClassifier = cv2.CascadeClassifier(data_dir + '/opencv/haarcascade_frontalface_default.xml') | |
cap = cv2.VideoCapture(0) | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
break | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
face_rects = faceClassifier.detectMultiScale( | |
gray, | |
scaleFactor = 1.1, | |
minNeighbors = 5, | |
minSize = (50, 50), | |
flags = cv2.CASCADE_SCALE_IMAGE) | |
for (x, y, w, h) in face_rects: | |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) | |
face = frame[y:y + h, x:x + w] | |
face_encodings_in_image = get_face_encodings(face) | |
if (face_encodings_in_image): | |
match = find_match(face_encodings, person_names, face_encodings_in_image[0]) | |
cv2.putText(frame, match, (x+5, y-15), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) | |
cv2.imshow("bilde", frame) | |
if cv2.waitKey(10) == 27: | |
break | |
cap.release() | |
cv2.destroyAllWindows() | |
face_encodings, person_names = load_face_encodings(faces_folder_path) | |
recognize_faces_in_video(face_encodings, person_names) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment