Spaces:
Running
Running
Update SegCloth.py
Browse files- SegCloth.py +33 -2
SegCloth.py
CHANGED
@@ -1,3 +1,34 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
|
7 |
+
# Initialize segmentation pipeline
|
8 |
+
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes", device="cuda")
|
9 |
+
|
10 |
+
@spaces.GPU(enable_queue=True)
|
11 |
+
def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
|
12 |
+
# Segment image
|
13 |
+
segments = segmenter(img)
|
14 |
+
|
15 |
+
# Create list of masks
|
16 |
+
mask_list = []
|
17 |
+
for s in segments:
|
18 |
+
if(s['label'] in clothes):
|
19 |
+
mask_list.append(s['mask'])
|
20 |
+
|
21 |
+
|
22 |
+
# Paste all masks on top of eachother
|
23 |
+
final_mask = np.array(mask_list[0])
|
24 |
+
for mask in mask_list:
|
25 |
+
current_mask = np.array(mask)
|
26 |
+
final_mask = final_mask + current_mask
|
27 |
+
|
28 |
+
# Convert final mask from np array to PIL image
|
29 |
+
final_mask = Image.fromarray(final_mask)
|
30 |
+
|
31 |
+
# Apply mask to original image
|
32 |
+
img.putalpha(final_mask)
|
33 |
+
|
34 |
+
return img
|