-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·157 lines (116 loc) · 5.34 KB
/
test.py
File metadata and controls
executable file
·157 lines (116 loc) · 5.34 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/env python3
import numpy as np
import time
import sys
import cv2
import math
import dlib
BLINK_RATIO_THRESHOLD = 7
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
left_eye_landmarks = [36, 37, 38, 39, 40, 41]
right_eye_landmarks = [42, 43, 44, 45, 46, 47]
colorTreshold = [
([17, 15, 100], [50, 56, 200]), #red
([86, 31, 4], [220, 88, 50]), #blue
]
def midpoint(point1 ,point2):
return int((point1.x + point2.x)/2), int((point1.y + point2.y)/2)
def euclidean_distance(point1 , point2):
return math.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)
def getEyeRegion(eye_points, facial_landmarks):
corner_left = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
corner_right = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
center_top = midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
center_bottom = midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))
return [corner_left, center_top, center_bottom, corner_right]
def get_blink_ratio(eye_points, facial_landmarks):
ratio = 0
#loading all the required points
corner_left = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
corner_right = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
center_top = midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
center_bottom = midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))
#calculating distance
horizontal_length = euclidean_distance(corner_left,corner_right)
vertical_length = euclidean_distance(center_top,center_bottom)
if vertical_length > 0:
ratio = horizontal_length / vertical_length
return ratio
def detectBrightness(dst):
brightness = dst[...,2].mean()
if brightness < 110:
print("Pas assez lumineux")
print(brightness)
# else:
# print("C'est ok")
def turnCameraOn():
blinkFrequency = 0
blinkNb = 0
oldBlink = 0
newBlink = 0
while(True):
# Create the capture frame by frame
ret, frame = cap.read()
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Brightness detection
dst = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
detectBrightness(dst)
# Detection of the eye
faces,_,_ = detector.run(image = frame, upsample_num_times = 0, adjust_threshold = 0.0)
for face in faces:
landmarks = predictor(frame, face)
left_eye_ratio = get_blink_ratio(left_eye_landmarks, landmarks)
right_eye_ratio = get_blink_ratio(right_eye_landmarks, landmarks)
blink_ratio = (left_eye_ratio + right_eye_ratio) / 2
# eyePos = List of position (x,y) : corner right, center top, center bottom and corner left
if blink_ratio > BLINK_RATIO_THRESHOLD:
oldBlink = newBlink
newBlink = time.time()
blinkNb += 1
# print (newBlink - oldBlink)
cv2.putText(frame,"BLINKING",(10,50), cv2.FONT_HERSHEY_SIMPLEX, 2,(255,255,255),2,cv2.LINE_AA)
if newBlink - oldBlink < 1 and newBlink - oldBlink > 0.1 and blinkNb > 6:
blinkNb = 0
print("c rapide un peu")
cv2.imshow('frame', frame)
#ROI the eye
clone = frame.copy()
eyePosLeft = getEyeRegion(left_eye_landmarks ,landmarks)
eyePosRight = getEyeRegion(right_eye_landmarks ,landmarks)
leftStartPoint = (eyePosLeft[0][0],eyePosLeft[1][1])
leftEndPoint = (eyePosLeft[3][0],eyePosLeft[2][1])
rightStartPoint = (eyePosRight[0][0],eyePosRight[1][1])
rightEndPoint = (eyePosRight[3][0],eyePosRight[2][1])
# clone = cv2.rectangle(clone, leftStartPoint, leftEndPoint, (0,255,0), 2)
# clone = cv2.rectangle(clone, rightStartPoint, rightEndPoint, (0,255,0), 2)
leftArea = clone[leftStartPoint[1]:leftEndPoint[1], leftStartPoint[0]:leftEndPoint[0]]
rightArea = clone[rightStartPoint[1]:rightEndPoint[1], rightStartPoint[0]:rightEndPoint[0]]
# for (lower, upper) in colorTreshold:
# # create NumPy arrays from the boundaries
# lower = np.array(lower, dtype = "uint8")
# upper = np.array(upper, dtype = "uint8")
# # find the colors within the specified boundaries and apply
# # the mask
# mask = cv2.inRange(leftArea, lower, upper)
# output = cv2.bitwise_and(leftArea, leftArea, mask = mask)
# # show the images
# cv2.imshow("images", np.hstack([leftArea, output]))
# cv2.waitKey(0)
# cv2.imshow('left', leftArea)
# cv2.imshow('right', rightArea)
colorSum = 0
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# release the capture and destroy the window
cap.release()
cv2.destroyAllWindows()
def leader():
turnCameraOn()
if __name__ == '__main__':
try:
leader()
except (ValueError, IndexError, OSError, KeyError):
sys.exit(84)