-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheyes_position.py
180 lines (135 loc) · 6.22 KB
/
eyes_position.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import math
import autopy
import cv2 as cv2
import numpy as np
import mediapipe as mp
from scipy.spatial import distance as dist
# TODO: Receber os valores do retângulo em volta dos olhos
def move_mouse(initial_point, final_point, c_left):
# Iris Movimentation
put_text('iris')
cv2.rectangle(frame, initial_point, final_point, (255, 255, 255), 1)
x1, y1 = c_left[0], c_left[1]
w, h = autopy.screen.size()
X = int(np.interp(x1, [initial_point[0], final_point[0]], [0, w - 1]))
Y = int(np.interp(y1, [initial_point[1], final_point[1]], [0, h - 1]))
if X % 2 != 0:
X = X - X % 2
if Y % 2 != 0:
Y = Y - Y % 2
# print(X, Y)
autopy.mouse.move(X, Y)
# defining a function to calculate the EAR
def calculate_EAR(eye):
# calculate the vertical distances
y1 = dist.euclidean(eye[1], eye[5])
y2 = dist.euclidean(eye[2], eye[4])
# calculate the horizontal distance
x1 = dist.euclidean(eye[0], eye[3])
# calculate the EAR
EAR = (y1 + y2) / x1
return EAR
# TODO: Criar função para clique do mouse através da piscada
def put_text(text_mode, loc=(250, 450), text_color=(0, 255, 255)):
cv2.putText(frame, str(text_mode), loc, cv2.FONT_HERSHEY_COMPLEX_SMALL,
3, text_color, 3)
def euclidian_distance(point_one, point_two):
x1, y1 = point_one.ravel()
x2, y2 = point_two.ravel()
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return distance
def iris_position(iris_center, right_point, left_point, eye):
position_iris = ''
ratio = None
center_to_right_distance = euclidian_distance(iris_center, right_point)
center_to_left_distance = euclidian_distance(iris_center, left_point)
total_distance = euclidian_distance(right_point, left_point)
if eye == 'left':
ratio = center_to_left_distance / total_distance
elif eye == 'right':
ratio = center_to_right_distance / total_distance
if ratio <= 0.42:
position_iris = 'right'
elif 0.42 < ratio <= 0.57:
position_iris = 'center'
elif ratio > 0.57:
position_iris = 'left'
return position_iris, ratio
# Início do código principal
mp_face_mesh = mp.solutions.face_mesh
# Right Eyes Points
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
RIGHT_IRIS = [474, 475, 476, 477]
R_H_LEFT = [33]
R_H_RIGHT = [133]
R_REC = [33, 145, 133, 159]
# Left Eyes Points
LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
LEFT_IRIS = [469, 470, 471, 472]
L_H_LEFT = [362]
L_H_RIGHT = [263]
L_REC = [362, 374, 263, 386]
i = 0
camera = cv2.VideoCapture(0)
with mp_face_mesh.FaceMesh(max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
while True:
ret, frame = camera.read()
if not ret:
break
scale_percent = 200 # percent of original size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image_h, image_w = frame.shape[:2]
results = face_mesh.process(rgb_frame)
if results.multi_face_landmarks:
mesh_points = np.array([np.multiply([p.x, p.y], [image_w, image_h]).astype(int) for p in
results.multi_face_landmarks[0].landmark])
# Iris Detection: Create Circle
(l_cx, l_cy), l_radius = cv2.minEnclosingCircle(mesh_points[LEFT_IRIS])
(r_cx, r_cy), r_radius = cv2.minEnclosingCircle(mesh_points[RIGHT_IRIS])
center_left = np.array([l_cx, l_cy], dtype=np.int32)
center_right = np.array([r_cx, r_cy], dtype=np.int32)
# Iris Detection: Circle
cv2.circle(frame, center_left, int(l_radius), (255, 0, 255), 1, cv2.LINE_AA)
cv2.circle(frame, center_right, int(r_radius), (255, 0, 255), 1, cv2.LINE_AA)
# Iris Detection: Right Points
cv2.circle(frame, mesh_points[R_H_RIGHT][0], 1, (255, 0, 255), -1, cv2.LINE_AA)
cv2.circle(frame, mesh_points[R_H_LEFT][0], 1, (0, 255, 255), -1, cv2.LINE_AA)
# Iris Detection: Left Points
cv2.circle(frame, mesh_points[L_H_RIGHT][0], 1, (0, 255, 255), -1, cv2.LINE_AA)
cv2.circle(frame, mesh_points[L_H_LEFT][0], 1, (255, 0, 255), -1, cv2.LINE_AA)
# Iris Detection: Position
left_iris_pos, ratio_l = iris_position(center_left, mesh_points[L_H_LEFT],
mesh_points[L_H_RIGHT][0], 'left')
right_iris_pos, ratio_r = iris_position(center_right, mesh_points[R_H_RIGHT],
mesh_points[R_H_LEFT][0], 'right')
# Iris Detection: Result With Detection
cv2.putText(frame, 'Left Iris Position: {} | Right Iris Position {}'.format(left_iris_pos, right_iris_pos),
(30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 1, cv2.LINE_AA)
# Iris Rectangle (Colocar os pontos que formam um rentângulo em torno do olho a partid dos pontos extremos)
# cv2.polylines(frame, [mesh_points[LEFT_EYE]], True, (0, 255, 0), 1, cv2.LINE_AA)
# cv2.polylines(frame, [mesh_points[RIGHT_EYE]], True, (0, 255, 0), 1, cv2.LINE_AA)
# criar uma linha entre os dois olhos e pegar o ponto central da linha para nortear o movimento
cv2.line(frame, center_left, center_right, (0, 0, 0), 1, cv2.LINE_AA)
# point_xi = center_left[0] - 36
# point_yi = center_left[1] + 20
# point_xf = center_left[0] + 36
# point_yf = center_left[1] - 20
# p_ini = (point_xi, point_yi)
# p_fin = (point_xf, point_yf)
# if i == 0:
pi = (512, 384)
pf = (768, 576)
# i += 1
move_mouse(pi, pf, center_left)
cv2.imshow('image', frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
camera.release()
cv2.destroyAllWindows()