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