-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
100 lines (92 loc) · 1.69 KB
/
models.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
from giskard_vision.landmark_detection.models.base import FaceLandmarksModelBase
import mediapipe as mp
import numpy as np
model_path = "face_landmarker.task"
BaseOptions = mp.tasks.BaseOptions
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode
options = FaceLandmarkerOptions(
base_options=BaseOptions(model_asset_path=model_path), running_mode=VisionRunningMode.IMAGE
)
# index of the landmarks to match the dlib landmarks
DLIB_MASK = [
162,
234,
93,
58,
172,
136,
149,
148,
152,
377,
378,
365,
397,
288,
323,
454,
389,
71,
63,
105,
66,
107,
336,
296,
334,
293,
301,
168,
197,
5,
4,
75,
97,
2,
326,
305,
33,
160,
158,
133,
153,
144,
362,
385,
387,
263,
373,
380,
61,
39,
37,
0,
267,
269,
291,
405,
314,
17,
84,
181,
78,
82,
13,
312,
308,
317,
14,
87,
]
class MediapipeWrapper(FaceLandmarksModelBase):
def __init__(self):
super().__init__(n_landmarks=68, n_dimensions=2, name="Mediapipe")
self.mp_face_mesh = mp.solutions.face_mesh
self.landmarker = FaceLandmarker.create_from_options(options)
def predict_image(self, image):
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=image)
results = self.landmarker.detect(image)
landmarks_array = np.array([[l.x, l.y] for landmarks in results.face_landmarks for l in landmarks], np.float32)
return landmarks_array[DLIB_MASK]