devendergarg14 commited on
Commit
2f63795
·
verified ·
1 Parent(s): 509bed9

Update SegCloth.py

Browse files
Files changed (1) hide show
  1. SegCloth.py +10 -4
SegCloth.py CHANGED
@@ -6,12 +6,13 @@ 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):
10
  # Segment image
11
  segments = segmenter(img)
12
 
13
  # Define clothing items to expand
14
- EXPAND_CLOTHING = {"Upper-clothes", "Skirt", "Pants", "Dress"}
15
 
16
  # Create list of masks
17
  mask_list = []
@@ -35,14 +36,19 @@ def segment_clothing(img, clothes):
35
  for mask in mask_list:
36
  final_mask = np.maximum(final_mask, mask)
37
 
38
- # Expand selected clothing masks using morphological dilation
39
  for mask in expand_mask_list:
40
  height, width = mask.shape
41
  kernel_size = max(1, int(0.02 * min(height, width))) # 5% expansion
42
  kernel = np.ones((kernel_size, kernel_size), np.uint8)
43
 
44
- # Dilate mask and merge into final mask
45
- dilated_mask = cv2.dilate(mask, kernel, iterations=1)
 
 
 
 
 
46
  final_mask = np.maximum(final_mask, dilated_mask)
47
 
48
  # Optional: Use contour filling to ensure all areas within contours are filled
 
6
  # Initialize segmentation pipeline
7
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
8
 
9
+
10
  def segment_clothing(img, clothes):
11
  # Segment image
12
  segments = segmenter(img)
13
 
14
  # Define clothing items to expand
15
+ EXPAND_CLOTHING = {"Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe"}
16
 
17
  # Create list of masks
18
  mask_list = []
 
36
  for mask in mask_list:
37
  final_mask = np.maximum(final_mask, mask)
38
 
39
+ # Expand selected clothing masks using closing + dilation
40
  for mask in expand_mask_list:
41
  height, width = mask.shape
42
  kernel_size = max(1, int(0.02 * min(height, width))) # 5% expansion
43
  kernel = np.ones((kernel_size, kernel_size), np.uint8)
44
 
45
+ # **Step 1: Fill gaps using Closing (Dilation + Erosion)**
46
+ closed_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
47
+
48
+ # **Step 2: Expand using Dilation**
49
+ dilated_mask = cv2.dilate(closed_mask, kernel, iterations=1)
50
+
51
+ # Merge into final mask
52
  final_mask = np.maximum(final_mask, dilated_mask)
53
 
54
  # Optional: Use contour filling to ensure all areas within contours are filled