Spaces:
Runtime error
Runtime error
Update SegCloth.py
Browse files- SegCloth.py +19 -25
SegCloth.py
CHANGED
@@ -4,39 +4,33 @@ import numpy as np
|
|
4 |
from io import BytesIO
|
5 |
import io
|
6 |
import base64
|
|
|
7 |
# Initialize segmentation pipeline
|
8 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
9 |
|
10 |
|
11 |
-
def
|
12 |
-
buffered = BytesIO()
|
13 |
-
image.save(buffered, format="PNG")
|
14 |
-
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
15 |
-
|
16 |
-
def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
|
17 |
# Segment image
|
18 |
segments = segmenter(img)
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
# Create individual masks for each clothing type
|
24 |
for s in segments:
|
25 |
-
if
|
26 |
-
|
27 |
-
|
28 |
-
masks[s['label']] = mask
|
29 |
-
else:
|
30 |
-
masks[s['label']] += mask
|
31 |
-
|
32 |
-
# Create and save images for each clothing type
|
33 |
result_images = []
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
return result_images
|
|
|
4 |
from io import BytesIO
|
5 |
import io
|
6 |
import base64
|
7 |
+
|
8 |
# Initialize segmentation pipeline
|
9 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
10 |
|
11 |
|
12 |
+
def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
|
|
|
|
|
|
|
|
|
|
|
13 |
# Segment image
|
14 |
segments = segmenter(img)
|
15 |
|
16 |
+
# Create list of masks
|
17 |
+
mask_list = []
|
|
|
|
|
18 |
for s in segments:
|
19 |
+
if(s['label'] in clothes):
|
20 |
+
mask_list.append(s['mask'])
|
21 |
+
|
|
|
|
|
|
|
|
|
|
|
22 |
result_images = []
|
23 |
+
|
24 |
+
# Paste all masks on top of eachother
|
25 |
+
final_mask = np.array(mask_list[0])
|
26 |
+
for mask in mask_list:
|
27 |
+
current_mask = np.array(mask)
|
28 |
+
final_mask_bis = Image.fromarray(current_mask)
|
29 |
+
img.putalpha(final_mask_bis)
|
30 |
+
imageBase64 = encode_image_to_base64(img)
|
31 |
+
result_images.append(('clothing_type', imageBase64))
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
|
36 |
return result_images
|