-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdm_ai.py
More file actions
457 lines (387 loc) · 20.1 KB
/
Copy pathdm_ai.py
File metadata and controls
457 lines (387 loc) · 20.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#**************************************************************************************
#
# T4AV - Driver Monitoring Systems using AI (Assignment)
#
# File: dm-ai.py
# Author: Amir Sasanfar
# Company: Politecnico di Torino
#
#
#**************************************************************************************
import cv2
import mediapipe as mp
import numpy as np
import time
import statistics as st
import os
import math
DROWSY_EAR=0.8
# EAR baseline(the normal openness)
baseline_ear = 0.3
# Drowsiness detection (timing the drowsiness)
drowsy_start_time = None
drowsiness_timer = 0
#for PERCLOS
ear_history = []
interval_duration_perclos = 20 # measured in seconds
threshold_perclos = 0.25 # under this value the eyes are considered closed
update_time_perclos = time.time()
##############distraction
distracted_timeout = 0
distracted_msg_timeout = 0
init = 0
#for head angles
NORMAL_ANGLES = [0,0,0] # in order pith , yaw, roll
distracted = False # setting a flag for determining distraction
################functions##################
def ear_cal(coord_list: list): # in order p1_RE , p2_RE --> p6_RE the same for LE
numerator_ear = abs(coord_list[1][1] - coord_list[5][1]) + abs(coord_list[2][1] - coord_list[4][1])
denominator_ear = 2*abs(coord_list[0][0] - coord_list[3][0])
return numerator_ear/denominator_ear
# function for computing PERCLOS
def perclos_cal(ear_history, threshold):
if not ear_history:
return 0
closed_eyes_count = sum(1 for ear, t in ear_history if ear < threshold)
return closed_eyes_count / len(ear_history)
#distraction criteria
################END functions##################
# 1 - Imports
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
# 2 - Set the desired setting
mp_drawing_styles = mp.solutions.drawing_styles
mp_drawing = mp.solutions.drawing_utils
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
# 3 - Open the video source
cap = cv2.VideoCapture(0) # Local webcam (index start from 0)
start = time.time() # timing start before the loop
# 4 - Iterate (within an infinite loop)
while cap.isOpened():
# 4.1 - Get the new frame
success, image = cap.read()
# Also convert the color space from BGR to RGB
if image is None:
break
#continue
#else: #needed with some cameras/video input format
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# To improve performace
image.flags.writeable = False
# 4.2 - Run MediaPipe on the frame
results = face_mesh.process(image)
# To improve performance
image.flags.writeable = True
# Convert the color space from RGB to BGR
#image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
img_h, img_w, img_c = image.shape
#these are tuples
point_RER = [] # Right Eye Right # p1
point_REB = [] # Right Eye Bottom
point_REL = [] # Right Eye Left # p4
point_RET = [] # Right Eye Top
p2_RE = [] #p2
p3_RE = [] #p3
p5_RE=[]
p6_RE = [] #p6
point_LER = [] # Left Eye Right # p1
point_LEB = [] # Left Eye Bottom
point_LEL = [] # Left Eye Left #p4
point_LET = [] # Left Eye Top
p2_LE =[]
p3_LE =[]
p5_LE= []
p6_LE = []
point_REIC = [] # Right Eye Iris Center
point_LEIC = [] # Left Eye Iris Center
# added for gaze and distraction tracking
face_2d = []
face_3d = []
left_eye_2d = []
left_eye_3d = []
right_eye_2d = []
right_eye_3d = []
#
# 4.3 - Get the landmark coordinates
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
for idx, lm in enumerate(face_landmarks.landmark):
# Eye Gaze (Iris Tracking)
# Left eye indices list
LEFT_EYE =[ 362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385,384, 398 ]
# Right eye indices list
RIGHT_EYE=[ 33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161 , 246 ]
LEFT_IRIS = [473, 474, 475, 476, 477]
RIGHT_IRIS = [468, 469, 470, 471, 472]
if idx == 33: #p1
point_RER = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
if idx == 145:
point_REB = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
# setting the rest of the eye coordinates for EAR
# RE
if idx == 160:
p2_RE = (lm.x *img_w , lm.y*img_h)
if idx == 158:
p3_RE = (lm.x *img_w , lm.y*img_h)
if idx == 153:
p5_RE = (lm.x *img_w , lm.y*img_h)
if idx == 144:
p6_RE = (lm.x *img_w , lm.y*img_h)
#
if idx ==1:
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 255, 0), thickness=-1)
if idx == 133: #p4
point_REL = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
if idx == 159:
point_RET = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
# EAR calculation coordinates Left Eye
if idx == 362: # p1_LE
point_LER = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
if idx == 385:
p2_LE = (lm.x * img_w, lm.y * img_h)
if idx == 387:
p3_LE = (lm.x * img_w, lm.y * img_h)
if idx == 373:
p5_LE = (lm.x * img_w, lm.y * img_h)
if idx == 380:
p6_LE = (lm.x * img_w, lm.y * img_h)
#
if idx == 374:
point_LEB = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
if idx == 263: # p4 left eye
point_LEL = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
if idx == 386:
point_LET = (lm.x * img_w, lm.y * img_h)
cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 0, 255), thickness=-1)
if idx == 468:
point_REIC = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(255, 255, 0), thickness=-1)
if idx == 469:
point_469 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 255, 0), thickness=-1)
if idx == 470:
point_470 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 255, 0), thickness=-1)
if idx == 471:
point_471 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 255, 0), thickness=-1)
if idx == 472:
point_472 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 255, 0), thickness=-1)
if idx == 473:
point_LEIC = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(0, 255, 255), thickness=-1)
if idx == 474:
point_474 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(255, 0, 0), thickness=-1)
if idx == 475:
point_475 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(255, 0, 0), thickness=-1)
if idx == 476:
point_476 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(255, 0, 0), thickness=-1)
if idx == 477:
point_477 = (lm.x * img_w, lm.y * img_h)
#cv2.circle(image, (int(lm.x * img_w), int(lm.y * img_h)), radius=5, color=(255, 0, 0), thickness=-1)
if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
if idx == 1:
nose_2d = (lm.x * img_w, lm.y * img_h)
nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)
x, y = int(lm.x * img_w), int(lm.y * img_h)
face_2d.append([x, y])
face_3d.append([x, y, lm.z])
#converting into numpy arrays
face_2d = np.array(face_2d, dtype=np.float64)
face_3d = np.array(face_3d, dtype=np.float64)
# Calcuate camera matrix
focal_length = 2 * img_w
cam_matrix = np.array([ [focal_length, 0, img_h / 2],
[0, focal_length, img_w / 2],
[0, 0, 1]])
dist_matrix = np.zeros((4,1), dtype=np.float64)
## Calculate head gaze based on 3d points
# Solve PnP
success, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix)
# Get rotational matrices
rmat, jac = cv2.Rodrigues(rot_vec)
# Get angles
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
# Convert angles in degrees
pitch = angles[0] * 1800
yaw = -angles[1] * 1800
roll = 180 + (np.arctan2(point_RER[1] - point_LEL[1], point_RER[0] - point_LEL[0]) * 180 / np.pi)
if roll > 180:
roll = roll - 360
# Display head gaze angles
cv2.putText(image, f"HEAD Roll: {roll:.2f} Pitch: {pitch:.2f} Yaw: {yaw:.2f}", (10, 160), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
head_distracted = 0
if abs(roll) > 30 or abs(pitch) > 30 or abs(yaw) > 30:
head_distracted = 1
## Calculate eyes gaze based on 2d points ##
width_fraction = 0.5
height_fraction = 0.4
down_gaze_fraction = 0.3
# compute some eye parameters
r_eye_height = point_REB[1] - point_RET[1]
l_eye_height = point_LEB[1] - point_LET[1]
r_eye_center = [(point_REB[0] + point_RET[0])/2, (point_REL[1] + point_RER[1])/2]
l_eye_center = [(point_LEB[0] + point_LET[0])/2, (point_LEL[1] + point_LER[1])/2]
# calculate vertical distance between upper eye lid and eyebrow
r_lid_eyebrow_dist = point_RET[1] - point_RET[1]
l_lid_eyebrow_dist = p3_LE[1] - point_LET[1]
r_semiaxes = (int(abs(point_469[0]-point_471[0])*0.5 * width_fraction),
int(abs(point_472[1]-point_470[1])*0.5 * height_fraction))
r_a = r_semiaxes[0]
r_b = r_semiaxes[1]
r_xc = int(r_eye_center[0])
r_yc = int(r_eye_center[1])
r_x = point_REIC[0]
r_y = point_REIC[1]
##
l_semiaxes = (int(abs(point_474[0]-point_476[0])*0.5 * width_fraction),
int(abs(point_477[1]-point_475[1])*0.5 * height_fraction))
l_a = l_semiaxes[0]
l_b = l_semiaxes[1]
l_xc = int(l_eye_center[0])
l_yc = int(l_eye_center[1])
l_x = point_LEIC[0]
l_y = point_LEIC[1]
#checking eye angles
# RIGHT EYE
re_pitch = 0
re_yaw = 0
if r_y >= r_yc + r_b*down_gaze_fraction or r_lid_eyebrow_dist > 0.4*r_eye_height:
re_pitch = -1
if r_y <= r_yc - r_b:
re_pitch = 1
if r_x >= r_xc + r_a:
re_yaw = 1
if r_x <= r_xc - r_a:
re_yaw = -1
cv2.putText(image, f"Right_eye Pitch: {re_pitch:.2f} Yaw: {re_yaw:.2f}", (10, 115), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# LEFT EYE
le_pitch = 0
l_yaw = 0
if l_y >= l_yc+ l_b*down_gaze_fraction or l_lid_eyebrow_dist > 0.4*l_eye_height:
le_pitch = -1
if l_y <= l_yc - l_b:
le_pitch = 1
if l_x >= l_xc + l_a:
l_yaw = 1
if l_x <= l_xc - l_a:
l_yaw = -1
cv2.putText(image, f"Left_eye Pitch: {le_pitch:.2f} Yaw: {l_yaw:.2f}", (10, 140), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
eyes_distracted = 0
if abs(re_pitch) >= 1 or abs(le_pitch) >= 1 or abs(re_yaw) >= 1 or abs(l_yaw) >= 1:
eyes_distracted = 1
######calculating EAR
# Compute EAR for left and right eyes
left_coord = [point_LER, p2_LE, p3_LE, point_LEL, p5_LE, p6_LE]
ear_L = ear_cal(left_coord)
right_coord = [point_RER, p2_RE, p3_RE, point_REL, p5_RE, p6_RE]
ear_R = ear_cal(right_coord)
# Average both eyes EAR
avg_ear = (ear_L + ear_R) / 2
# perclos logic
# Get the current time for timing drowsiness
current_time = time.time()
# Define the threshold based on 80% of the baseline EAR.
# According to literature, drowsiness is typically inferred when EAR falls
# below a percentage of a person's normal (baseline) eye openness.
threshold = 0.8 * baseline_ear
# If the EAR remains below the threshold, then consider it a sign of drowsiness.
if avg_ear > threshold:
if drowsy_start_time is None:
drowsy_start_time = current_time # start the drowsiness timer
else:
drowsiness_timer = current_time - drowsy_start_time # update timer
else:
drowsy_start_time = None # reset if eye reopens
drowsiness_timer = 0
# Display the drowsiness timer in the bottom right of the image.
# cv2.putText(image, f"Drowsiness: {drowsiness_timer:.2f} sec",
# (250, 20),
# cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# When the drowsiness timer exceeds 10 seconds, display an alarm message.
if drowsiness_timer >= 10:
cv2.putText(image, "DROWSINESS ALARM",
(200,225),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
# PERCLOS LOGIC
ear_history.append((avg_ear, current_time))
ear_history = [(ear, t) for ear, t in ear_history \
if current_time - t <= interval_duration_perclos]
if (current_time - update_time_perclos) >=1:
perclos_val = perclos_cal(ear_history, threshold_perclos)
update_time_perclos = current_time
# cv2.putText(image, f'PERCLOS: {perclos_val: .2f}' , \
# (10,80), cv2.FONT_HERSHEY_SIMPLEX,1,(240,150,200),2)
if perclos_val >= 0.8:
cv2.putText(image, 'Possible Drowsiness', (200,260), \
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,165,255),4)
##Distraction criteria
#####################
# 4.4. - Draw the positions on the frame
l_eye_width = point_LEL[0] - point_LER[0]
l_eye_height = point_LEB[1] - point_LET[1]
l_eye_center = [(point_LEL[0] + point_LER[0])/2 ,(point_LEB[1] + point_LET[1])/2]
#cv2.circle(image, (int(l_eye_center[0]), int(l_eye_center[1])), radius=int(horizontal_threshold * l_eye_width), color=(255, 0, 0), thickness=-1) #center of eye and its radius
cv2.circle(image, (int(point_LEIC[0]), int(point_LEIC[1])), radius=3, color=(0, 255, 0), thickness=-1) # Center of iris
cv2.circle(image, (int(l_eye_center[0]), int(l_eye_center[1])), radius=2, color=(128, 128, 128), thickness=-1) # Center of eye
#print("Left eye: x = " + str(np.round(point_LEIC[0],0)) + " , y = " + str(np.round(point_LEIC[1],0)))
cv2.putText(image, "Left eye: x = " + str(np.round(point_LEIC[0],0)) + " , y = " + str(np.round(point_LEIC[1],0)), (10, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
r_eye_width = point_REL[0] - point_RER[0]
r_eye_height = point_REB[1] - point_RET[1]
r_eye_center = [(point_REL[0] + point_RER[0])/2 ,(point_REB[1] + point_RET[1])/2]
##############3
##################
#cv2.circle(image, (int(r_eye_center[0]), int(r_eye_center[1])), radius=int(horizontal_threshold * r_eye_width), color=(255, 0, 0), thickness=-1) #center of eye and its radius
cv2.circle(image, (int(point_REIC[0]), int(point_REIC[1])), radius=3, color=(0, 0, 255), thickness=-1) # Center of iris
cv2.circle(image, (int(r_eye_center[0]), int(r_eye_center[1])), radius=2, color=(128, 128, 128), thickness=-1) # Center of eye
#print("right eye: x = " + str(np.round(point_REIC[0],0)) + " , y = " + str(np.round(point_REIC[1],0)))
cv2.putText(image, "Right eye: x = " + str(np.round(point_REIC[0],0)) + " , y = " + str(np.round(point_REIC[1],0)), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
###########my texts
cv2.putText(image, "Left_EAR: " + str(np.round(ear_L, 2)), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.putText(image, "Right_EAR: " + str(np.round(ear_R, 2)), (10, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.putText(image, f'PERCLOS value: {perclos_val: .2f}' , \
(10,95), cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,90),2)
# speed reduction (comment out for full speed)
cv2.putText(image, f'distraction flags: Head_{head_distracted}_Eye_{eyes_distracted}' , \
(10,180), cv2.FONT_HERSHEY_SIMPLEX,0.5,(90,0,255),2)
time.sleep(1/25) # [s]
end = time.time()
totalTime = end-start
if totalTime>0:
fps = 1 / totalTime
else:
fps=0
# Detect if driver is distracted
if head_distracted and eyes_distracted:
distracted_timeout += totalTime
if distracted_timeout >= 200: # if the head and eye gaze continued for more than a certain amount of time
cv2.putText(image, f'DISTRACTION ALARM', (200,300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
else:
distracted_timeout = 0
cv2.putText(image, f'Distracted time: {int(distracted_timeout)}', (20,400), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
#print("FPS:", fps)
# cv2.putText(image, f'FPS : {int(fps)}', (20,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# 4.5 - Show the frame to the user
cv2.imshow('Technologies for Autonomous Vehicles - Driver Monitoring Systems using AI code sample', image)
if cv2.waitKey(5) & 0xFF == 27:
break
# 5 - Close properly source and eventual log file
cap.release()
#log_file.close()
# [EOF]