Spaces:
Running
Running
Update landmarkdiff/masking.py to v0.3.2
Browse files- landmarkdiff/masking.py +19 -156
landmarkdiff/masking.py
CHANGED
|
@@ -21,187 +21,55 @@ if TYPE_CHECKING:
|
|
| 21 |
MASK_CONFIG: dict[str, dict] = {
|
| 22 |
"rhinoplasty": {
|
| 23 |
"landmark_indices": [
|
| 24 |
-
1,
|
| 25 |
-
|
| 26 |
-
4,
|
| 27 |
-
5,
|
| 28 |
-
6,
|
| 29 |
-
19,
|
| 30 |
-
94,
|
| 31 |
-
141,
|
| 32 |
-
168,
|
| 33 |
-
195,
|
| 34 |
-
197,
|
| 35 |
-
236,
|
| 36 |
-
240,
|
| 37 |
-
274,
|
| 38 |
-
275,
|
| 39 |
-
278,
|
| 40 |
-
279,
|
| 41 |
-
294,
|
| 42 |
-
326,
|
| 43 |
-
327,
|
| 44 |
-
360,
|
| 45 |
-
363,
|
| 46 |
-
370,
|
| 47 |
-
456,
|
| 48 |
-
460,
|
| 49 |
],
|
| 50 |
"dilation_px": 30,
|
| 51 |
"feather_sigma": 15.0,
|
| 52 |
},
|
| 53 |
"blepharoplasty": {
|
| 54 |
"landmark_indices": [
|
| 55 |
-
33,
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
144,
|
| 59 |
-
145,
|
| 60 |
-
153,
|
| 61 |
-
154,
|
| 62 |
-
155,
|
| 63 |
-
157,
|
| 64 |
-
158,
|
| 65 |
-
159,
|
| 66 |
-
160,
|
| 67 |
-
161,
|
| 68 |
-
246,
|
| 69 |
-
362,
|
| 70 |
-
382,
|
| 71 |
-
381,
|
| 72 |
-
380,
|
| 73 |
-
374,
|
| 74 |
-
373,
|
| 75 |
-
390,
|
| 76 |
-
249,
|
| 77 |
-
263,
|
| 78 |
-
466,
|
| 79 |
-
388,
|
| 80 |
-
387,
|
| 81 |
-
386,
|
| 82 |
-
385,
|
| 83 |
-
384,
|
| 84 |
-
398,
|
| 85 |
],
|
| 86 |
"dilation_px": 15,
|
| 87 |
"feather_sigma": 10.0,
|
| 88 |
},
|
| 89 |
"rhytidectomy": {
|
| 90 |
"landmark_indices": [
|
| 91 |
-
10,
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
58,
|
| 95 |
-
67,
|
| 96 |
-
93,
|
| 97 |
-
103,
|
| 98 |
-
109,
|
| 99 |
-
127,
|
| 100 |
-
132,
|
| 101 |
-
136,
|
| 102 |
-
150,
|
| 103 |
-
162,
|
| 104 |
-
172,
|
| 105 |
-
176,
|
| 106 |
-
187,
|
| 107 |
-
207,
|
| 108 |
-
213,
|
| 109 |
-
234,
|
| 110 |
-
284,
|
| 111 |
-
297,
|
| 112 |
-
323,
|
| 113 |
-
332,
|
| 114 |
-
338,
|
| 115 |
-
356,
|
| 116 |
-
361,
|
| 117 |
-
365,
|
| 118 |
-
379,
|
| 119 |
-
389,
|
| 120 |
-
397,
|
| 121 |
-
400,
|
| 122 |
-
427,
|
| 123 |
-
454,
|
| 124 |
],
|
| 125 |
"dilation_px": 40,
|
| 126 |
"feather_sigma": 20.0,
|
| 127 |
},
|
| 128 |
"orthognathic": {
|
| 129 |
"landmark_indices": [
|
| 130 |
-
0,
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
36,
|
| 134 |
-
37,
|
| 135 |
-
39,
|
| 136 |
-
40,
|
| 137 |
-
57,
|
| 138 |
-
61,
|
| 139 |
-
78,
|
| 140 |
-
80,
|
| 141 |
-
81,
|
| 142 |
-
82,
|
| 143 |
-
84,
|
| 144 |
-
87,
|
| 145 |
-
88,
|
| 146 |
-
91,
|
| 147 |
-
95,
|
| 148 |
-
146,
|
| 149 |
-
167,
|
| 150 |
-
169,
|
| 151 |
-
170,
|
| 152 |
-
175,
|
| 153 |
-
181,
|
| 154 |
-
191,
|
| 155 |
-
200,
|
| 156 |
-
201,
|
| 157 |
-
202,
|
| 158 |
-
204,
|
| 159 |
-
208,
|
| 160 |
-
211,
|
| 161 |
-
212,
|
| 162 |
-
214,
|
| 163 |
],
|
| 164 |
"dilation_px": 35,
|
| 165 |
"feather_sigma": 18.0,
|
| 166 |
},
|
| 167 |
"brow_lift": {
|
| 168 |
"landmark_indices": [
|
| 169 |
-
70,
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
66,
|
| 173 |
-
107, # left brow
|
| 174 |
-
300,
|
| 175 |
-
293,
|
| 176 |
-
334,
|
| 177 |
-
296,
|
| 178 |
-
336, # right brow
|
| 179 |
-
9,
|
| 180 |
-
8,
|
| 181 |
-
10, # forehead midline
|
| 182 |
-
109,
|
| 183 |
-
67,
|
| 184 |
-
103, # upper face left
|
| 185 |
-
338,
|
| 186 |
-
297,
|
| 187 |
-
332, # upper face right
|
| 188 |
],
|
| 189 |
"dilation_px": 25,
|
| 190 |
"feather_sigma": 15.0,
|
| 191 |
},
|
| 192 |
"mentoplasty": {
|
| 193 |
"landmark_indices": [
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
150,
|
| 197 |
-
152,
|
| 198 |
-
171,
|
| 199 |
-
175,
|
| 200 |
-
176,
|
| 201 |
-
377,
|
| 202 |
],
|
| 203 |
-
"dilation_px":
|
| 204 |
-
"feather_sigma":
|
| 205 |
},
|
| 206 |
}
|
| 207 |
|
|
@@ -284,19 +152,14 @@ def generate_surgical_mask(
|
|
| 284 |
# Vitiligo: reduce mask over depigmented patches to preserve them
|
| 285 |
if clinical_flags.vitiligo and image is not None:
|
| 286 |
from landmarkdiff.clinical import adjust_mask_for_vitiligo, detect_vitiligo_patches
|
| 287 |
-
|
| 288 |
patches = detect_vitiligo_patches(image, face)
|
| 289 |
mask = adjust_mask_for_vitiligo(mask, patches)
|
| 290 |
|
| 291 |
# Keloid: soften transitions in keloid-prone regions
|
| 292 |
if clinical_flags.keloid_prone and clinical_flags.keloid_regions:
|
| 293 |
from landmarkdiff.clinical import adjust_mask_for_keloid, get_keloid_exclusion_mask
|
| 294 |
-
|
| 295 |
keloid_mask = get_keloid_exclusion_mask(
|
| 296 |
-
face,
|
| 297 |
-
clinical_flags.keloid_regions,
|
| 298 |
-
w,
|
| 299 |
-
h,
|
| 300 |
)
|
| 301 |
mask = adjust_mask_for_keloid(mask, keloid_mask)
|
| 302 |
|
|
|
|
| 21 |
MASK_CONFIG: dict[str, dict] = {
|
| 22 |
"rhinoplasty": {
|
| 23 |
"landmark_indices": [
|
| 24 |
+
1, 2, 4, 5, 6, 19, 94, 141, 168, 195, 197, 236, 240,
|
| 25 |
+
274, 275, 278, 279, 294, 326, 327, 360, 363, 370, 456, 460,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
],
|
| 27 |
"dilation_px": 30,
|
| 28 |
"feather_sigma": 15.0,
|
| 29 |
},
|
| 30 |
"blepharoplasty": {
|
| 31 |
"landmark_indices": [
|
| 32 |
+
33, 7, 163, 144, 145, 153, 154, 155, 157, 158, 159, 160, 161, 246,
|
| 33 |
+
362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386,
|
| 34 |
+
385, 384, 398,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
],
|
| 36 |
"dilation_px": 15,
|
| 37 |
"feather_sigma": 10.0,
|
| 38 |
},
|
| 39 |
"rhytidectomy": {
|
| 40 |
"landmark_indices": [
|
| 41 |
+
10, 21, 54, 58, 67, 93, 103, 109, 127, 132, 136, 150, 162, 172,
|
| 42 |
+
176, 187, 207, 213, 234, 284, 297, 323, 332, 338, 356, 361, 365,
|
| 43 |
+
379, 389, 397, 400, 427, 454,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
],
|
| 45 |
"dilation_px": 40,
|
| 46 |
"feather_sigma": 20.0,
|
| 47 |
},
|
| 48 |
"orthognathic": {
|
| 49 |
"landmark_indices": [
|
| 50 |
+
0, 17, 18, 36, 37, 39, 40, 57, 61, 78, 80, 81, 82, 84, 87, 88,
|
| 51 |
+
91, 95, 146, 167, 169, 170, 175, 181, 191, 200, 201, 202, 204,
|
| 52 |
+
208, 211, 212, 214,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
],
|
| 54 |
"dilation_px": 35,
|
| 55 |
"feather_sigma": 18.0,
|
| 56 |
},
|
| 57 |
"brow_lift": {
|
| 58 |
"landmark_indices": [
|
| 59 |
+
10, 21, 46, 52, 53, 54, 55, 63, 65, 66, 67, 68, 69, 70, 71,
|
| 60 |
+
103, 104, 105, 107, 108, 109, 151, 282, 283, 284, 285, 293,
|
| 61 |
+
295, 296, 297, 298, 299, 300, 301, 332, 333, 334, 336, 337, 338,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
],
|
| 63 |
"dilation_px": 25,
|
| 64 |
"feather_sigma": 15.0,
|
| 65 |
},
|
| 66 |
"mentoplasty": {
|
| 67 |
"landmark_indices": [
|
| 68 |
+
0, 17, 18, 57, 83, 84, 85, 86, 87, 146, 167, 169, 170, 175,
|
| 69 |
+
181, 191, 199, 200, 201, 202, 204, 208, 211, 212, 214,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
],
|
| 71 |
+
"dilation_px": 30,
|
| 72 |
+
"feather_sigma": 16.0,
|
| 73 |
},
|
| 74 |
}
|
| 75 |
|
|
|
|
| 152 |
# Vitiligo: reduce mask over depigmented patches to preserve them
|
| 153 |
if clinical_flags.vitiligo and image is not None:
|
| 154 |
from landmarkdiff.clinical import adjust_mask_for_vitiligo, detect_vitiligo_patches
|
|
|
|
| 155 |
patches = detect_vitiligo_patches(image, face)
|
| 156 |
mask = adjust_mask_for_vitiligo(mask, patches)
|
| 157 |
|
| 158 |
# Keloid: soften transitions in keloid-prone regions
|
| 159 |
if clinical_flags.keloid_prone and clinical_flags.keloid_regions:
|
| 160 |
from landmarkdiff.clinical import adjust_mask_for_keloid, get_keloid_exclusion_mask
|
|
|
|
| 161 |
keloid_mask = get_keloid_exclusion_mask(
|
| 162 |
+
face, clinical_flags.keloid_regions, w, h,
|
|
|
|
|
|
|
|
|
|
| 163 |
)
|
| 164 |
mask = adjust_mask_for_keloid(mask, keloid_mask)
|
| 165 |
|