Spaces:
Sleeping
Sleeping
Update SegCloth.py
Browse files- SegCloth.py +12 -8
SegCloth.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
-
from PIL import Image
|
| 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=
|
| 10 |
# Segment image
|
| 11 |
segments = segmenter(img)
|
| 12 |
|
| 13 |
# Create list of masks
|
| 14 |
mask_list = []
|
| 15 |
for s in segments:
|
| 16 |
-
if
|
| 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
|
|
@@ -23,12 +23,16 @@ def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "D
|
|
| 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=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
from PIL import Image
|
| 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)
|
| 12 |
|
| 13 |
# Create list of masks
|
| 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
|
|
|
|
| 23 |
for mask in mask_list:
|
| 24 |
final_mask = np.maximum(final_mask, mask)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Optional: Dilate the mask to ensure coverage at edges
|
| 27 |
+
kernel = np.ones((5, 5), np.uint8)
|
| 28 |
+
final_mask = cv2.dilate(final_mask, kernel, iterations=2)
|
| 29 |
+
|
| 30 |
+
# Optional: Erode to slightly smoothen the mask
|
| 31 |
+
final_mask = cv2.erode(final_mask, kernel, iterations=1)
|
| 32 |
+
|
| 33 |
+
# Optional: Use contour filling to ensure all areas within contours are filled
|
| 34 |
+
contours, _ = cv2.findContours(final_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 35 |
+
cv2.drawContours(final_mask, contours, -1, (255), thickness=cv2.FILLED)
|
| 36 |
|
| 37 |
# Convert mask to binary (0 or 255) if needed for alpha channel
|
| 38 |
_, final_mask = cv2.threshold(final_mask, 127, 255, cv2.THRESH_BINARY)
|