forked from belan581/call-with-eyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eyes_move_detection.py
220 lines (199 loc) · 9.01 KB
/
eyes_move_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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import csv
import math
import os
import time
import winsound
import cv2 as cv
import joblib
import mediapipe as mp
import numpy as np
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
from tensorflow.keras.models import load_model
class eyesMoveDetection:
# Interest points
# right_eye = [157, 158, 159, 160, 161, 163, 144, 145, 153, 154, 155, 246, 33, 133]
right_eye = [157, 158, 159, 160, 161, 163, 144, 145, 153, 154]
iris_derecho = [468]
left_eye = [384, 385, 386, 387, 388, 390, 373, 374, 380, 381]
iris_izquierdo = [473]
references = [67, 297, 127, 264, 205, 425, 168]
nose = [4]
train_landmarks = right_eye + left_eye
full_landmarks = train_landmarks + iris_izquierdo + iris_derecho
# Load de model
model = load_model("model/modelo_call_with_eyes.h5")
# Load the scaler
scaler = joblib.load("model/scaler_entrenamiento.pkl")
def __init__(self):
self.mp_face_mesh = self.load_mediapipe()
def load_mediapipe(self):
mp_face_mesh = mp.solutions.face_mesh
return mp_face_mesh
def landmarks_to_px(self, landmark, image_width, image_height):
return int(landmark.x * image_width), int(landmark.y * image_height)
def get_landmarks_coordinates(self, frame, video_res=[1280, 720]):
with self.mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
) as face_mesh:
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
frame.flags.writeable = False
frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
results = face_mesh.process(frame)
# Draw the face mesh annotations on the image.
frame.flags.writeable = True
frame = cv.cvtColor(frame, cv.COLOR_RGB2BGR)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
points = []
ref_points = []
for idx, landmark in enumerate(face_landmarks.landmark):
if idx in self.train_landmarks:
point = [idx, landmark.x, landmark.y]
points.append(point)
point = self.landmarks_to_px(
landmark, video_res[0], video_res[1]
)
cv.circle(frame, point, 2, (0, 255, 0), -1)
if idx in self.references:
ref_point = [idx, landmark.x, landmark.y]
ref_points.append(ref_point)
ref_point = self.landmarks_to_px(
landmark, video_res[0], video_res[1]
)
cv.circle(frame, ref_point, 2, (0, 255, 0), -1)
if idx in self.iris_derecho:
right_iris = idx, landmark.x, landmark.y
righ_iris_point = self.landmarks_to_px(
landmark, video_res[0], video_res[1]
)
cv.circle(frame, righ_iris_point, 2, (0, 255, 0), -1)
if idx in self.iris_izquierdo:
left_iris = idx, landmark.x, landmark.y
left_iris_point = self.landmarks_to_px(
landmark, video_res[0], video_res[1]
)
cv.circle(frame, left_iris_point, 2, (0, 255, 0), -1)
if idx in self.nose:
nose_point = self.landmarks_to_px(
landmark, video_res[0], video_res[1]
)
cv.circle(frame, nose_point, 2, (0, 255, 0), -1)
return (
frame,
points,
self.can_process(points + ref_points),
right_iris,
left_iris,
)
else:
return frame, [], False, [], []
def calcular_distancia(self, punto1, punto2):
dx = (punto1[1] - punto2[1]) ** 2
dy = (punto1[2] - punto2[2]) ** 2
return math.sqrt(dx + dy)
def calcular_distancia_dos(self, punto1, punto2):
dx = (punto1[0] - punto2[0]) ** 2
dy = (punto1[1] - punto2[1]) ** 2
return math.sqrt(dx + dy)
def is_close(self, d1, d2):
close = math.isclose(d1, d2, rel_tol=0.1)
return close
def get_data(self, x, lst):
res = [(i, row) for i, row in enumerate(lst) if x in row]
return res[0][1]
def can_process(self, data):
center = self.get_data(168, data)
right = self.get_data(127, data)
left = self.get_data(264, data)
d_center_right = self.calcular_distancia(center, right)
d_center_left = self.calcular_distancia(center, left)
res = self.is_close(d_center_right, d_center_left)
return res
def compute_gesture(self, points, right_iris, left_iris):
distances = []
for p in points:
try:
distances.append(self.calcular_distancia(punto1=p, punto2=right_iris))
except Exception as e:
distances.append(0.0)
print(f"Error: {e.args}")
for p in points:
try:
distances.append(self.calcular_distancia(punto1=p, punto2=left_iris))
except Exception as e:
distances.append(0.0)
print(f"Error: {e.args}")
# caracteristicas = np.array([distances])
X_sample = np.array([distances])
# X_sample = np.expand_dims(distances, axis=0)
X_sample_normalized = self.scaler.transform(X_sample)
gesto_predicho = self.model.predict(X_sample_normalized).argmax()
# gesto_predicho_prob = np.argmax(gesto_predicho, axis=1)
return gesto_predicho
def save_gesture(self, option: int, frame, freq):
data = []
with self.mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
) as face_mesh:
for i in range(100):
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
frame.flags.writeable = False
frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
results = face_mesh.process(frame)
# Draw the face mesh annotations on the image.
frame.flags.writeable = True
frame = cv.cvtColor(frame, cv.COLOR_RGB2BGR)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
points = []
right_iris = []
left_iris = []
for idx, landmark in enumerate(face_landmarks.landmark):
if idx in self.train_landmarks:
point = landmark.x, landmark.y
points.append(point)
if idx in self.iris_derecho:
right_iris = landmark.x, landmark.y
if idx in self.iris_izquierdo:
left_iris = landmark.x, landmark.y
# print(points)
distances = []
for p in points:
try:
distances.append(
self.calcular_distancia_dos(punto1=p, punto2=right_iris)
)
except Exception as e:
distances.append(0.0)
print(f"Error: {e.args}")
# print(f"distance_right:{distances}")
# distances.append(option)
# data.append(distances)
for p in points:
try:
distances.append(
self.calcular_distancia_dos(punto1=p, punto2=left_iris)
)
except Exception as e:
distances = 0
print(f"Error: {e.args}")
# print(f"distance_right:{distances}")
distances.append(option)
print(len(distances))
data.append(distances)
time.sleep(freq)
self.write_data(data, "distances")
winsound.PlaySound("*", winsound.SND_ALIAS)
def write_data(self, data, name):
with open(f"{name}.csv", "a", newline="") as file:
writer = csv.writer(file, delimiter=",")
writer.writerows(data)
file.close()