Spaces:
Runtime error
Runtime error
Update SegCloth.py
Browse files- SegCloth.py +14 -13
SegCloth.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from transformers import pipeline
|
2 |
-
from PIL import Image, ImageChops
|
3 |
import numpy as np
|
4 |
from io import BytesIO
|
5 |
import base64
|
@@ -8,7 +8,7 @@ from transparent_background import Remover
|
|
8 |
# Initialisation du pipeline de segmentation
|
9 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
10 |
|
11 |
-
|
12 |
def remove_background(image):
|
13 |
remover = Remover()
|
14 |
if isinstance(image, Image.Image):
|
@@ -19,7 +19,8 @@ def remove_background(image):
|
|
19 |
else:
|
20 |
raise TypeError("Unsupported image type")
|
21 |
return output
|
22 |
-
|
|
|
23 |
def encode_image_to_base64(image):
|
24 |
buffered = BytesIO()
|
25 |
image.save(buffered, format="PNG")
|
@@ -34,17 +35,16 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
|
|
34 |
|
35 |
for s in segments:
|
36 |
if s['label'] in clothes:
|
37 |
-
# Conversion du masque en tableau NumPy
|
38 |
-
mask_array = np.array(s['mask'])
|
39 |
-
|
40 |
# Création d'une image vide avec transparence
|
41 |
empty_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
42 |
|
43 |
-
# Conversion du masque en
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
|
49 |
# Application du masque sur l'image vide
|
50 |
empty_image.paste(segmented_part, mask=mask_image)
|
@@ -62,10 +62,11 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
|
|
62 |
# Recadrer l'image à la taille du masque avec la marge
|
63 |
cropped_image = empty_image.crop((left, top, right, bottom))
|
64 |
|
65 |
-
#
|
66 |
image_rm_background = remove_background(cropped_image)
|
|
|
|
|
67 |
imageBase64 = encode_image_to_base64(image_rm_background)
|
68 |
-
#result_images.append((s['label'], imageBase64))
|
69 |
result_images.append(imageBase64)
|
70 |
|
71 |
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
|
|
|
8 |
# Initialisation du pipeline de segmentation
|
9 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
10 |
|
11 |
+
# Fonction pour supprimer l'arrière-plan
|
12 |
def remove_background(image):
|
13 |
remover = Remover()
|
14 |
if isinstance(image, Image.Image):
|
|
|
19 |
else:
|
20 |
raise TypeError("Unsupported image type")
|
21 |
return output
|
22 |
+
|
23 |
+
# Fonction pour encoder une image en base64
|
24 |
def encode_image_to_base64(image):
|
25 |
buffered = BytesIO()
|
26 |
image.save(buffered, format="PNG")
|
|
|
35 |
|
36 |
for s in segments:
|
37 |
if s['label'] in clothes:
|
|
|
|
|
|
|
38 |
# Création d'une image vide avec transparence
|
39 |
empty_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
40 |
|
41 |
+
# Conversion du masque en tableau NumPy et ajustement de l'image
|
42 |
+
mask_array = np.array(s['mask'])
|
43 |
+
mask_image = Image.fromarray(mask_array).convert("L") # Convertir le masque en niveau de gris
|
44 |
+
|
45 |
+
# Appliquer le masque à l'image d'origine (en RGBA)
|
46 |
+
mask_rgba = Image.merge("RGBA", [mask_image, mask_image, mask_image, mask_image])
|
47 |
+
segmented_part = ImageChops.multiply(img.convert("RGBA"), mask_rgba)
|
48 |
|
49 |
# Application du masque sur l'image vide
|
50 |
empty_image.paste(segmented_part, mask=mask_image)
|
|
|
62 |
# Recadrer l'image à la taille du masque avec la marge
|
63 |
cropped_image = empty_image.crop((left, top, right, bottom))
|
64 |
|
65 |
+
# Supprimer l'arrière-plan
|
66 |
image_rm_background = remove_background(cropped_image)
|
67 |
+
|
68 |
+
# Encodage de l'image recadrée en base64
|
69 |
imageBase64 = encode_image_to_base64(image_rm_background)
|
|
|
70 |
result_images.append(imageBase64)
|
71 |
|
72 |
return result_images
|