-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (42 loc) · 1.77 KB
/
Copy pathutils.py
File metadata and controls
56 lines (42 loc) · 1.77 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
#filename:utils.py
import cv2
import mediapipe as mp
import numpy as np
def get_face_landmarks(image, face_mesh, draw=False, static_image_mode=True):
# Read input image
image_input_rgb = cv2.cvtColor (image, cv2.COLOR_BGR2RGB)
image_rows, image_cols, _ = image.shape
results= face_mesh.process(image_input_rgb)
image_landmarks = []
if results.multi_face_landmarks:
if draw:
mp_drawing= mp.solutions.drawing_utils
mp_drawing_styles= mp.solutions.drawing_styles
drawing_spec= mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
mp_drawing.draw_landmarks(
image= image,
landmark_list= results.multi_face_landmarks[0],
connections= mp.solutions.face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec= drawing_spec,
connection_drawing_spec= drawing_spec,
)
ls_single_face= results.multi_face_landmarks[0].landmark
xs_ =[]
ys_ =[]
zs_ =[]
for idx in ls_single_face:
xs_.append(idx.x)
ys_.append(idx.y)
zs_.append(idx.z)
# Normalize landmarks using Min-Max scaling for each face
x_min, x_max = np.min(xs_), np.max(xs_)
y_min, y_max = np.min(ys_), np.max(ys_)
z_min, z_max = np.min(zs_), np.max(zs_)
x_range = x_max - x_min
y_range = y_max - y_min
z_range = z_max - z_min
for j in range(len(xs_)):
image_landmarks.append((xs_[j] - x_min) / (x_range if x_range > 0 else 1))
image_landmarks.append((ys_[j] - y_min) / (y_range if y_range > 0 else 1))
image_landmarks.append((zs_[j] - z_min) / (z_range if z_range > 0 else 1))
return image_landmarks