-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrate.py
132 lines (93 loc) · 3.48 KB
/
calibrate.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import cv2
import imutils
from utils import get_rect_vertices, get_point_coords, calculate_ear, notify
import dlib
import time
from playsound import playsound
import sys
user_video = cv2.VideoCapture(0)
# user_video.open("http://192.168.0.4:8080/video") # Im using my phone's camera as my laptop webcam quality is too bad
face_detector = dlib.get_frontal_face_detector()
landmarks_predictor = dlib.shape_predictor(
"./shape_predictor_68_face_landmarks.dat")
def get_threshold():
return float(open("threshold.txt", "r").read())
def set_threshold(t):
f = open("threshold.txt", "w")
f.write(str(t))
f.close()
THRESHOLD = get_threshold()
FRAME_COUNT = 3
COUNTER = 0
BLINK_COUNT = 0
timer = time.time()
index = 0
calibrate = True
while True:
e, frame = user_video.read()
try:
frame = imutils.resize(frame, width=600)
except AttributeError:
notify("Heyelth - Error", "Please check your webcam and calibrate again!")
exit()
unchanged_frame = cv2.flip(frame, 1)
frame = cv2.flip(frame, 1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detecting Faces
detected_faces = face_detector(gray)
for face in detected_faces:
# Drawing a Rectangle on the detected faces.
x1, x2, y1, y2 = get_rect_vertices(face)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 255, 0), 3)
landmarks = landmarks_predictor(gray, face)
left_eye_points = []
for i in range(36, 43):
points = tuple(get_point_coords(landmarks.part(i)))
left_eye_points.append(points)
cv2.circle(frame, points, 1, (255, 0, 255), 1)
right_eye_points = []
for i in range(42, 48):
points = tuple(get_point_coords(landmarks.part(i)))
right_eye_points.append(points)
cv2.circle(frame, points, 1, (0, 255, 255), 1)
leftEyeEAR = calculate_ear(left_eye_points)
rightEyeEAR = calculate_ear(right_eye_points)
averageEAR = (leftEyeEAR + rightEyeEAR) / 2
if averageEAR < THRESHOLD:
COUNTER += 1
else:
if COUNTER >= FRAME_COUNT:
BLINK_COUNT += 1
COUNTER = 0
cv2.putText(frame, f"Total Blinks: {BLINK_COUNT}",
(10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 150), 2)
cv2.putText(frame, f"EAR: {str(averageEAR)[:4]}",
(10, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 150, 0), 2)
cv2.putText(frame, f"Threshold: {str(THRESHOLD)[:4]}",
(10, 90), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0), 2)
if (calibrate):
cv2.putText(frame, str(index), (200, 270), cv2.FONT_HERSHEY_DUPLEX, 10, (0, 0, 0), 10)
if time.time() - timer >= 2:
timer = time.time()
index += 1
if index == 10 and calibrate:
print("Blinked", BLINK_COUNT, "times")
if BLINK_COUNT > 10:
set_threshold(THRESHOLD + 0.015)
elif BLINK_COUNT < 9:
set_threshold(THRESHOLD - 0.01)
if THRESHOLD == get_threshold():
calibrate = False
print("Threshold is the same")
exit()
THRESHOLD = get_threshold()
calibrate = False
exit()
cv2.imshow("Debug Feed", frame)
# cv2.imshow("Unchanged User Feed", unchanged_frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
user_video.release()
cv2.destroyAllWindows()
exit()