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

Update SegCloth.py

Browse files
Files changed (1) hide show
  1. SegCloth.py +23 -12
SegCloth.py CHANGED
@@ -10,29 +10,40 @@ 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 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
 
25
- # Combine masks into one
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)
 
10
  # Segment image
11
  segments = segmenter(img)
12
 
13
+ # Define clothing items to expand
14
+ EXPAND_CLOTHING = {"Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"}
15
+
16
  # Create list of masks
17
  mask_list = []
18
+ expand_mask_list = [] # Separate list for clothes that need expansion
19
+
20
  for s in segments:
21
+ mask = np.array(s['mask'], dtype=np.uint8) # Convert mask to numpy array
22
  if s['label'] in clothes:
23
+ if s['label'] in EXPAND_CLOTHING:
24
+ expand_mask_list.append(mask) # Store separately for expansion
25
+ else:
26
+ mask_list.append(mask) # Keep others as they are
27
 
28
+ if not mask_list and not expand_mask_list:
29
+ return img # Return original image if no relevant items found
30
 
31
  # Initialize final mask with zeros
32
+ final_mask = np.zeros_like(mask_list[0] if mask_list else expand_mask_list[0], dtype=np.uint8)
33
 
34
+ # Combine normal masks into one
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.05 * 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
49
  contours, _ = cv2.findContours(final_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)