-
Notifications
You must be signed in to change notification settings - Fork 8
/
inference.py
280 lines (245 loc) · 10.7 KB
/
inference.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import cv2
import mediapipe as mp
import math
import numpy as np
import os
import time
import torch
def distance(p1, p2):
''' Calculate distance between two points
:param p1: First Point
:param p2: Second Point
:return: Euclidean distance between the points. (Using only the x and y coordinates).
'''
return (((p1[:2] - p2[:2])**2).sum())**0.5
def eye_aspect_ratio(landmarks, eye):
''' Calculate the ratio of the eye length to eye width.
:param landmarks: Face Landmarks returned from FaceMesh MediaPipe model
:param eye: List containing positions which correspond to the eye
:return: Eye aspect ratio value
'''
N1 = distance(landmarks[eye[1][0]], landmarks[eye[1][1]])
N2 = distance(landmarks[eye[2][0]], landmarks[eye[2][1]])
N3 = distance(landmarks[eye[3][0]], landmarks[eye[3][1]])
D = distance(landmarks[eye[0][0]], landmarks[eye[0][1]])
return (N1 + N2 + N3) / (3 * D)
def eye_feature(landmarks):
''' Calculate the eye feature as the average of the eye aspect ratio for the two eyes
:param landmarks: Face Landmarks returned from FaceMesh MediaPipe model
:return: Eye feature value
'''
return (eye_aspect_ratio(landmarks, left_eye) + \
eye_aspect_ratio(landmarks, right_eye))/2
def mouth_feature(landmarks):
''' Calculate mouth feature as the ratio of the mouth length to mouth width
:param landmarks: Face Landmarks returned from FaceMesh MediaPipe model
:return: Mouth feature value
'''
N1 = distance(landmarks[mouth[1][0]], landmarks[mouth[1][1]])
N2 = distance(landmarks[mouth[2][0]], landmarks[mouth[2][1]])
N3 = distance(landmarks[mouth[3][0]], landmarks[mouth[3][1]])
D = distance(landmarks[mouth[0][0]], landmarks[mouth[0][1]])
return (N1 + N2 + N3)/(3*D)
def pupil_circularity(landmarks, eye):
''' Calculate pupil circularity feature.
:param landmarks: Face Landmarks returned from FaceMesh MediaPipe model
:param eye: List containing positions which correspond to the eye
:return: Pupil circularity for the eye coordinates
'''
perimeter = distance(landmarks[eye[0][0]], landmarks[eye[1][0]]) + \
distance(landmarks[eye[1][0]], landmarks[eye[2][0]]) + \
distance(landmarks[eye[2][0]], landmarks[eye[3][0]]) + \
distance(landmarks[eye[3][0]], landmarks[eye[0][1]]) + \
distance(landmarks[eye[0][1]], landmarks[eye[3][1]]) + \
distance(landmarks[eye[3][1]], landmarks[eye[2][1]]) + \
distance(landmarks[eye[2][1]], landmarks[eye[1][1]]) + \
distance(landmarks[eye[1][1]], landmarks[eye[0][0]])
area = math.pi * ((distance(landmarks[eye[1][0]], landmarks[eye[3][1]]) * 0.5) ** 2)
return (4*math.pi*area)/(perimeter**2)
def pupil_feature(landmarks):
''' Calculate the pupil feature as the average of the pupil circularity for the two eyes
:param landmarks: Face Landmarks returned from FaceMesh MediaPipe model
:return: Pupil feature value
'''
return (pupil_circularity(landmarks, left_eye) + \
pupil_circularity(landmarks, right_eye))/2
def run_face_mp(image):
''' Get face landmarks using the FaceMesh MediaPipe model.
Calculate facial features using the landmarks.
:param image: Image for which to get the face landmarks
:return: Feature 1 (Eye), Feature 2 (Mouth), Feature 3 (Pupil), \
Feature 4 (Combined eye and mouth feature), image with mesh drawings
'''
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = face_mesh.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_face_landmarks:
landmarks_positions = []
# assume that only face is present in the image
for _, data_point in enumerate(results.multi_face_landmarks[0].landmark):
landmarks_positions.append([data_point.x, data_point.y, data_point.z]) # saving normalized landmark positions
landmarks_positions = np.array(landmarks_positions)
landmarks_positions[:, 0] *= image.shape[1]
landmarks_positions[:, 1] *= image.shape[0]
# draw face mesh over image
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACE_CONNECTIONS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
ear = eye_feature(landmarks_positions)
mar = mouth_feature(landmarks_positions)
puc = pupil_feature(landmarks_positions)
moe = mar/ear
else:
ear = -1000
mar = -1000
puc = -1000
moe = -1000
return ear, mar, puc, moe, image
def calibrate(calib_frame_count=25):
''' Perform clibration. Get features for the neutral position.
:param calib_frame_count: Image frames for which calibration is performed. Default Vale of 25.
:return: Normalization Values for feature 1, Normalization Values for feature 2, \
Normalization Values for feature 3, Normalization Values for feature 4
'''
ears = []
mars = []
pucs = []
moes = []
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
ear, mar,puc, moe, image = run_face_mp(image)
if ear != -1000:
ears.append(ear)
mars.append(mar)
pucs.append(puc)
moes.append(moe)
cv2.putText(image, "Calibration", (int(0.02*image.shape[1]), int(0.14*image.shape[0])),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 2)
cv2.imshow('MediaPipe FaceMesh', image)
if cv2.waitKey(5) & 0xFF == ord("q"):
break
if len(ears) >= calib_frame_count:
break
cv2.destroyAllWindows()
cap.release()
ears = np.array(ears)
mars = np.array(mars)
pucs = np.array(pucs)
moes = np.array(moes)
return [ears.mean(), ears.std()], [mars.mean(), mars.std()], \
[pucs.mean(), pucs.std()], [moes.mean(), moes.std()]
def get_classification(input_data):
''' Perform classification over the facial features.
:param input_data: List of facial features for 20 frames
:return: Alert / Drowsy state prediction
'''
model_input = []
model_input.append(input_data[:5])
model_input.append(input_data[3:8])
model_input.append(input_data[6:11])
model_input.append(input_data[9:14])
model_input.append(input_data[12:17])
model_input.append(input_data[15:])
model_input = torch.FloatTensor(np.array(model_input))
preds = torch.sigmoid(model(model_input)).gt(0.5).int().data.numpy()
return int(preds.sum() >= 5)
def infer(ears_norm, mars_norm, pucs_norm, moes_norm):
''' Perform inference.
:param ears_norm: Normalization values for eye feature
:param mars_norm: Normalization values for mouth feature
:param pucs_norm: Normalization values for pupil feature
:param moes_norm: Normalization values for mouth over eye feature.
'''
ear_main = 0
mar_main = 0
puc_main = 0
moe_main = 0
decay = 0.9 # use decay to smoothen the noise in feature values
label = None
input_data = []
frame_before_run = 0
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
ear, mar, puc, moe, image = run_face_mp(image)
if ear != -1000:
ear = (ear - ears_norm[0])/ears_norm[1]
mar = (mar - mars_norm[0])/mars_norm[1]
puc = (puc - pucs_norm[0])/pucs_norm[1]
moe = (moe - moes_norm[0])/moes_norm[1]
if ear_main == -1000:
ear_main = ear
mar_main = mar
puc_main = puc
moe_main = moe
else:
ear_main = ear_main*decay + (1-decay)*ear
mar_main = mar_main*decay + (1-decay)*mar
puc_main = puc_main*decay + (1-decay)*puc
moe_main = moe_main*decay + (1-decay)*moe
else:
ear_main = -1000
mar_main = -1000
puc_main = -1000
moe_main = -1000
if len(input_data) == 20:
input_data.pop(0)
input_data.append([ear_main, mar_main, puc_main, moe_main])
frame_before_run += 1
if frame_before_run >= 15 and len(input_data) == 20:
frame_before_run = 0
label = get_classification(input_data)
print ('got label ', label)
cv2.putText(image, "EAR: %.2f" %(ear_main), (int(0.02*image.shape[1]), int(0.07*image.shape[0])),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
cv2.putText(image, "MAR: %.2f" %(mar_main), (int(0.27*image.shape[1]), int(0.07*image.shape[0])),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
cv2.putText(image, "PUC: %.2f" %(puc_main), (int(0.52*image.shape[1]), int(0.07*image.shape[0])),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
cv2.putText(image, "MOE: %.2f" %(moe_main), (int(0.77*image.shape[1]), int(0.07*image.shape[0])),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
if label is not None:
if label == 0:
color = (0, 255, 0)
else:
color = (0, 0, 255)
cv2.putText(image, "%s" %(states[label]), (int(0.02*image.shape[1]), int(0.2*image.shape[0])),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 2)
cv2.imshow('MediaPipe FaceMesh', image)
if cv2.waitKey(5) & 0xFF == ord("q"):
break
cv2.destroyAllWindows()
cap.release()
right_eye = [[33, 133], [160, 144], [159, 145], [158, 153]] # right eye landmark positions
left_eye = [[263, 362], [387, 373], [386, 374], [385, 380]] # left eye landmark positions
mouth = [[61, 291], [39, 181], [0, 17], [269, 405]] # mouth landmark coordinates
states = ['alert', 'drowsy']
# Declaring FaceMesh model
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
min_detection_confidence=0.3, min_tracking_confidence=0.8)
mp_drawing = mp.solutions.drawing_utils
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
model_lstm_path = 'models\clf_lstm_jit6.pth'
model = torch.jit.load(model_lstm_path)
model.eval()
print ('Starting calibration. Please be in neutral state')
time.sleep(1)
ears_norm, mars_norm, pucs_norm, moes_norm = calibrate()
print ('Starting main application')
time.sleep(1)
infer(ears_norm, mars_norm, pucs_norm, moes_norm)
face_mesh.close()