|
from transformers import pipeline |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
|
|
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes") |
|
|
|
|
|
def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]): |
|
|
|
segments = segmenter(img) |
|
|
|
|
|
mask_list = [] |
|
for s in segments: |
|
if(s['label'] in clothes): |
|
mask_list.append(s['mask']) |
|
|
|
|
|
|
|
final_mask = np.array(mask_list[0]) |
|
for mask in mask_list: |
|
current_mask = np.array(mask) |
|
final_mask = final_mask + current_mask |
|
|
|
|
|
final_mask = Image.fromarray(final_mask) |
|
|
|
|
|
img.putalpha(final_mask) |
|
|
|
return img |