-
Notifications
You must be signed in to change notification settings - Fork 9
/
detecDrowsiness.py
executable file
·170 lines (129 loc) · 5.45 KB
/
detecDrowsiness.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
#!/usr/bin/env python
import cv2
import time
import numpy as np
from scipy.spatial import distance as dist
from imutils import face_utils
import dlib
EYE_AR_THRESH = 0.3
EYE_AR_CONSEC_FRAMES = 7
MOUTH_AR_THRESH = 0.4
SHOW_POINTS_FACE = False
SHOW_CONVEX_HULL_FACE = False
SHOW_INFO = False
ear = 0
mar = 0
COUNTER_FRAMES_EYE = 0
COUNTER_FRAMES_MOUTH = 0
COUNTER_BLINK = 0
COUNTER_MOUTH = 0
def eye_aspect_ratio(eye):
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
C = dist.euclidean(eye[0], eye[3])
return (A + B) / (2.0 * C)
def mouth_aspect_ratio(mouth):
A = dist.euclidean(mouth[5], mouth[8])
B = dist.euclidean(mouth[1], mouth[11])
C = dist.euclidean(mouth[0], mouth[6])
return (A + B) / (2.0 * C)
videoSteam = cv2.VideoCapture(0)
ret, frame = videoSteam.read()
size = frame.shape
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
model_points = np.array([(0.0, 0.0, 0.0),
(0.0, -330.0, -65.0),
(-225.0, 170.0, -135.0),
(225.0, 170.0, -135.0),
(-150.0, -150.0, -125.0),
(150.0, -150.0, -125.0)])
focal_length = size[1]
center = (size[1]/2, size[0]/2)
camera_matrix = np.array([[focal_length, 0, center[0]],
[0, focal_length, center[1]],
[0, 0, 1]], dtype = "double")
dist_coeffs = np.zeros((4,1))
t_end = time.time()
while(True):
ret, frame = videoSteam.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
jaw = shape[48:61]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
mar = mouth_aspect_ratio(jaw)
image_points = np.array([
(shape[30][0], shape[30][1]),
(shape[8][0], shape[8][1]),
(shape[36][0], shape[36][1]),
(shape[45][0], shape[45][1]),
(shape[48][0], shape[48][1]),
(shape[54][0], shape[54][1])
], dtype="double")
(success, rotation_vector, translation_vector) = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE)
(nose_end_point2D, jacobian) = cv2.projectPoints(np.array([(0.0, 0.0, 1000.0)]), rotation_vector, translation_vector, camera_matrix, dist_coeffs)
if SHOW_POINTS_FACE:
for p in image_points:
cv2.circle(frame, (int(p[0]), int(p[1])), 3, (0,0,255), -1)
p1 = (int(image_points[0][0]), int(image_points[0][1]))
p2 = (int(nose_end_point2D[0][0][0]), int(nose_end_point2D[0][0][1]))
if SHOW_CONVEX_HULL_FACE:
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
jawHull = cv2.convexHull(jaw)
cv2.drawContours(frame, [leftEyeHull], 0, (255, 255, 255), 1)
cv2.drawContours(frame, [rightEyeHull], 0, (255, 255, 255), 1)
cv2.drawContours(frame, [jawHull], 0, (255, 255, 255), 1)
cv2.line(frame, p1, p2, (255,255,255), 2)
if p2[1] > p1[1]*1.5 or COUNTER_BLINK > 25 or COUNTER_MOUTH > 2:
cv2.putText(frame, "Send Alert!", (200, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
if ear < EYE_AR_THRESH:
COUNTER_FRAMES_EYE += 1
if COUNTER_FRAMES_EYE >= EYE_AR_CONSEC_FRAMES:
cv2.putText(frame, "Sleeping Driver!", (200, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
else:
if COUNTER_FRAMES_EYE > 2:
COUNTER_BLINK += 1
COUNTER_FRAMES_EYE = 0
if mar >= MOUTH_AR_THRESH:
COUNTER_FRAMES_MOUTH += 1
else:
if COUNTER_FRAMES_MOUTH > 5:
COUNTER_MOUTH += 1
COUNTER_FRAMES_MOUTH = 0
if (time.time() - t_end) > 60:
t_end = time.time()
COUNTER_BLINK = 0
COUNTER_MOUTH = 0
if SHOW_INFO:
cv2.putText(frame, "EAR: {:.2f}".format(ear), (30, 450),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.putText(frame, "MAR: {:.2f}".format(mar), (200, 450),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.putText(frame, "Blinks: {}".format(COUNTER_BLINK), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.putText(frame, "Mouths: {}".format(COUNTER_MOUTH), (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.imshow("Output", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
if key == ord('p'):
SHOW_POINTS_FACE = not SHOW_POINTS_FACE
if key == ord('c'):
SHOW_CONVEX_HULL_FACE = not SHOW_CONVEX_HULL_FACE
if key == ord('i'):
SHOW_INFO = not SHOW_INFO
time.sleep(0.02)
videoSteam.release()
cv2.destroyAllWindows()