-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheye_detect.py
32 lines (28 loc) · 913 Bytes
/
eye_detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3.7
import cv2
import numpy
import dlib
from imutils import face_utils
import numpy as np
"""
This file implements eye detection in a photo. It will detect
one pair of eyes in a frame and return the points.
"""
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"]
def detectEyes(image):
shape = None
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
if shape is not None:
left_eye = shape[lStart:lEnd]
right_eye = shape[rStart:rEnd]
else:
left_eye = None
right_eye = None
return left_eye, right_eye