Spaces:
Runtime error
Runtime error
Update model/cloth_masker.py
Browse files- model/cloth_masker.py +44 -50
model/cloth_masker.py
CHANGED
@@ -80,10 +80,10 @@ PROTECT_CLOTH_PARTS = {
|
|
80 |
}
|
81 |
MASK_CLOTH_PARTS = {
|
82 |
'upper': ['Upper-clothes', 'Coat', 'Dress', 'Jumpsuits'],
|
83 |
-
'lower': ['Pants', 'Skirt', 'Dress', 'Jumpsuits'],
|
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 |
}
|
@@ -92,11 +92,11 @@ MASK_DENSE_PARTS = {
|
|
92 |
'lower': ['thighs', 'legs'],
|
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']
|
101 |
schp_protect_parts = {
|
102 |
'upper': ['Left-leg', 'Right-leg', 'Skirt', 'Pants', 'Jumpsuits'],
|
@@ -110,7 +110,7 @@ schp_mask_parts = {
|
|
110 |
'lower': ['Pants', 'Skirt', 'Dress', 'Jumpsuits', 'socks'],
|
111 |
'overall': ['Upper-clothes', 'Dress', 'Pants', 'Skirt', 'Coat', 'Jumpsuits', 'socks'],
|
112 |
'inner': ['Upper-clothes'],
|
113 |
-
'outer': ['Coat'
|
114 |
}
|
115 |
|
116 |
dense_mask_parts = {
|
@@ -154,7 +154,6 @@ def hull_mask(mask_area: np.ndarray):
|
|
154 |
hull = cv2.convexHull(c)
|
155 |
hull_mask = cv2.fillPoly(np.zeros_like(mask_area), [hull], 255) | hull_mask
|
156 |
return hull_mask
|
157 |
-
|
158 |
|
159 |
class AutoMasker:
|
160 |
def __init__(
|
@@ -168,7 +167,7 @@ class AutoMasker:
|
|
168 |
|
169 |
self.densepose_processor = DensePose(densepose_ckpt, device)
|
170 |
self.schp_processor_atr = SCHP(ckpt_path=os.path.join(schp_ckpt, 'exp-schp-201908301523-atr.pth'), device=device)
|
171 |
-
self.schp_processor_lip = SCHP(ckpt_path=os.path.join(schp_ckpt, 'exp-schp-201908261155-lip.pth'), device=device)
|
172 |
|
173 |
self.mask_processor = VaeImageProcessor(vae_scale_factor=8, do_normalize=False, do_binarize=True, do_convert_grayscale=True)
|
174 |
|
@@ -190,45 +189,42 @@ class AutoMasker:
|
|
190 |
|
191 |
@staticmethod
|
192 |
def cloth_agnostic_mask(
|
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 |
-
strong_protect_area = hands_protect_area | face_protect_area
|
222 |
|
223 |
-
|
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 |
-
part_mask_of(accessory_parts, schp_atr_mask, ATR_MAPPING)
|
231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
# Mask Area
|
234 |
strong_mask_area = part_mask_of(MASK_CLOTH_PARTS[part], schp_lip_mask, LIP_MAPPING) | \
|
@@ -237,13 +233,12 @@ class AutoMasker:
|
|
237 |
mask_dense_area = part_mask_of(MASK_DENSE_PARTS[part], densepose_mask, DENSE_INDEX_MAP)
|
238 |
mask_dense_area = cv2.resize(mask_dense_area.astype(np.uint8), None, fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
|
239 |
mask_dense_area = cv2.dilate(mask_dense_area, dilate_kernel, iterations=2)
|
240 |
-
mask_dense_area = cv2.resize(mask_dense_area.astype(np.uint8), None, fx=4, fy=4, interpolation=cv2.INTER_NEAREST)
|
241 |
|
242 |
-
|
243 |
-
mask_area = (np.ones_like(densepose_mask) & (~weak_protect_area) & (~background_area)) | mask_dense_area
|
244 |
|
245 |
mask_area = hull_mask(mask_area * 255) // 255 # Convex Hull to expand the mask area
|
246 |
-
mask_area = mask_area & (~
|
247 |
mask_area = cv2.GaussianBlur(mask_area * 255, (kernal_size, kernal_size), 0)
|
248 |
mask_area[mask_area < 25] = 0
|
249 |
mask_area[mask_area >= 25] = 1
|
@@ -272,6 +267,5 @@ class AutoMasker:
|
|
272 |
'schp_atr': preprocess_results['schp_atr']
|
273 |
}
|
274 |
|
275 |
-
|
276 |
if __name__ == '__main__':
|
277 |
-
pass
|
|
|
80 |
}
|
81 |
MASK_CLOTH_PARTS = {
|
82 |
'upper': ['Upper-clothes', 'Coat', 'Dress', 'Jumpsuits'],
|
83 |
+
'lower': ['Pants', 'Skirt', 'Dress', ' Jumpsuits'],
|
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 |
}
|
|
|
92 |
'lower': ['thighs', 'legs'],
|
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']
|
101 |
schp_protect_parts = {
|
102 |
'upper': ['Left-leg', 'Right-leg', 'Skirt', 'Pants', 'Jumpsuits'],
|
|
|
110 |
'lower': ['Pants', 'Skirt', 'Dress', 'Jumpsuits', 'socks'],
|
111 |
'overall': ['Upper-clothes', 'Dress', 'Pants', 'Skirt', 'Coat', 'Jumpsuits', 'socks'],
|
112 |
'inner': ['Upper-clothes'],
|
113 |
+
'outer': ['Coat']
|
114 |
}
|
115 |
|
116 |
dense_mask_parts = {
|
|
|
154 |
hull = cv2.convexHull(c)
|
155 |
hull_mask = cv2.fillPoly(np.zeros_like(mask_area), [hull], 255) | hull_mask
|
156 |
return hull_mask
|
|
|
157 |
|
158 |
class AutoMasker:
|
159 |
def __init__(
|
|
|
167 |
|
168 |
self.densepose_processor = DensePose(densepose_ckpt, device)
|
169 |
self.schp_processor_atr = SCHP(ckpt_path=os.path.join(schp_ckpt, 'exp-schp-201908301523-atr.pth'), device=device)
|
170 |
+
self.schp_processor_lip = SCHP(ckpt_path =os.path.join(schp_ckpt, 'exp-schp-201908261155-lip.pth'), device=device)
|
171 |
|
172 |
self.mask_processor = VaeImageProcessor(vae_scale_factor=8, do_normalize=False, do_binarize=True, do_convert_grayscale=True)
|
173 |
|
|
|
189 |
|
190 |
@staticmethod
|
191 |
def cloth_agnostic_mask(
|
192 |
+
densepose_mask: Image.Image,
|
193 |
+
schp_lip_mask: Image.Image,
|
194 |
+
schp_atr_mask: Image.Image,
|
195 |
+
part: str='overall',
|
196 |
+
**kwargs
|
197 |
+
):
|
198 |
+
assert part in ['upper', 'lower', 'bags', 'footwear', 'inner', 'outer'], f"part should be one of ['upper', 'lower', 'bags', 'footwear', 'inner', 'outer'], but got {part}"
|
199 |
+
w, h = densepose_mask.size
|
200 |
+
|
201 |
+
dilate_kernel = max(w, h) // 250
|
202 |
+
dilate_kernel = dilate_kernel if dilate_kernel % 2 == 1 else dilate_kernel + 1
|
203 |
+
dilate_kernel = np.ones((dilate_kernel, dilate_kernel), np.uint8)
|
204 |
+
|
205 |
+
kernal_size = max(w, h) // 15
|
206 |
+
kernal_size = kernal_size if kernal_size % 2 == 1 else kernal_size + 1
|
207 |
+
|
208 |
+
densepose_mask = np.array(densepose_mask)
|
209 |
+
schp_lip_mask = np.array(schp_lip_mask)
|
210 |
+
schp_atr_mask = np.array(schp_atr_mask)
|
211 |
+
|
212 |
+
# Strong Protect Area (Hands, Face, Accessory, Feet)
|
213 |
+
hands_protect_area = part_mask_of(['hands', 'feet'], densepose_mask, DENSE_INDEX_MAP)
|
214 |
+
hands_protect_area = cv2.dilate(hands_protect_area, dilate_kernel, iterations=1)
|
215 |
+
hands_protect_area = hands_protect_area & \
|
216 |
+
(part_mask_of(['Left-arm', 'Right-arm', 'Left-leg', 'Right-leg'], schp_atr_mask, ATR_MAPPING) | \
|
217 |
+
part_mask_of(['Left-arm', 'Right-arm', 'Left-leg', 'Right-leg'], schp_lip_mask, LIP_MAPPING))
|
218 |
+
face_protect_area = part_mask_of('Face', schp_lip_mask, LIP_MAPPING)
|
|
|
|
|
219 |
|
220 |
+
strong_protect_area = hands_protect_area | face_protect_area
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
+
# Weak Protect Area (Hair, Irrelevant Clothes, Body Parts)
|
223 |
+
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)
|
224 |
+
hair_protect_area = part_mask_of(['Hair'], schp_lip_mask, LIP_MAPPING) | \
|
225 |
+
part_mask_of(['Hair'], schp_atr_mask, ATR_MAPPING)
|
226 |
+
cloth_protect_area = part_mask_of(PROTECT_CLOTH_PARTS[part]['LIP'], schp_lip_mask, LIP_MAPPING) | \
|
227 |
+
part_mask_of(PROTECT_CLOTH_PARTS[part]['ATR'], schp_atr_mask, ATR_MAPPING)
|
228 |
|
229 |
# Mask Area
|
230 |
strong_mask_area = part_mask_of(MASK_CLOTH_PARTS[part], schp_lip_mask, LIP_MAPPING) | \
|
|
|
233 |
mask_dense_area = part_mask_of(MASK_DENSE_PARTS[part], densepose_mask, DENSE_INDEX_MAP)
|
234 |
mask_dense_area = cv2.resize(mask_dense_area.astype(np.uint8), None, fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
|
235 |
mask_dense_area = cv2.dilate(mask_dense_area, dilate_kernel, iterations=2)
|
236 |
+
mask_dense_area = cv2.resize(mask_dense_area .astype(np.uint8), None, fx=4, fy=4, interpolation=cv2.INTER_NEAREST)
|
237 |
|
238 |
+
mask_area = (np.ones_like(densepose_mask) & (~body_protect_area) & (~background_area)) | mask_dense_area
|
|
|
239 |
|
240 |
mask_area = hull_mask(mask_area * 255) // 255 # Convex Hull to expand the mask area
|
241 |
+
mask_area = mask_area & (~body_protect_area)
|
242 |
mask_area = cv2.GaussianBlur(mask_area * 255, (kernal_size, kernal_size), 0)
|
243 |
mask_area[mask_area < 25] = 0
|
244 |
mask_area[mask_area >= 25] = 1
|
|
|
267 |
'schp_atr': preprocess_results['schp_atr']
|
268 |
}
|
269 |
|
|
|
270 |
if __name__ == '__main__':
|
271 |
+
pass
|