-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_eyes_from_cam.py
88 lines (81 loc) · 3.74 KB
/
detect_eyes_from_cam.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
from cv2 import dnn
from faceDetector.caffe import ultra_face_opencvdnn_inference as ultra_face
import numpy as np
import time
import dlib
from imutils import face_utils
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from src.face_detector import FaceDetector
from src.face_landmark_predictor import FaceLandmarkPredictor
import time
def getBoudingboxFromLandmarksList(landmaks_list):
bouding_box = [landmaks_list[0],landmaks_list[-1]]
for landmark in landmaks_list:
if bouding_box[0][0] < bouding_box[0][0]:
bouding_box[0][0] = landmark[0]
if landmark[1] < bouding_box[0][1]:
bouding_box[0][1] = landmark[1]
if landmark[0] > bouding_box[1][0]:
bouding_box[1][0] = landmark[0]
if landmark[1] > bouding_box[1][1]:
bouding_box[1][1] = landmark[1]
return bouding_box
if __name__ == "__main__":
cap = cv2.VideoCapture(0)
face_detector = FaceDetector()
face_landmark_predictor = FaceLandmarkPredictor()
range_idxs = []
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"])
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"])
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["right_eyebrow"])
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["left_eyebrow"])
model = load_model("./models/eyes_closed_open_model_64_64.h5")
while cap.isOpened:
ret, img = cap.read()
img = cv2.resize(img,(240,320))
show_img = img.copy()
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
for box in face_detector.detectFaces(img):
landmark_list = face_landmark_predictor.predictLandmarks(img_gray,box)
isInRange = lambda idx, idx_range : True if idx>=idx_range[0] and idx<=idx_range[1] else False
good_landmarks_list = []
cv2.rectangle(show_img,(box[0],box[1]),(box[2],box[3]),(255,0,0))
for i in range(48):
for range_idx in range_idxs:
if isInRange(i, range_idx) or i == 30:
good_landmarks_list.append(list(landmark_list[i]))
#cv2.circle(show_img,landmark_list[i],1,(255,0,0))
bb = getBoudingboxFromLandmarksList(good_landmarks_list)
pt1 = (bb[0][0],bb[0][1])
pt2 = (bb[1][0],bb[1][1])
eye_img = None
try:
eye_img = img[pt1[1]:pt2[1],pt1[0]:pt2[0]]
eye_img = cv2.resize(eye_img,(64,64),interpolation=cv2.INTER_AREA)
except:
continue
eye_img = eye_img/255
eye_img = img_to_array(eye_img)
eye_img = np.expand_dims(eye_img, axis=0)
(closed, opened) = model.predict(eye_img)[0]
#print(f'closed score: {closed} , opened score: {opened}')
label = f'Closed: {round(closed*100,2)}%'
score = closed
color = (0,0,255)
if opened > closed:
label = f'Opened: {round(opened*100,2)}%'
score = opened
color = (0,255,0)
if score>0.9:
cv2.putText(show_img,label,(int(box[0]),box[3]+10),cv2.FONT_HERSHEY_PLAIN,1,color,2)
print(label)
cv2.rectangle(show_img,(pt1),(pt2),color)
cv2.imshow("img",show_img)
k = cv2.waitKey(1)
if k == 27:
break
cv2.destroyAllWindows()