Skip to content

Commit 1644466

Browse files
Add files via upload
1 parent 8edc7d1 commit 1644466

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

Eye blinking detection/Readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Eye Blinking Detection
2+
## Authors
3+
4+
- [@Yashrajsingh2001](https://github.com/Yashrajsingh2001)
5+
6+
7+
## Installation
8+
9+
For installing required libraries run:
10+
11+
```bash
12+
pip install opencv-python
13+
pip install numpy
14+
pip install dlib
15+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import cv2
2+
import numpy as np
3+
import dlib
4+
from math import hypot
5+
6+
cap = cv2.VideoCapture(0)
7+
8+
detector = dlib.get_frontal_face_detector()
9+
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #available on kaggle
10+
11+
def midpoint(p1 ,p2):
12+
return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)
13+
14+
font = cv2.FONT_HERSHEY_PLAIN
15+
16+
def get_blinking_ratio(eye_points, facial_landmarks):
17+
left_point = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
18+
right_point = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
19+
center_top = midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
20+
center_bottom = midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))
21+
22+
hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
23+
ver_line = cv2.line(frame, center_top, center_bottom, (0, 255, 0), 2)
24+
25+
hor_line_lenght = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
26+
ver_line_lenght = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))
27+
28+
ratio = hor_line_lenght / ver_line_lenght
29+
return ratio
30+
31+
while True:
32+
_, frame = cap.read()
33+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
34+
35+
faces = detector(gray)
36+
for face in faces:
37+
#x, y = face.left(), face.top()
38+
#x1, y1 = face.right(), face.bottom()
39+
#cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
40+
41+
landmarks = predictor(gray, face)
42+
43+
left_eye_ratio = get_blinking_ratio([36, 37, 38, 39, 40, 41], landmarks)
44+
right_eye_ratio = get_blinking_ratio([42, 43, 44, 45, 46, 47], landmarks)
45+
blinking_ratio = (left_eye_ratio + right_eye_ratio) / 2
46+
47+
if blinking_ratio > 5.7:
48+
cv2.putText(frame, "BLINKING", (50, 150), font, 7, (255, 0, 0))
49+
50+
51+
cv2.imshow("Frame", frame)
52+
53+
key = cv2.waitKey(1)
54+
if key == 27:
55+
break
56+
57+
cap.release()
58+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)