Created
February 11, 2017 08:55
-
-
Save yu4u/18de62c37138ff60f6fcb4cfd5d03ad5 to your computer and use it in GitHub Desktop.
Real-time face recognition and visualization via dlib and matplotlib
This file contains hidden or 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
| import cv2 | |
| import dlib | |
| import matplotlib.pyplot as plt | |
| import matplotlib.patches as patches | |
| import os | |
| predictor_path = "shape_predictor_68_face_landmarks.dat" | |
| # download trained model | |
| if not os.path.isfile(predictor_path): | |
| os.system("wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2") | |
| os.system("bunzip2 shape_predictor_68_face_landmarks.dat.bz2") | |
| cap = cv2.VideoCapture(0) | |
| cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) | |
| cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) | |
| detector = dlib.get_frontal_face_detector() | |
| predictor = dlib.shape_predictor(predictor_path) | |
| fig, ax = plt.subplots() | |
| while True: | |
| ret, frame = cap.read() | |
| frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| dets = detector(frame, 1) | |
| # for each detected face | |
| for d in dets: | |
| # draw bounding box of the detected face | |
| rect = patches.Rectangle((d.left(), d.top()), d.width(), d.height(), fill=False) | |
| ax.add_patch(rect) | |
| # draw landmarks | |
| parts = predictor(frame, d).parts() | |
| ax.scatter([point.x for point in parts], [point.y for point in parts]) | |
| for k, point in enumerate(parts): | |
| ax.text(point.x, point.y, k) | |
| ax.imshow(frame) | |
| plt.pause(0.1) | |
| plt.cla() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is face detection not recognition!
Sent from my Sony D5803 using FastHub