Last active
July 12, 2018 12:43
-
-
Save livenson/8f921cf0390f4b04fc2463d87bb313fe to your computer and use it in GitHub Desktop.
opencv poc
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 | |
from time import sleep | |
import dlib | |
from scipy.spatial import distance as dist | |
import numpy as np | |
def eye_aspect_ratio(eye): | |
# compute the euclidean distances between the two sets of | |
# vertical eye landmarks (x, y)-coordinates | |
A = dist.euclidean(eye[1], eye[5]) | |
B = dist.euclidean(eye[2], eye[4]) | |
# compute the euclidean distance between the horizontal | |
# eye landmark (x, y)-coordinates | |
C = dist.euclidean(eye[0], eye[3]) | |
# compute the eye aspect ratio | |
ear = (A + B) / (2.0 * C) | |
# return the eye aspect ratio | |
return ear | |
def detect_face(): | |
face_cascade_path = 'haarcascade_frontalface_default.xml' | |
eye_cascade_path = 'haarcascade_eye_tree_eyeglasses.xml' | |
smile_cascade_path = 'haarcascade_smile.xml' | |
predictor_path = 'shape_predictor_68_face_landmarks.dat' | |
face_cascade = cv2.CascadeClassifier(face_cascade_path) | |
faces_dlib = dlib.get_frontal_face_detector() | |
eye_cascade = cv2.CascadeClassifier(eye_cascade_path) | |
smile_cascade = cv2.CascadeClassifier(smile_cascade_path) | |
predictor = dlib.shape_predictor(predictor_path) | |
video_capture = cv2.VideoCapture(0) | |
# dlib-based feature extraction | |
JAWLINE_POINTS = list(range(0, 17)) | |
RIGHT_EYEBROW_POINTS = list(range(17, 22)) | |
LEFT_EYEBROW_POINTS = list(range(22, 27)) | |
NOSE_POINTS = list(range(27, 36)) | |
RIGHT_EYE_POINTS = list(range(36, 42)) | |
LEFT_EYE_POINTS = list(range(42, 48)) | |
MOUTH_OUTLINE_POINTS = list(range(48, 61)) | |
MOUTH_INNER_POINTS = list(range(61, 68)) | |
while True: | |
if not video_capture.isOpened(): | |
print('Unable to load camera.') | |
sleep(5) | |
pass | |
# Capture frame-by-frame | |
ret, frame = video_capture.read() | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
faces = face_cascade.detectMultiScale( | |
gray, | |
scaleFactor=1.3, | |
minNeighbors=5, | |
minSize=(30, 30) | |
) | |
detected_faces_dlib = faces_dlib(gray, 1) | |
print("Number of faces detected with dlib: {}, opencv: {}".format(len(detected_faces_dlib), len(faces))) | |
# Draw a rectangle aroudn the faces (dlib) | |
for d in detected_faces_dlib: | |
x = d.left() | |
y = d.bottom() | |
w = d.right() - d.left() | |
h = d.top() - d.bottom() | |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), 2) | |
# Draw a rectangle around the faces (opencv) | |
for (x, y, w, h) in faces: | |
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2) | |
# dlib processing | |
# dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h)) | |
# detected_landmarks = predictor(frame, dlib_rect).parts() | |
# | |
# landmarks = np.matrix([[p.x, p.y] for p in detected_landmarks]) | |
# | |
# landmarks_display = landmarks[RIGHT_EYE_POINTS + LEFT_EYE_POINTS] | |
# | |
# # check if eyes are open or not - based on http://www.pyimagesearch.com/2017/04/24/eye-blink-detection-opencv-python-dlib/ | |
# leftEAR = eye_aspect_ratio(landmarks[LEFT_EYE_POINTS]) | |
# rightEAR = eye_aspect_ratio(landmarks[RIGHT_EYE_POINTS]) | |
# | |
# # average the eye aspect ratio together for both eyes | |
# ear = (leftEAR + rightEAR) / 2.0 | |
# | |
# if ear < 0.2: # ratio of open/closed eyes | |
# print("Eyes are closed, EAR: " + str(ear)) | |
# | |
# for idx, point in enumerate(landmarks_display): | |
# pos = (point[0, 0], point[0, 1]) | |
# # annotate the positions | |
# cv2.putText(frame, str(idx), pos, | |
# fontFace=cv2.FONT_HERSHEY_SIMPLEX, | |
# fontScale=0.4, | |
# color=(0, 0, 255)) | |
# cv2.circle(frame, pos, 2, color=(0, 255, 255), thickness=-1) | |
# ####### | |
# highlight features | |
# roi_gray = gray[y:y + h, x:x + w] | |
# roi_color = frame[y:y + h, x:x + w] | |
# highlight eyes | |
# eyes = eye_cascade.detectMultiScale(roi_gray) | |
# for (ex, ey, ew, eh) in eyes: | |
# cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) | |
# | |
# # highlight smile | |
# roi_gray = gray[y + (h // 3 * 2):y + h, x:x + w] | |
# roi_color = frame[y + (h // 3 * 2):y + h, x:x + w] | |
# | |
# smile = smile_cascade.detectMultiScale( | |
# roi_gray, | |
# scaleFactor=1.5, | |
# minNeighbors=10, | |
# minSize=(15, 15), | |
# ) | |
# for (sx, sy, sw, sh) in smile: | |
# cv2.rectangle(roi_color, (sx, sy), (sx + sw, sy + sh), (255, 0, 0), 2) | |
# blur faces | |
# sub_face = frame[y:y + h, x:x + w] | |
# cv2.GaussianBlur(sub_face, (33, 33), 30, sub_face) | |
# frame[y:y + sub_face.shape[0], x:x + sub_face.shape[1]] = sub_face | |
# Display the resulting frame | |
cv2.imshow('Video', frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# When everything is done, release the capture | |
video_capture.release() | |
cv2.destroyAllWindows() | |
if __name__ == "__main__": | |
detect_face() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Mister.
Let us explain about this code:
# compute the euclidean distances between the two sets of
# vertical eye landmarks (x, y)-coordinates
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
and where did they come from?
thank you