-
Notifications
You must be signed in to change notification settings - Fork 0
/
camDetectionFunctions.py
304 lines (251 loc) · 7.61 KB
/
camDetectionFunctions.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import math
import cv2
import numpy as np
from typing import NamedTuple
# from shapely.geometry import Polygon
class Point(NamedTuple):
x: float
y: float
# Define the landmarks for the face and eyes
face_border_indices = [
10,
338,
297,
332,
284,
251,
389,
356,
454,
323,
361,
288,
397,
365,
379,
378,
400,
377,
152,
148,
176,
149,
150,
136,
172,
58,
132,
93,
234,
127,
162,
21,
54,
103,
67,
109,
]
left_eye_indices = [
362,
382,
381,
380,
374,
373,
390,
249,
263,
466,
388,
387,
386,
385,
384,
398,
]
right_eye_indices = [
33,
7,
163,
144,
145,
153,
154,
155,
133,
173,
157,
158,
159,
160,
161,
246,
]
def get_distance(p1: Point, p2: Point) -> float:
"""
Calculate the Euclidean distance between two points in a 2D plane.
Parameters:
p1 (Point): A point having coordinates (x, y).
p2 (Point): Another point having coordinates (x, y).
Returns:
float: The Euclidean distance between point p1 and point p2.
Assumes that both p1 and p2 are objects with 'x' and 'y' attributes,
representing their respective coordinates on the 2D plane.
Note:
This function requires the math module to be imported.
Example:
>>> p1 = Point(1, 2)
>>> p2 = Point(4, 6)
>>> get_distance(p1, p2)
5.0
"""
return math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2)
def get_face_orientation(face_landmarks, img_w, img_h):
# Initialize lists to store 2D and 3D points
face_2d = []
face_3d = []
for idx, lm in enumerate(face_landmarks.landmark):
# Non so perché fa 'sta cosa
if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
x, y = int(lm.x * img_w), int(lm.y * img_h)
# Get 2D coordinates
face_2d.append([x, y])
# Get 3D coordinates
face_3d.append([x, y, lm.z])
# Convert to NumPy array
face_2d = np.array(face_2d, dtype=np.float64)
face_3d = np.array(face_3d, dtype=np.float64)
# Camera matrix
focal_length = 1 * img_w
cam_matrix = np.array(
[
[focal_length, 0, img_h / 2],
[0, focal_length, img_w / 2],
[0, 0, 1],
]
)
# Distortion parameters
dist_matrix = np.zeros((4, 1), dtype=np.float64)
# Solve PnP
# This function call is where the magic happens. It uses the camera matrix and the distortion parameters to solve the Perspective-n-Point (PnP) problem. The PnP problem involves finding the position and orientation (rotation and translation) of a 3D object in space, given a set of 3D points on the object (face_3d), their corresponding 2D projections on the image (face_2d), and the camera parameters. In this case, it's likely trying to determine the position and orientation of a face relative to the camera.
success, rot_vec, trans_vec = cv2.solvePnP(
face_3d, face_2d, cam_matrix, dist_matrix
)
# Get rotational matrix
rmat, jac = cv2.Rodrigues(rot_vec)
# Get angles
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
# Convert angles to degrees
x = angles[0] * (360)
y = angles[1] * (360)
z = angles[2] * (360)
return x, y, z
def get_eye_aperture_ratio_SEGMENTSMETHOD(face_landmarks):
# Landmarks for eye width calculation
LElandmarksW = [
face_landmarks.landmark[33],
face_landmarks.landmark[133],
] # Corrected indices for left eye horizontal
RElandmarksW = [
face_landmarks.landmark[362],
face_landmarks.landmark[263],
] # Corrected indices for right eye horizontal
# Landmarks for eye height calculation
LElandmarksH = [
face_landmarks.landmark[159],
face_landmarks.landmark[145],
] # Corrected indices for left eye vertical
RElandmarksH = [
face_landmarks.landmark[386],
face_landmarks.landmark[374],
] # Corrected indices for right eye vertical
# Calculate distances
LEw = get_distance(LElandmarksW[0], LElandmarksW[1])
REw = get_distance(RElandmarksW[0], RElandmarksW[1])
LEh = get_distance(LElandmarksH[0], LElandmarksH[1])
REh = get_distance(RElandmarksH[0], RElandmarksH[1])
# Calculate ratios
LERatio = LEh / LEw if LEw > 0 else 0
RERatio = REh / REw if REw > 0 else 0
return (LERatio, RERatio)
def get_eye_aperture_ratio_TWOSEGMENTSMETHOD(face_landmarks):
# Landmarks for left eye
LElandmarksW = [
face_landmarks.landmark[33],
face_landmarks.landmark[133],
]
LElandmarksH1 = [
face_landmarks.landmark[159],
face_landmarks.landmark[145],
]
LElandmarksH2 = [
face_landmarks.landmark[158],
face_landmarks.landmark[153],
]
# Landmarks for right eye
RElandmarksW = [
face_landmarks.landmark[362],
face_landmarks.landmark[263],
]
RElandmarksH1 = [
face_landmarks.landmark[386],
face_landmarks.landmark[374],
]
RElandmarksH2 = [
face_landmarks.landmark[385],
face_landmarks.landmark[380],
]
# Calculate distances
LEw = get_distance(LElandmarksW[0], LElandmarksW[1])
LEh1 = get_distance(LElandmarksH1[0], LElandmarksH1[1])
LEh2 = get_distance(LElandmarksH2[0], LElandmarksH2[1])
REw = get_distance(RElandmarksW[0], RElandmarksW[1])
REh1 = get_distance(RElandmarksH1[0], RElandmarksH1[1])
REh2 = get_distance(RElandmarksH2[0], RElandmarksH2[1])
# Calculate ratios
LERatio = (LEh1 + LEh2) / (2 * LEw) if LEw > 0 else 0
RERatio = (REh1 + REh2) / (2 * REw) if REw > 0 else 0
return (LERatio, RERatio)
# def get_eye_face_ratio_AREASMETHOD(face_landmarks):
# # Get the coordinates from the landmarks
# face_coords = [
# (face_landmarks.landmark[i].x, face_landmarks.landmark[i].y)
# for i in face_border_indices
# ]
# left_eye_coords = [
# (face_landmarks.landmark[i].x, face_landmarks.landmark[i].y)
# for i in left_eye_indices
# ]
# right_eye_coords = [
# (face_landmarks.landmark[i].x, face_landmarks.landmark[i].y)
# for i in right_eye_indices
# ]
# # Create polygons
# face_polygon = Polygon(face_coords)
# left_eye_polygon = Polygon(left_eye_coords)
# right_eye_polygon = Polygon(right_eye_coords)
# # Calculate areas
# face_area = face_polygon.area
# left_eye_area = left_eye_polygon.area
# right_eye_area = right_eye_polygon.area
# # Calculate ratios
# left_eye_ratio = left_eye_area / face_area if face_area > 0 else 0
# right_eye_ratio = right_eye_area / face_area if face_area > 0 else 0
# return (left_eye_ratio, right_eye_ratio)
def get_mouth_aperture_ratio(face_landmarks):
# Landmarks for upper to lower lip distance
MlandmarksH = [face_landmarks.landmark[13], face_landmarks.landmark[14]]
# Landmarks for mouth width
MlandmarksW = [face_landmarks.landmark[78], face_landmarks.landmark[366]]
# Get distances
mh = get_distance(MlandmarksH[0], MlandmarksH[1])
mw = get_distance(MlandmarksW[0], MlandmarksW[1])
# Calculate ratios
Mratio = mh / mw if mw > 0 else 0
return Mratio
def get_head_roll(face_landmarks):
# ^^ Calculate the angle described by the landmarks 127 and 356
p1 = Point(face_landmarks.landmark[127].x, face_landmarks.landmark[127].y)
p2 = Point(face_landmarks.landmark[356].x, face_landmarks.landmark[356].y)
angle = math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / math.pi
return angle