Last active
March 7, 2017 04:00
-
-
Save allskyee/24524df650f1b700fc64775979dbe246 to your computer and use it in GitHub Desktop.
Face landmark 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
#!/usr/bin/env python | |
import sys | |
import dlib | |
import cv2 | |
import numpy as np | |
from opencv_webcam_multithread import WebcamVideoStream | |
# dlib predictor can be found in | |
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 | |
if __name__ == "__main__" : | |
detector = dlib.get_frontal_face_detector() | |
assert detector is not None | |
face_predictor_path = "shape_predictor_68_face_landmarks.dat" | |
predictor = dlib.shape_predictor(face_predictor_path) | |
INNER_EYES_AND_BOTTOM_LIP = [39, 42, 57] | |
OUTER_EYES_AND_NOSE = [36, 45, 33] | |
all_landmark_indices = np.array(INNER_EYES_AND_BOTTOM_LIP + OUTER_EYES_AND_NOSE) | |
assert predictor is not None | |
vs = WebcamVideoStream().start() | |
assert vs is not None | |
while True : | |
frame = vs.read() | |
# 0 in second argument so that we don't upsample face | |
faces = detector(frame, 0) | |
# draw face rectangle | |
for f in faces : | |
cv2.rectangle(frame, (f.left(), f.top()), (f.right(), f.bottom()), (255, 255, 0), 2) | |
l = predictor(frame, f).parts() | |
pp = list(map(lambda i : (l[i].x, l[i].y), all_landmark_indices)) | |
# draw landmarks | |
for p in pp : | |
cv2.circle(frame, p, 2, (0, 0, 255)) | |
cv2.imshow('webcam', frame) | |
if cv2.waitKey(1) == 27 : | |
break | |
vs.stop() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment