Spaces:
Runtime error
Runtime error
Update SegCloth.py
Browse files- SegCloth.py +24 -16
SegCloth.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
from transformers import pipeline
|
2 |
-
from PIL import Image
|
3 |
import numpy as np
|
4 |
from io import BytesIO
|
5 |
import base64
|
6 |
|
7 |
-
#
|
8 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
9 |
|
10 |
def encode_image_to_base64(image):
|
@@ -13,23 +13,31 @@ def encode_image_to_base64(image):
|
|
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 |
-
#
|
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 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
result_images.append((clothing_type, imageBase64))
|
34 |
|
35 |
-
return result_images
|
|
|
1 |
from transformers import pipeline
|
2 |
+
from PIL import Image, ImageChops
|
3 |
import numpy as np
|
4 |
from io import BytesIO
|
5 |
import base64
|
6 |
|
7 |
+
# Initialisation du pipeline de segmentation
|
8 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
9 |
|
10 |
def encode_image_to_base64(image):
|
|
|
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 |
+
# Segmentation de l'image
|
17 |
segments = segmenter(img)
|
18 |
+
|
19 |
+
# Liste des images segmentées
|
20 |
+
result_images = []
|
21 |
|
|
|
|
|
22 |
for s in segments:
|
23 |
if s['label'] in clothes:
|
24 |
+
# Conversion du masque en tableau NumPy
|
25 |
+
mask_array = np.array(s['mask'])
|
26 |
+
|
27 |
+
# Création d'une image vide avec transparence
|
28 |
+
empty_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
29 |
+
|
30 |
+
# Conversion du masque en image PIL (niveau de gris)
|
31 |
+
mask_image = Image.fromarray(mask_array).convert("L")
|
32 |
+
|
33 |
+
# Extraction de la partie de l'image correspondant au masque
|
34 |
+
segmented_part = ImageChops.multiply(img.convert("RGBA"), Image.merge("RGBA", [mask_image, mask_image, mask_image, mask_image]))
|
35 |
|
36 |
+
# Application du masque sur l'image vide
|
37 |
+
empty_image.paste(segmented_part, mask=mask_image)
|
38 |
+
|
39 |
+
# Encodage de l'image résultante en base64
|
40 |
+
imageBase64 = encode_image_to_base64(empty_image)
|
41 |
+
result_images.append((s['label'], imageBase64))
|
|
|
42 |
|
43 |
+
return result_images
|