-
Notifications
You must be signed in to change notification settings - Fork 0
/
drowsiness detection.py
276 lines (202 loc) · 7.83 KB
/
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
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
from __future__ import division
import dlib
import imutils
from imutils import face_utils
import cv2
import numpy as np
from scipy.spatial import distance as dist
from threading import Thread
import threading
import pygame
import argparse
from gpiozero import Buzzer
from time import sleep
import serial
def start_sound2():
pygame.mixer.init()
pygame.mixer.music.load("Yawn.mp3")
pygame.mixer.music.play()
def lip_distance(shape):
top_lip = shape[50:53]
top_lip = np.concatenate((top_lip, shape[61:64]))
low_lip = shape[56:59]
low_lip = np.concatenate((low_lip, shape[65:68]))
top_mean = np.mean(top_lip, axis=0)
low_mean = np.mean(low_lip, axis=0)
distance = abs(top_mean[1] - low_mean[1])
return distance
def pep():
buzzer.on()
sleep(1)
buzzer.off()
sleep(1)
def blue1():
port="/dev/rfcomm0"
bluetooth=serial.Serial(port, 9600)
bluetooth.flushInput()
for i in range(5):
bluetooth.write(b"s"+str.encode(str(i)))
input_data=bluetooth.readline()
print(input_data.decode())
time.sleep(0.1)
bluetooth.close()
def blue2():
print("Start")
port="/dev/rfcomm0"
bluetooth=serial.Serial(port, 9600)
print("Connected")
bluetooth.flushInput()
for i in range(5):
print("Ping")
bluetooth.write(b"a"+str.encode(str(i)))
input_data=bluetooth.readline()
print(input_data.decode())
time.sleep(0.1)
bluetooth.close()
print("Done")
camera = cv2.VideoCapture(0)
s, img = camera.read()
if s:
cv2.imwrite("belt.jpg",img)
#Slope of line
def Slope(a,b,c,d):
return (d - b)/(c - a)
# Reading Image
beltframe = cv2.imread("belt.jpg")
# Resizing The Image
beltframe = imutils.resize(beltframe, height=800)
#Converting To GrayScale
beltgray = cv2.cvtColor(beltframe, cv2.COLOR_BGR2GRAY)
belt = False
# Bluring The Image For Smoothness
blur = cv2.blur(beltgray, (1, 1))
# Converting Image To Edges
edges = cv2.Canny(blur, 50, 400)
ps = 0
px1, py1, px2, py2 = 0, 0, 0, 0
# Extracting Lines
lines = cv2.HoughLinesP(edges, 1, np.pi/270, 30, maxLineGap = 20, minLineLength = 170)
# If "lines" Is Not Empty
if lines is not None:
# Loop line by line
for line in lines:
# Co-ordinates Of Current Line
x1, y1, x2, y2 = line[0]
# Slope Of Current Line
s = Slope(x1,y1,x2,y2)
# If Current Line's Slope Is Greater Than 0.7 And Less Than 2
if ((abs(s) > 0.7) and (abs (s) < 2)):
# And Previous Line's Slope Is Within 0.7 To 2
if((abs(ps) > 0.7) and (abs(ps) < 2)):
# And Both The Lines Are Not Too Far From Each Other
if(((abs(x1 - px1) > 5) and (abs(x2 - px2) > 5)) or ((abs(y1 - py1) > 5) and (abs(y2 - py2) > 5))):
# Plot The Lines On "beltframe"
cv2.line(beltframe, (x1, y1), (x2, y2), (0, 0, 255), 3)
cv2.line(beltframe, (px1, py1), (px2, py2), (0, 0, 255), 3)
# Belt Is Detected
print ("Belt Detected")
belt = True
# Otherwise Current Slope Becomes Previous Slope (ps) And Current Line Becomes Previous Line (px1, py1, px2, py2)
ps = s
px1, py1, px2, py2 = line[0]
if belt == False:
print("No Seatbelt detected")
blue1()
exit()
buzzer = Buzzer(17)
def resize(img, width=None, height=None, interpolation=cv2.INTER_AREA):
global ratio
w, h = img.shape
if width is None and height is None:
return img
elif width is None:
ratio = height / h
width = int(w * ratio)
resized = cv2.resize(img, (height, width), interpolation)
return resized
else:
ratio = width / w
height = int(h * ratio)
resized = cv2.resize(img, (height, width), interpolation)
return resized
######
def shape_to_np(shape, dtype="int"):
coords = np.zeros((68, 2), dtype=dtype)
for i in range(36,68):
coords[i] = (shape.part(i).x, shape.part(i).y)
return coords
def eye_aspect_ratio(eye):
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
# compute the euclidean distance between the horizontal
# eye landmark (x, y)-coordinates
C = dist.euclidean(eye[0], eye[3])
# compute the eye aspect ratio
ear = (A + B) / (2.0 * C)
# return the eye aspect ratio
return ear
alarm=False
YAWN_THRESH = 12
predictor_path = 'shape_predictor_68_face_landmarks.dat'
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
predictor = dlib.shape_predictor(predictor_path)
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
total=0
while True:
ret, frame = camera.read()
if ret == False:
print('Failed to capture frame from camera. Check camera index in cv2.VideoCapture(0) \n')
break
frame_grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# frame_resized = imutils.resize(frame, width=120)
frame_resized = resize(frame_grey, width=120)
dets = detector.detectMultiScale(frame_resized,scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(dets) > 0:
for (x, y, w, h) in dets:
d = dlib.rectangle(int(x), int(y), int(x + w),int(y + h))
shape = predictor(frame_resized, d)
# shape = face_utils.shape_to_np(shape)
shape = shape_to_np(shape)
leftEye= shape[lStart:lEnd]
rightEye= shape[rStart:rEnd]
leftEAR= eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
distance = lip_distance(shape)
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)
lip = shape[48:60]
cv2.drawContours(frame, [lip], -1, (0, 255, 0), 1)
if (distance > YAWN_THRESH):
cv2.putText(frame, "Yawn Alert", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
alarm=True
t=threading.Thread(target=start_sound2)
t.start()
t.setDaemon=True
else:
alarm=False
if ear>.25:
#print (ear)
total=0
else:
total+=1
if total>9:
t = Thread(target = pep,args=())
t.deamon = True
t.start()
cv2.putText(frame, "drowsiness detect" ,(250, 30),cv2.FONT_HERSHEY_SIMPLEX, 1.7, (0, 0, 0), 4)
if total>16:
blue2()
for (x, y) in shape:
cv2.circle(frame, (int(x/ratio), int(y/ratio)), 3, (255, 255, 255), -1)
cv2.imshow("image", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
camera.release()
break