Spaces:
Running
Running
Update SegCloth.py
Browse files- SegCloth.py +21 -11
SegCloth.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
-
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
|
| 6 |
# Initialize segmentation pipeline
|
| 7 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
| 8 |
|
| 9 |
-
|
| 10 |
def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
|
| 11 |
# Segment image
|
| 12 |
segments = segmenter(img)
|
|
@@ -15,19 +14,30 @@ def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "D
|
|
| 15 |
mask_list = []
|
| 16 |
for s in segments:
|
| 17 |
if(s['label'] in clothes):
|
| 18 |
-
mask_list.append(s['mask'])
|
| 19 |
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
final_mask = np.array(mask_list[0])
|
| 23 |
for mask in mask_list:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Convert final mask from np array to PIL image
|
| 28 |
final_mask = Image.fromarray(final_mask)
|
| 29 |
|
| 30 |
-
# Apply mask to original image
|
|
|
|
| 31 |
img.putalpha(final_mask)
|
| 32 |
|
| 33 |
-
return img
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
from PIL import Image, ImageFilter
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2 # OpenCV for better mask processing
|
| 5 |
|
| 6 |
# Initialize segmentation pipeline
|
| 7 |
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
|
| 8 |
|
|
|
|
| 9 |
def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
|
| 10 |
# Segment image
|
| 11 |
segments = segmenter(img)
|
|
|
|
| 14 |
mask_list = []
|
| 15 |
for s in segments:
|
| 16 |
if(s['label'] in clothes):
|
| 17 |
+
mask_list.append(np.array(s['mask'], dtype=np.uint8)) # Convert to numpy array and ensure it's uint8
|
| 18 |
|
| 19 |
+
# Initialize final mask with zeros
|
| 20 |
+
final_mask = np.zeros_like(mask_list[0], dtype=np.uint8)
|
| 21 |
|
| 22 |
+
# Combine masks into one
|
|
|
|
| 23 |
for mask in mask_list:
|
| 24 |
+
final_mask = np.maximum(final_mask, mask)
|
| 25 |
+
|
| 26 |
+
# Optional: Smooth the mask to reduce rough edges (using Gaussian blur)
|
| 27 |
+
final_mask = cv2.GaussianBlur(final_mask, (7, 7), 0)
|
| 28 |
+
|
| 29 |
+
# Optional: Dilate the mask to ensure coverage at edges
|
| 30 |
+
kernel = np.ones((5,5), np.uint8)
|
| 31 |
+
final_mask = cv2.dilate(final_mask, kernel, iterations=1)
|
| 32 |
+
|
| 33 |
+
# Convert mask to binary (0 or 255) if needed for alpha channel
|
| 34 |
+
_, final_mask = cv2.threshold(final_mask, 127, 255, cv2.THRESH_BINARY)
|
| 35 |
+
|
| 36 |
# Convert final mask from np array to PIL image
|
| 37 |
final_mask = Image.fromarray(final_mask)
|
| 38 |
|
| 39 |
+
# Apply mask to original image (convert to RGBA first)
|
| 40 |
+
img = img.convert("RGBA")
|
| 41 |
img.putalpha(final_mask)
|
| 42 |
|
| 43 |
+
return img
|