File size: 1,469 Bytes
8f2a481
 
 
871269e
8f2a481
 
 
 
 
 
1fceef8
 
 
 
 
 
8f2a481
 
 
 
 
 
 
 
 
 
 
ba06fe5
 
8f2a481
 
ba06fe5
 
8f2a481
ba06fe5
 
8f2a481
 
1fceef8
8f2a481
 
 
 
 
 
 
 
 
ba06fe5
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
from transformers import pipeline
from PIL import Image
import numpy as np
from io import BytesIO
import io
import base64

# Initialize segmentation pipeline
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")


def encode_image_to_base64(image):
    buffered = BytesIO()
    image.save(buffered, format="PNG")
    return base64.b64encode(buffered.getvalue()).decode('utf-8')


def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
    # Segment image
    segments = segmenter(img)

    # List to hold the results
    results = []

    # Process each segment
    for s in segments:
        if s['label'] in clothes:
            # Create a blank image with the same size as the original
            clothing_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
            
            # Apply the mask to the new image
            mask = np.array(s['mask'])
            mask_image = Image.fromarray(mask * 255)  # Convert mask to 255 range for alpha channel
            
            # Paste mask onto the blank image
            clothing_image.paste(img, mask=mask_image)
            
            # Convert image to base64
            image_base64 = encode_image_to_base64(clothing_image)
            
            # Add to results list
            results.append({
                "type": s['label'],
                "image_base64": image_base64
            })

    return results