This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
eye_blink.py
105 lines (93 loc) · 3.64 KB
/
eye_blink.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
from __future__ import division
import dlib
from imutils import face_utils
import cv2
import numpy as np
from scipy.spatial import distance as dist
import threading
import pygame
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,48):
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
camera = cv2.VideoCapture(0)
predictor_path = 'shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
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
alarm=True
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 = resize(frame_grey, width=120)
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(frame_resized, 1)
if len(dets) > 0:
for k, d in enumerate(dets):
shape = predictor(frame_resized, d)
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
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)
if ear>.25:
print (round(ear,2))
#total=0
alarm=False
cv2.putText(frame, "Eyes Open ", (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
else:
total+=1
if total>20:
if not alarm:
alarm=True
#print("so jaaaaaaaaaa")
cv2.putText(frame, "drowsiness detect" ,(250, 30),cv2.FONT_HERSHEY_SIMPLEX, 1.7, (0, 0, 0), 4)
cv2.putText(frame, "Eyes close".format(total), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
for (x, y) in shape:
cv2.circle(frame, (int(x/ratio), int(y/ratio)), 3, (255, 255, 255), -1)
print(total)
cv2.putText(frame, str(total), (100, 100),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow("image", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
camera.release()
break