-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2).Eye_Blinking_Detection.py
81 lines (57 loc) · 2.91 KB
/
2).Eye_Blinking_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
import cv2
import numpy as np
import dlib
from math import hypot
# we used the detector to detect the frontal face
detector = dlib.get_frontal_face_detector()
# it will dectect the facial landwark points
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
font = cv2.FONT_HERSHEY_PLAIN
#We create a function that we will need later on to detect the medium point.
#On this function we simply put the coordinates of two points and will return the medium point
#(the points in the middle between the two points).
def midpoint(p1 ,p2):
return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)
def get_blinking_ratio(eye_points, facial_landmarks):
# to detect the left_side of a left eye
left_point = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
# to detect the right_side of the left eye
right_point = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
# to detect the mid point for the center of top in left eye
center_top = midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
# to detect the mid point for the center of the bottom in left eye
center_bottom = midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))
# creating the horizontal line from the left_side to the right_side of the left eye
hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
# creating the vertical line from the center_top to center_bottom
ver_line = cv2.line(frame, center_top, center_bottom, (0, 255, 0), 2)
# to calculate horizontal line distance
hor_line_lenght = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
# to calculate vertical line distance
ver_line_lenght = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))
# to calculate ratio
ratio = hor_line_lenght / ver_line_lenght
return ratio
# to open webcab to capture the image
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
# change the color of the frame captured by webcam to grey
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# to detect faces from grey color frame
faces = detector(gray)
for face in faces:
# to detect the landmarks of a face
landmarks = predictor(gray, face)
left_eye_ratio = get_blinking_ratio([36, 37, 38, 39, 40, 41], landmarks)
right_eye_ratio = get_blinking_ratio([42, 43, 44, 45, 46, 47], landmarks)
blinking_ratio = (left_eye_ratio + right_eye_ratio) / 2
if blinking_ratio > 5.7:
cv2.putText(frame, "BLINKING", (50, 150), font, 7, (255, 0, 0))
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
# close the webcam when escape key is pressed
if key == 27:
break
cap.release()
cv2.destroyAllWindows()