-
Notifications
You must be signed in to change notification settings - Fork 20
/
Driver Drowsiness Detection.py
228 lines (199 loc) · 8.97 KB
/
Driver Drowsiness Detection.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
#!/usr/bin/env python
from scipy.spatial import distance as dist
from imutils.video import VideoStream
from imutils import face_utils
import argparse
import imutils
import time
import dlib
import math
from cv2 import cv2
import numpy as np
from EAR import eye_aspect_ratio
from MAR import mouth_aspect_ratio
from HeadPose import getHeadTiltAndCoords
# initialize dlib's face detector (HOG-based) and then create the
# facial landmark predictor
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(
'./dlib_shape_predictor/shape_predictor_68_face_landmarks.dat')
# initialize the video stream and sleep for a bit, allowing the
# camera sensor to warm up
print("[INFO] initializing camera...")
vs = VideoStream(src=1).start()
# vs = VideoStream(usePiCamera=True).start() # Raspberry Pi
time.sleep(2.0)
# 400x225 to 1024x576
frame_width = 1024
frame_height = 576
# loop over the frames from the video stream
# 2D image points. If you change the image, you need to change vector
image_points = np.array([
(359, 391), # Nose tip 34
(399, 561), # Chin 9
(337, 297), # Left eye left corner 37
(513, 301), # Right eye right corne 46
(345, 465), # Left Mouth corner 49
(453, 469) # Right mouth corner 55
], dtype="double")
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
EYE_AR_THRESH = 0.25
MOUTH_AR_THRESH = 0.79
EYE_AR_CONSEC_FRAMES = 3
COUNTER = 0
# grab the indexes of the facial landmarks for the mouth
(mStart, mEnd) = (49, 68)
while True:
# grab the frame from the threaded video stream, resize it to
# have a maximum width of 400 pixels, and convert it to
# grayscale
frame = vs.read()
frame = imutils.resize(frame, width=1024, height=576)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
size = gray.shape
# detect faces in the grayscale frame
rects = detector(gray, 0)
# check to see if a face was detected, and if so, draw the total
# number of faces on the frame
if len(rects) > 0:
text = "{} face(s) found".format(len(rects))
cv2.putText(frame, text, (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# loop over the face detections
for rect in rects:
# compute the bounding box of the face and draw it on the
# frame
(bX, bY, bW, bH) = face_utils.rect_to_bb(rect)
cv2.rectangle(frame, (bX, bY), (bX + bW, bY + bH), (0, 255, 0), 1)
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# extract the left and right eye coordinates, then use the
# coordinates to compute the eye aspect ratio for both eyes
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
# average the eye aspect ratio together for both eyes
ear = (leftEAR + rightEAR) / 2.0
# compute the convex hull for the left and right eye, then
# visualize each of the eyes
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
# check to see if the eye aspect ratio is below the blink
# threshold, and if so, increment the blink frame counter
if ear < EYE_AR_THRESH:
COUNTER += 1
# if the eyes were closed for a sufficient number of times
# then show the warning
if COUNTER >= EYE_AR_CONSEC_FRAMES:
cv2.putText(frame, "Eyes Closed!", (500, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# otherwise, the eye aspect ratio is not below the blink
# threshold, so reset the counter and alarm
else:
COUNTER = 0
mouth = shape[mStart:mEnd]
mouthMAR = mouth_aspect_ratio(mouth)
mar = mouthMAR
# compute the convex hull for the mouth, then
# visualize the mouth
mouthHull = cv2.convexHull(mouth)
cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1)
cv2.putText(frame, "MAR: {:.2f}".format(mar), (650, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# Draw text if mouth is open
if mar > MOUTH_AR_THRESH:
cv2.putText(frame, "Yawning!", (800, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw each of them
for (i, (x, y)) in enumerate(shape):
if i == 33:
# something to our key landmarks
# save to our new key point list
# i.e. keypoints = [(i,(x,y))]
image_points[0] = np.array([x, y], dtype='double')
# write on frame in Green
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0), 1)
elif i == 8:
# something to our key landmarks
# save to our new key point list
# i.e. keypoints = [(i,(x,y))]
image_points[1] = np.array([x, y], dtype='double')
# write on frame in Green
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0), 1)
elif i == 36:
# something to our key landmarks
# save to our new key point list
# i.e. keypoints = [(i,(x,y))]
image_points[2] = np.array([x, y], dtype='double')
# write on frame in Green
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0), 1)
elif i == 45:
# something to our key landmarks
# save to our new key point list
# i.e. keypoints = [(i,(x,y))]
image_points[3] = np.array([x, y], dtype='double')
# write on frame in Green
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0), 1)
elif i == 48:
# something to our key landmarks
# save to our new key point list
# i.e. keypoints = [(i,(x,y))]
image_points[4] = np.array([x, y], dtype='double')
# write on frame in Green
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0), 1)
elif i == 54:
# something to our key landmarks
# save to our new key point list
# i.e. keypoints = [(i,(x,y))]
image_points[5] = np.array([x, y], dtype='double')
# write on frame in Green
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0), 1)
else:
# everything to all other landmarks
# write on frame in Red
cv2.circle(frame, (x, y), 1, (0, 0, 255), -1)
cv2.putText(frame, str(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
#Draw the determinant image points onto the person's face
for p in image_points:
cv2.circle(frame, (int(p[0]), int(p[1])), 3, (0, 0, 255), -1)
(head_tilt_degree, start_point, end_point,
end_point_alt) = getHeadTiltAndCoords(size, image_points, frame_height)
cv2.line(frame, start_point, end_point, (255, 0, 0), 2)
cv2.line(frame, start_point, end_point_alt, (0, 0, 255), 2)
if head_tilt_degree:
cv2.putText(frame, 'Head Tilt Degree: ' + str(head_tilt_degree[0]), (170, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# extract the mouth coordinates, then use the
# coordinates to compute the mouth aspect ratio
# show the frameq
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# print(image_points)
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()