-
Notifications
You must be signed in to change notification settings - Fork 37
/
a_person_face_landmark_mask_generator_comfyui.py
331 lines (299 loc) · 9.09 KB
/
a_person_face_landmark_mask_generator_comfyui.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import cv2 as cv
import torch
import numpy as np
from PIL import Image
import mediapipe as mp
class APersonFaceLandmarkMaskGenerator:
# https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/python/solutions/face_mesh_connections.py
# order matters for these
FACEMESH_FACE_OVAL = [
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,
]
FACEMESH_LEFT_EYEBROW = [336, 296, 334, 293, 300, 276, 283, 282, 295, 285]
FACEMESH_RIGHT_EYEBROW = [70, 63, 105, 66, 107, 55, 65, 52, 53, 46]
FACEMESH_LEFT_EYE = [
362,
382,
381,
380,
374,
373,
390,
249,
263,
466,
388,
387,
386,
385,
384,
398,
]
FACEMESH_RIGHT_EYE = [
33,
7,
163,
144,
145,
153,
154,
155,
133,
173,
157,
158,
159,
160,
161,
246,
]
FACEMESH_LEFT_PUPIL = [474, 475, 476, 477]
FACEMESH_RIGHT_PUPIL = [469, 470, 471, 472]
FACEMESH_LIPS = [
61,
146,
91,
181,
84,
17,
314,
405,
321,
375,
291,
308,
324,
318,
402,
317,
14,
87,
178,
88,
95,
185,
40,
39,
37,
0,
267,
269,
270,
409,
415,
310,
311,
312,
13,
82,
81,
42,
183,
78,
]
def __init__(self):
pass
@classmethod
def INPUT_TYPES(self):
false_widget = (
"BOOLEAN",
{"default": False, "label_on": "enabled", "label_off": "disabled"},
)
true_widget = (
"BOOLEAN",
{"default": True, "label_on": "enabled", "label_off": "disabled"},
)
return {
"required": {
"images": ("IMAGE",),
},
"optional": {
"face": false_widget,
"left_eyebrow": false_widget,
"right_eyebrow": false_widget,
"left_eye": true_widget,
"right_eye": true_widget,
"left_pupil": false_widget,
"right_pupil": false_widget,
"lips": true_widget,
"number_of_faces": (
"INT",
{"default": 1, "min": 1, "max": 10, "step": 1},
),
"confidence": (
"FLOAT",
{"default": 0.40, "min": 0.01, "max": 1.0, "step": 0.01},
),
},
}
CATEGORY = "A Person Mask Generator - David Bielejeski"
RETURN_TYPES = ("MASK",)
RETURN_NAMES = ("masks",)
FUNCTION = "generate_mask"
def generate_mask(
self,
images,
face: bool,
left_eyebrow: bool,
right_eyebrow: bool,
left_eye: bool,
right_eye: bool,
left_pupil: bool,
right_pupil: bool,
lips: bool,
number_of_faces: int,
confidence: float,
):
"""Create a segmentation mask from an image
Args:
image (torch.Tensor): The image to create the mask for.
face (bool): create a mask for the face.
left_eyebrow (bool): create a mask for the left eyebrow.
right_eyebrow (bool): create a mask for the right eyebrow.
left_eye (bool): create a mask for the left eye.
right_eye (bool): create a mask for the right eye.
left_pupil (bool): create a mask for the left eye pupil.
right_pupil (bool): create a mask for the right eye pupil.
lips (bool): create a mask for the lips.
Returns:
torch.Tensor: The segmentation masks.
"""
res_masks = []
with mp.solutions.face_mesh.FaceMesh(
static_image_mode=True,
refine_landmarks=True,
max_num_faces=number_of_faces,
min_detection_confidence=confidence,
) as face_mesh:
for image in images:
# Convert the Tensor to a PIL image
i = 255.0 * image.cpu().numpy()
image_pil = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
# Process the image
results = (
face_mesh.process(np.asarray(image_pil))
if any(
[
face,
left_eyebrow,
right_eyebrow,
left_eye,
right_eye,
left_pupil,
right_pupil,
lips,
]
)
else None
)
img_width, img_height = image_pil.size
mask = np.zeros((img_height, img_width), dtype=np.uint8)
if results and results.multi_face_landmarks:
mesh_coords = [
(int(point.x * img_width), int(point.y * img_height))
for point in results.multi_face_landmarks[0].landmark
]
if face:
face_coords = [mesh_coords[p] for p in self.FACEMESH_FACE_OVAL]
cv.fillPoly(mask, [np.array(face_coords, dtype=np.int32)], 255)
else:
if left_eyebrow:
left_eyebrow_coords = [
mesh_coords[p] for p in self.FACEMESH_LEFT_EYEBROW
]
cv.fillPoly(
mask,
[np.array(left_eyebrow_coords, dtype=np.int32)],
255,
)
if right_eyebrow:
right_eyebrow_coords = [
mesh_coords[p] for p in self.FACEMESH_RIGHT_EYEBROW
]
cv.fillPoly(
mask,
[np.array(right_eyebrow_coords, dtype=np.int32)],
255,
)
if left_eye:
left_eye_coords = [
mesh_coords[p] for p in self.FACEMESH_LEFT_EYE
]
cv.fillPoly(
mask, [np.array(left_eye_coords, dtype=np.int32)], 255
)
if right_eye:
right_eye_coords = [
mesh_coords[p] for p in self.FACEMESH_RIGHT_EYE
]
cv.fillPoly(
mask, [np.array(right_eye_coords, dtype=np.int32)], 255
)
if left_pupil:
left_pupil_coords = [
mesh_coords[p] for p in self.FACEMESH_LEFT_PUPIL
]
cv.fillPoly(
mask, [np.array(left_pupil_coords, dtype=np.int32)], 255
)
if right_pupil:
right_pupil_coords = [
mesh_coords[p] for p in self.FACEMESH_RIGHT_PUPIL
]
cv.fillPoly(
mask,
[np.array(right_pupil_coords, dtype=np.int32)],
255,
)
if lips:
lips_coords = [mesh_coords[p] for p in self.FACEMESH_LIPS]
cv.fillPoly(
mask, [np.array(lips_coords, dtype=np.int32)], 255
)
# Create the image
mask_image = Image.fromarray(mask)
# convert PIL image to tensor image
tensor_mask = mask_image.convert("RGB")
tensor_mask = np.array(tensor_mask).astype(np.float32) / 255.0
tensor_mask = torch.from_numpy(tensor_mask)[None,]
tensor_mask = tensor_mask.squeeze(3)[..., 0]
res_masks.append(tensor_mask)
return (torch.cat(res_masks, dim=0),)