-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrowinessDetection.py
70 lines (62 loc) · 2.17 KB
/
DrowinessDetection.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
import imutils
import face_recognition
import cv2
from scipy.spatial import distance as dist
import playsound
from threading import Thread
import numpy as np
import os
MIN_AER =0.30
EYE_AR_CONSEC_FRAMES =10
COUNTER = 0
ALARM_ON = False
def playAlarm(soundfile):
playsound.playsound(soundfile)
def eye_aspect_ratio(eye):
V1 = dist.euclidean(eye[1],eye[5])
V2 = dist.euclidean(eye[2], eye[4])
H = dist.euclidean(eye[0], eye[3])
ear = (V1+V2)/(2.0*H)
return ear
def main():
global COUNTER,ALARM_ON
video_capture = cv2.VideoCapture(0)
video_capture.set(3,320)
video_capture.set(4,240)
while True:
ret ,frame = video_capture.read()
face_landmarks_list = face_recognition.face_landmarks(frame)
for face_landmark in face_landmarks_list:
leftEye = face_landmark["left_eye"]
rightEye = face_landmark["right_eye"]
lpts = np.array(leftEye)
rpts = np.array(rightEye)
cv2.polylines(frame,[lpts],True,(255,255,0),1)
cv2.polylines(frame, [rpts], True, (255, 255, 0), 1)
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR+rightEAR)/2
if ear<MIN_AER:
COUNTER+=1
if COUNTER>=EYE_AR_CONSEC_FRAMES:
if not ALARM_ON:
ALARM_ON = True
t = Thread(target=playAlarm,args=('alarm.wav',))
t.daemon = True
t.start()
cv2.putText(frame,"ALERT! You are feeling asleep!",(10,10),
cv2.FONT_HERSHEY_SIMPLEX,0.4,(0,0,255),1)
if COUNTER>=30:
shutDownComputer()
else:
COUNTER=0
ALARM_ON = False
cv2.putText(frame, "EAR: {:.2f}".format(ear), (250, 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1)
cv2.imshow("Sleep detection system",frame)
if cv2.waitKey(1)== ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()