-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain_dlib.py
76 lines (70 loc) · 2.34 KB
/
main_dlib.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
import cv2
import math
import numpy as np
import dlib
import imutils
from imutils import face_utils
from matplotlib import pyplot as plt
import vlc
import train as train
def euclideanDist(a, b):
return (math.sqrt(math.pow(a[0]-b[0], 2)+math.pow(a[1]-b[1], 2)))
#EAR -> Eye Aspect ratio
def ear(eye):
return ((euclideanDist(eye[1], eye[5])+euclideanDist(eye[2], eye[4]))/(2*euclideanDist(eye[0], eye[3])))
def writeEyes(a, b, img):
y1 = max(a[1][1], a[2][1])
y2 = min(a[4][1], a[5][1])
x1 = a[0][0]
x2 = a[3][0]
cv2.imwrite('left-eye.jpg', img[y1:y2, x1:x2])
y1 = max(b[1][1], b[2][1])
y2 = min(b[4][1], b[5][1])
x1 = b[0][0]
x2 = b[3][0]
cv2.imwrite('right-eye.jpg', img[y1:y2, x1:x2])
# open_avg = train.getAvg()
# close_avg = train.getAvg()
alert = vlc.MediaPlayer('alert-sound.mp3')
frame_thresh = 15
close_thresh = 0.3#(close_avg+open_avg)/2.0
flag = 0
print(close_thresh)
capture = cv2.VideoCapture(0)
avgEAR = 0
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
(leStart, leEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(reStart, reEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
while(True):
ret, frame = capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
if(len(rects)):
shape = face_utils.shape_to_np(predictor(gray, rects[0]))
leftEye = shape[leStart:leEnd]
rightEye = shape[reStart:reEnd]
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
leftEAR = ear(leftEye) #Get the left eye aspect ratio
rightEAR = ear(rightEye) #Get the right eye aspect ratio
avgEAR = (leftEAR+rightEAR)/2.0
if(avgEAR<close_thresh):
flag+=1
print(flag)
if(flag>=frame_thresh):
alert.play()
elif(avgEAR>close_thresh and flag):
print("Flag reseted to 0")
alert.stop()
flag=0
cv2.drawContours(gray, [leftEyeHull], -1, (255, 255, 255), 1)
cv2.drawContours(gray, [rightEyeHull], -1, (255, 255, 255), 1)
writeEyes(leftEye, rightEye, frame)
if(avgEAR>close_thresh):
alert.stop()
cv2.imshow('Driver', gray)
if(cv2.waitKey(1)==27):
break
capture.release()
cv2.destroyAllWindows()