/
drowsiness_detect.py
114 lines (94 loc) · 3.91 KB
/
drowsiness_detect.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
#This is for geometric calculations in euclidean space
from scipy.spatial import distance
#This is for eye detection
from imutils import face_utils
#Matrix calculations
import numpy as np
#For playing sound
import pygame
import time
#For face detection and classification
import dlib
#Graphic input handling in this case the video
import cv2
#Initialize Pygame and load music
pygame.mixer.init()
pygame.mixer.music.load('audio/alert.wav')
#Minimum threshold of eye aspect ratio below which alarm is triggerd
#limit: > 0.2 < 0.4
EYE_ASPECT_RATIO_THRESHOLD = 0.2
#Minimum consecutive frames for which eye ratio is below threshold for alarm to be triggered
EYE_ASPECT_RATIO_CONSEC_FRAMES = 50
#Counts no. of consecutive frames below threshold value
COUNTER = 0
#Load face cascade which will be used to draw a rectangle around detected faces.
face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")
#This function calculates and return eye aspect ratio
def eye_aspect_ratio(eye):
print(eye)
A = distance.euclidean(eye[1], eye[5])
B = distance.euclidean(eye[2], eye[4])
C = distance.euclidean(eye[0], eye[3])
ear = (A+B) / (2*C)
return ear
#Load face detector and predictor, uses dlib shape predictor file
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
#Extract indexes of facial landmarks for the left and right eye
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS['left_eye']
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS['right_eye']
#Start webcam video capture
video_capture = cv2.VideoCapture("video1.mp4")
#Give some time for camera to initialize(not required)
time.sleep(2)
count=0
while(True):
#Read each frame and flip it, and convert to grayscale
ret, frame = video_capture.read() #--> Que hago con el ret?
#if count == 3:
# break
count=count+1
if ret == False:
break
#frame = cv2.flip(frame,0) --> preguntar a martin cual es la intention de tener esta linea aqui
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Detect facial points through detector function
faces = detector(gray, 0)
#Detect faces through haarcascade_frontalface_default.xml
face_rectangle = face_cascade.detectMultiScale(gray, 1.3, 5)
#Draw rectangle around each face detected
for (x,y,w,h) in face_rectangle:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
#Detect facial points
for face in faces:
shape = predictor(gray, face)
shape = face_utils.shape_to_np(shape)
#Get array of coordinates of leftEye and rightEye
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
#Calculate aspect ratio of both eyes
leftEyeAspectRatio = eye_aspect_ratio(leftEye)
rightEyeAspectRatio = eye_aspect_ratio(rightEye)
eyeAspectRatio = (leftEyeAspectRatio + rightEyeAspectRatio) / 2
#Use hull to remove convex contour discrepencies and draw eye shape around 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)
#Detect if eye aspect ratio is less than threshold
if(eyeAspectRatio < EYE_ASPECT_RATIO_THRESHOLD):
COUNTER += 1
#If no. of frames is greater than threshold frames,
if COUNTER >= EYE_ASPECT_RATIO_CONSEC_FRAMES:
pygame.mixer.music.play(-1)
cv2.putText(frame, "ALERTA!", (150,200), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,0,255), 2)
else:
pygame.mixer.music.stop()
COUNTER = 0
#Show video feed
cv2.imshow('Video', frame)
if(cv2.waitKey(1) & 0xFF == ord('q')):
break
#Finally when video capture is over, release the video capture and destroyAllWindows
video_capture.release()
cv2.destroyAllWindows()