devendergarg14 commited on
Commit
7ac5a56
·
verified ·
1 Parent(s): 6143da8

Update SegCloth.py

Browse files
Files changed (1) hide show
  1. SegCloth.py +13 -11
SegCloth.py CHANGED
@@ -6,7 +6,7 @@ import cv2 # OpenCV for better mask processing
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
 
@@ -16,6 +16,9 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
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
 
@@ -23,24 +26,22 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
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
- # Apply Gaussian blur to smooth edges and reduce noise
38
- #final_mask = cv2.GaussianBlur(final_mask, (7, 7), 0)
39
-
40
  # Convert mask to binary (0 or 255) if needed for alpha channel
41
  _, final_mask = cv2.threshold(final_mask, 127, 255, cv2.THRESH_BINARY)
42
 
43
- # Convert final mask from np array to PIL image
44
  final_mask = Image.fromarray(final_mask)
45
 
46
  # Apply mask to original image (convert to RGBA first)
@@ -48,3 +49,4 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
48
  img.putalpha(final_mask)
49
 
50
  return img
 
 
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
 
 
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
+ if not mask_list:
20
+ return img # Return original image if no clothes found
21
+
22
  # Initialize final mask with zeros
23
  final_mask = np.zeros_like(mask_list[0], dtype=np.uint8)
24
 
 
26
  for mask in mask_list:
27
  final_mask = np.maximum(final_mask, mask)
28
 
29
+ # Expand clothing boundaries using morphological dilation
30
+ height, width = final_mask.shape
31
+ kernel_size = max(1, int(0.05 * min(height, width))) # Ensure at least 1 pixel
32
+ kernel = np.ones((kernel_size, kernel_size), np.uint8)
33
+
34
+ # Dilate mask to expand clothing area
35
+ final_mask = cv2.dilate(final_mask, kernel, iterations=1)
36
 
37
  # Optional: Use contour filling to ensure all areas within contours are filled
38
  contours, _ = cv2.findContours(final_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
39
  cv2.drawContours(final_mask, contours, -1, (255), thickness=cv2.FILLED)
40
 
 
 
 
41
  # Convert mask to binary (0 or 255) if needed for alpha channel
42
  _, final_mask = cv2.threshold(final_mask, 127, 255, cv2.THRESH_BINARY)
43
 
44
+ # Convert final mask from numpy array to PIL image
45
  final_mask = Image.fromarray(final_mask)
46
 
47
  # Apply mask to original image (convert to RGBA first)
 
49
  img.putalpha(final_mask)
50
 
51
  return img
52
+