Spaces:
Runtime error
Runtime error
Update model/cloth_masker.py
Browse files- model/cloth_masker.py +40 -37
model/cloth_masker.py
CHANGED
@@ -84,6 +84,8 @@ MASK_CLOTH_PARTS = {
|
|
84 |
'overall': ['Upper-clothes', 'Dress', 'Pants', 'Skirt', 'Coat', 'Jumpsuits'],
|
85 |
'inner': ['Upper-clothes'],
|
86 |
'outer': ['Coat',]
|
|
|
|
|
87 |
}
|
88 |
MASK_DENSE_PARTS = {
|
89 |
'upper': ['torso', 'big arms', 'forearms'],
|
@@ -91,6 +93,8 @@ MASK_DENSE_PARTS = {
|
|
91 |
'overall': ['torso', 'thighs', 'legs', 'big arms', 'forearms'],
|
92 |
'inner': ['torso'],
|
93 |
'outer': ['torso', 'big arms', 'forearms']
|
|
|
|
|
94 |
}
|
95 |
|
96 |
schp_public_protect_parts = ['Hat', 'Hair', 'Sunglasses', 'Left-shoe', 'Right-shoe', 'Bag', 'Glove', 'Scarf']
|
@@ -186,45 +190,44 @@ class AutoMasker:
|
|
186 |
|
187 |
@staticmethod
|
188 |
def cloth_agnostic_mask(
|
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 |
-
# Weak Protect Area (Hair, Irrelevant Clothes, Body Parts)
|
220 |
-
body_protect_area = part_mask_of(PROTECT_BODY_PARTS[part], schp_lip_mask, LIP_MAPPING) | part_mask_of(PROTECT_BODY_PARTS[part], schp_atr_mask, ATR_MAPPING)
|
221 |
-
hair_protect_area = part_mask_of(['Hair'], schp_lip_mask, LIP_MAPPING) | \
|
222 |
-
part_mask_of(['Hair'], schp_atr_mask, ATR_MAPPING)
|
223 |
-
cloth_protect_area = part_mask_of(PROTECT_CLOTH_PARTS[part]['LIP'], schp_lip_mask, LIP_MAPPING) | \
|
224 |
-
part_mask_of(PROTECT_CLOTH_PARTS[part]['ATR'], schp_atr_mask, ATR_MAPPING)
|
225 |
-
accessory_protect_area = part_mask_of((accessory_parts := ['Hat', 'Glove', 'Sunglasses', 'Bag', 'Left-shoe', 'Right-shoe', 'Scarf', 'Socks']), schp_lip_mask, LIP_MAPPING) | \
|
226 |
-
part_mask_of(accessory_parts, schp_atr_mask, ATR_MAPPING)
|
227 |
-
weak_protect_area = body_protect_area | cloth_protect_area | hair_protect_area | strong_protect_area | accessory_protect_area
|
228 |
|
229 |
# Mask Area
|
230 |
strong_mask_area = part_mask_of(MASK_CLOTH_PARTS[part], schp_lip_mask, LIP_MAPPING) | \
|
|
|
84 |
'overall': ['Upper-clothes', 'Dress', 'Pants', 'Skirt', 'Coat', 'Jumpsuits'],
|
85 |
'inner': ['Upper-clothes'],
|
86 |
'outer': ['Coat',]
|
87 |
+
'bags': ['Bag'], # New category for bags
|
88 |
+
'footwear': ['Left-shoe', 'Right-shoe'], # New category for footwear
|
89 |
}
|
90 |
MASK_DENSE_PARTS = {
|
91 |
'upper': ['torso', 'big arms', 'forearms'],
|
|
|
93 |
'overall': ['torso', 'thighs', 'legs', 'big arms', 'forearms'],
|
94 |
'inner': ['torso'],
|
95 |
'outer': ['torso', 'big arms', 'forearms']
|
96 |
+
'bags': [], # No dense parts for bags
|
97 |
+
'footwear': ['left foot', 'right foot'], # New category for footwear
|
98 |
}
|
99 |
|
100 |
schp_public_protect_parts = ['Hat', 'Hair', 'Sunglasses', 'Left-shoe', 'Right-shoe', 'Bag', 'Glove', 'Scarf']
|
|
|
190 |
|
191 |
@staticmethod
|
192 |
def cloth_agnostic_mask(
|
193 |
+
densepose_mask: Image.Image,
|
194 |
+
schp_lip_mask: Image.Image,
|
195 |
+
schp_atr_mask: Image.Image,
|
196 |
+
part: str='overall',
|
197 |
+
**kwargs
|
198 |
+
):
|
199 |
+
assert part in ['upper', 'lower', 'bags', 'footwear', 'inner', 'outer'], f"part should be one of ['upper', 'lower', 'bags', 'footwear', 'inner', 'outer'], but got {part}"
|
200 |
+
w, h = densepose_mask.size
|
201 |
+
|
202 |
+
dilate_kernel = max(w, h) // 250
|
203 |
+
dilate_kernel = dilate_kernel if dilate_kernel % 2 == 1 else dilate_kernel + 1
|
204 |
+
dilate_kernel = np.ones((dilate_kernel, dilate_kernel), np.uint8)
|
205 |
+
|
206 |
+
kernal_size = max(w, h) // 15
|
207 |
+
kernal_size = kernal_size if kernal_size % 2 == 1 else kernal_size + 1
|
208 |
+
|
209 |
+
densepose_mask = np.array(densepose_mask)
|
210 |
+
schp_lip_mask = np.array(schp_lip_mask)
|
211 |
+
schp_atr_mask = np.array(schp_atr_mask)
|
212 |
+
|
213 |
+
# Strong Protect Area (Hands, Face, Accessory, Feet)
|
214 |
+
hands_protect_area = part_mask_of(['hands', 'feet'], densepose_mask, DENSE_INDEX_MAP)
|
215 |
+
hands_protect_area = cv2.dilate(hands_protect_area, dilate_kernel, iterations=1)
|
216 |
+
hands_protect_area = hands_protect_area & \
|
217 |
+
(part_mask_of(['Left-arm', 'Right-arm', 'Left-leg', 'Right-leg'], schp_atr_mask, ATR_MAPPING) | \
|
218 |
+
part_mask_of(['Left-arm', 'Right-arm', 'Left-leg', 'Right-leg'], schp_lip_mask, LIP_MAPPING))
|
219 |
+
face_protect_area = part_mask_of('Face', schp_lip_mask, LIP_MAPPING)
|
220 |
+
|
221 |
+
strong_protect_area = hands_protect_area | face_protect_area
|
222 |
|
223 |
+
# Weak Protect Area (Hair, Irrelevant Clothes, Body Parts)
|
224 |
+
body_protect_area = part_mask_of(PROTECT_BODY_PARTS[part], schp_lip_mask, LIP_MAPPING) | part_mask_of(PROTECT_BODY_PARTS[part], schp_atr_mask, ATR_MAPPING)
|
225 |
+
hair_protect_area = part_mask_of(['Hair'], schp_lip_mask, LIP_MAPPING) | \
|
226 |
+
part_mask_of(['Hair'], schp_atr_mask, ATR_MAPPING)
|
227 |
+
cloth_protect_area = part_mask_of(PROTECT_CLOTH_PARTS[part]['LIP'], schp_lip_mask, LIP_MAPPING) | \
|
228 |
+
part_mask_of(PROTECT_CLOTH_PARTS[part]['ATR'], schp_atr_mask, ATR_MAPPING)
|
229 |
+
accessory_protect_area = part_mask_of((accessory_parts := ['Hat', 'Glove', 'Sunglasses
|
230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
# Mask Area
|
233 |
strong_mask_area = part_mask_of(MASK_CLOTH_PARTS[part], schp_lip_mask, LIP_MAPPING) | \
|