File size: 2,898 Bytes
61a0586
b23b737
8f2a481
871269e
8f2a481
1c0cda0
c89d4d6
8f2a481
0bac47f
b23b737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb456a2
c89d4d6
8f2a481
c89d4d6
 
 
8f2a481
187d444
a05ee1f
c8c83ea
 
 
c89d4d6
 
 
c8c83ea
 
 
 
 
0bac47f
c89d4d6
 
 
ab47e20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80171a4
c11222a
eb456a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from transformers import pipeline
from PIL import Image, ImageChops
import numpy as np
from io import BytesIO
import base64

# Initialisation du pipeline de segmentation
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")

def is_image_significant(image, threshold=0.01):
    """
    Vérifie si une image contient suffisamment de contenu non transparent.
    :param image: L'image PIL à vérifier.
    :param threshold: Seuil de pourcentage de pixels non transparents pour considérer l'image comme significative.
    :return: True si l'image est significative, sinon False.
    """
    np_image = np.array(image)
    # Compte le nombre de pixels non transparents
    non_transparent_pixels = np.sum(np_image[:, :, 3] > 0)
    # Calcul du pourcentage de pixels non transparents
    total_pixels = np_image.shape[0] * np_image.shape[1]
    non_transparent_percentage = non_transparent_pixels / total_pixels
    
    return non_transparent_percentage > threshold

def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"], margin=10):
    # Segmentation de l'image
    segments = segmenter(img)
    
    # Liste des images segmentées
    result_images = []

    for s in segments:
        if s['label'] in clothes:
            # Conversion du masque en tableau NumPy
            mask_array = np.array(s['mask'])
            
            # Création d'une image vide avec transparence
            empty_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
            
            # Conversion du masque en image PIL (niveau de gris)
            mask_image = Image.fromarray(mask_array).convert("L")
            
            # Extraction de la partie de l'image correspondant au masque
            segmented_part = ImageChops.multiply(img.convert("RGBA"), Image.merge("RGBA", [mask_image, mask_image, mask_image, mask_image]))

            # Application du masque sur l'image vide
            empty_image.paste(segmented_part, mask=mask_image)
            
            # Vérifier si l'image est significative avant le recadrage
            if is_image_significant(empty_image):
                # Déterminer la bounding box du masque
                bbox = mask_image.getbbox()
                if bbox:
                    # Ajouter la marge autour de la bounding box
                    left, top, right, bottom = bbox
                    left = max(0, left - margin)
                    top = max(0, top - margin)
                    right = min(img.width, right + margin)
                    bottom = min(img.height, bottom + margin)
                    
                    # Recadrer l'image à la taille du masque avec la marge
                    cropped_image = empty_image.crop((left, top, right, bottom))
                    
                    result_images.append(cropped_image)

    return result_images