Spaces:
Runtime error
Runtime error
Update SegCloth.py
Browse files- SegCloth.py +22 -15
SegCloth.py
CHANGED
@@ -13,26 +13,33 @@ def encode_image_to_base64(image):
|
|
13 |
image.save(buffered, format="PNG")
|
14 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
15 |
|
16 |
-
def segment_clothing(img, clothes=
|
17 |
# Segment image
|
18 |
segments = segmenter(img)
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
for s in segments:
|
23 |
-
if(s['label'] in clothes):
|
24 |
-
mask_list.append(s['mask'])
|
25 |
|
|
|
26 |
result_images = []
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
|
38 |
|
|
|
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", "Scarf"]):
|
17 |
# Segment image
|
18 |
segments = segmenter(img)
|
19 |
|
20 |
+
# Convert image to RGBA
|
21 |
+
img = img.convert("RGBA")
|
|
|
|
|
|
|
22 |
|
23 |
+
# Create list of masks
|
24 |
result_images = []
|
25 |
+
for s in segments:
|
26 |
+
if s['label'] in clothes:
|
27 |
+
# Extract mask and resize image to mask size
|
28 |
+
current_mask = np.array(s['mask'])
|
29 |
+
mask_size = current_mask.shape[::-1] # Mask size is (width, height)
|
30 |
+
|
31 |
+
# Resize the original image to match the mask size
|
32 |
+
resized_img = img.resize(mask_size)
|
33 |
+
|
34 |
+
# Apply mask to resized image
|
35 |
+
final_mask = Image.fromarray(current_mask)
|
36 |
+
resized_img.putalpha(final_mask)
|
37 |
+
|
38 |
+
# Convert the final image to base64
|
39 |
+
imageBase64 = encode_image_to_base64(resized_img)
|
40 |
+
result_images.append((s['label'], imageBase64))
|
41 |
+
|
42 |
+
return result_images
|
43 |
|
44 |
|
45 |
|