-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_eyes_realtime.py
61 lines (51 loc) · 1.76 KB
/
detect_eyes_realtime.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3.7
import cv2
import imutils
from imutils import face_utils
from imutils.video import WebcamVideoStream
import dlib
from scipy.spatial import distance
import faceops
import imgops
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# To capture video from webcam.
cap = WebcamVideoStream(src=0).start()
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]
while True:
# Read the frame
img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
face_list = []
for (i, rect) in enumerate(rects):
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
face_list.append(shape)
if len(face_list) > 0:
left_eye = face_list[0][lStart:lEnd]
right_eye = face_list[0][rStart:rEnd]
left_ear = faceops.getEAR(left_eye)
right_ear = faceops.getEAR(right_eye)
MAR = imgops.getMARs(face_list)[0]
for (x, y) in face_list[0][mStart:mEnd]:
img[y, x] = [255,255,255]
else:
left_eye = []
right_eye = []
left_ear = 0
right_ear = 0
cv2.putText(img, "EAR: " + str((left_ear+right_ear)/2), (0,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
cv2.putText(img, "EAM: " + str(MAR), (0, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
if cv2.waitKey(1) & 0xff == 27:
break
# Release the VideoCapture object
cap.stop()
cv2.destroyAllWindows()