Spaces:
Runtime error
Runtime error
Update SegCloth.py
Browse files- SegCloth.py +22 -5
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
|
@@ -12,6 +12,22 @@ def encode_image_to_base64(image):
|
|
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"], margin=10):
|
16 |
# Segmentation de l'image
|
17 |
segments = segmenter(img)
|
@@ -49,9 +65,10 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
|
|
49 |
# Recadrer l'image à la taille du masque avec la marge
|
50 |
cropped_image = empty_image.crop((left, top, right, bottom))
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
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
|
|
|
12 |
image.save(buffered, format="PNG")
|
13 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
14 |
|
15 |
+
def is_image_significant(image, threshold=0.01):
|
16 |
+
"""
|
17 |
+
Vérifie si une image contient suffisamment de contenu non transparent.
|
18 |
+
:param image: L'image PIL à vérifier.
|
19 |
+
:param threshold: Seuil de pourcentage de pixels non transparents pour considérer l'image comme significative.
|
20 |
+
:return: True si l'image est significative, sinon False.
|
21 |
+
"""
|
22 |
+
np_image = np.array(image)
|
23 |
+
# Compte le nombre de pixels non transparents
|
24 |
+
non_transparent_pixels = np.sum(np_image[:, :, 3] > 0)
|
25 |
+
# Calcul du pourcentage de pixels non transparents
|
26 |
+
total_pixels = np_image.shape[0] * np_image.shape[1]
|
27 |
+
non_transparent_percentage = non_transparent_pixels / total_pixels
|
28 |
+
|
29 |
+
return non_transparent_percentage > threshold
|
30 |
+
|
31 |
def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"], margin=10):
|
32 |
# Segmentation de l'image
|
33 |
segments = segmenter(img)
|
|
|
65 |
# Recadrer l'image à la taille du masque avec la marge
|
66 |
cropped_image = empty_image.crop((left, top, right, bottom))
|
67 |
|
68 |
+
# Vérifier si l'image recadrée est significative
|
69 |
+
if is_image_significant(cropped_image):
|
70 |
+
# Encodage de l'image recadrée en base64
|
71 |
+
imageBase64 = encode_image_to_base64(cropped_image)
|
72 |
+
result_images.append(imageBase64)
|
73 |
|
74 |
return result_images
|