patrickligardes commited on
Commit
979bdfb
β€’
1 Parent(s): 6525a88

Update utils_mask.py

Browse files
Files changed (1) hide show
  1. utils_mask.py +33 -45
utils_mask.py CHANGED
@@ -24,11 +24,11 @@ label_map = {
24
  }
25
 
26
  def extend_arm_mask(wrist, elbow, scale):
27
- wrist = elbow + scale * (wrist - elbow)
28
- return wrist
29
 
30
  def hole_fill(img):
31
- img = np.pad(img[1:-1, 1:-1], pad_width=1, mode='constant', constant_values=0)
32
  img_copy = img.copy()
33
  mask = np.zeros((img.shape[0] + 2, img.shape[1] + 2), dtype=np.uint8)
34
 
@@ -51,7 +51,7 @@ def refine_mask(mask):
51
 
52
  return refine_mask
53
 
54
- def get_mask_location(model_type, category, model_parse: Image.Image, keypoint: dict, width=384, height=512):
55
  im_parse = model_parse.resize((width, height), Image.NEAREST)
56
  parse_array = np.array(im_parse)
57
 
@@ -60,58 +60,48 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
60
  elif model_type == 'dc':
61
  arm_width = 45
62
  else:
63
- raise ValueError("model_type must be 'hd' or 'dc'!")
64
 
65
- parse_head = (parse_array == label_map["head"]).astype(np.float32) + \
66
- (parse_array == label_map["hat"]).astype(np.float32) + \
67
- (parse_array == label_map["hair"]).astype(np.float32) + \
68
- (parse_array == label_map["sunglasses"]).astype(np.float32)
69
 
70
  parser_mask_fixed = (parse_array == label_map["left_shoe"]).astype(np.float32) + \
71
  (parse_array == label_map["right_shoe"]).astype(np.float32) + \
72
- (parse_array == label_map["bag"]).astype(np.float32) + \
73
- (parse_array == label_map["scarf"]).astype(np.float32)
 
74
 
75
  parser_mask_changeable = (parse_array == label_map["background"]).astype(np.float32)
76
 
77
- arms_left = (parse_array == label_map["left_arm"]).astype(np.float32)
78
- arms_right = (parse_array == label_map["right_arm"]).astype(np.float32)
79
-
80
- if category == 'dresses':
81
- # Initial dress mask for the upper body (excluding head)
82
- parse_mask_upper = np.logical_or((parse_array == label_map["upper_clothes"]), (parse_array == label_map["dress"])).astype(np.float32)
83
-
84
- # Create a mask for the legs (including skirts and pants)
85
- parse_mask_legs = np.logical_or.reduce((parse_array == label_map["skirt"],
86
- parse_array == label_map["pants"],
87
- parse_array == label_map["left_leg"],
88
- parse_array == label_map["right_leg"])).astype(np.float32)
89
 
90
- # Dilate the leg mask to ensure coverage and fill gaps
91
- parse_mask_legs_dilated = cv2.dilate(parse_mask_legs.astype(np.uint8), np.ones((6, 6), np.uint8), iterations=6)
 
 
 
92
 
93
- # Combine the upper body mask with the dilated leg mask
94
- parse_mask = np.maximum(parse_mask_upper, parse_mask_legs_dilated)
95
 
96
  elif category == 'upper_body':
97
- parse_mask = (parse_array == label_map["upper_clothes"]).astype(np.float32)
98
-
 
 
99
  parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
100
-
101
  elif category == 'lower_body':
102
- parse_mask = (parse_array == label_map["pants"]).astype(np.float32) + \
103
- (parse_array == label_map["skirt"]).astype(np.float32) + \
104
- (parse_array == label_map["left_leg"]).astype(np.float32) + \
105
- (parse_array == label_map["right_leg"]).astype(np.float32)
106
-
107
  parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
108
- (parse_array == label_map["left_arm"]).astype(np.float32) + \
109
- (parse_array == label_map["right_arm"]).astype(np.float32)
110
-
111
  parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
112
-
113
  else:
114
- raise NotImplementedError("Category not implemented")
115
 
116
  # Load pose points
117
  pose_data = keypoint["pose_keypoints_2d"]
@@ -122,7 +112,6 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
122
  im_arms_right = Image.new('L', (width, height))
123
  arms_draw_left = ImageDraw.Draw(im_arms_left)
124
  arms_draw_right = ImageDraw.Draw(im_arms_right)
125
-
126
  if category == 'dresses' or category == 'upper_body':
127
  shoulder_right = np.multiply(tuple(pose_data[2][:2]), height / 512.0)
128
  shoulder_left = np.multiply(tuple(pose_data[5][:2]), height / 512.0)
@@ -134,6 +123,7 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
134
  size_left = [shoulder_left[0] - ARM_LINE_WIDTH // 2, shoulder_left[1] - ARM_LINE_WIDTH // 2, shoulder_left[0] + ARM_LINE_WIDTH // 2, shoulder_left[1] + ARM_LINE_WIDTH // 2]
135
  size_right = [shoulder_right[0] - ARM_LINE_WIDTH // 2, shoulder_right[1] - ARM_LINE_WIDTH // 2, shoulder_right[0] + ARM_LINE_WIDTH // 2,
136
  shoulder_right[1] + ARM_LINE_WIDTH // 2]
 
137
 
138
  if wrist_right[0] <= 1. and wrist_right[1] <= 1.:
139
  im_arms_right = arms_right
@@ -154,9 +144,7 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
154
  parser_mask_fixed += hands_left + hands_right
155
 
156
  parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
157
- parse_mask = cv2.dilate(parse_mask.astype(np.uint8), np.ones((5, 5), np.uint8), iterations=5)
158
-
159
-
160
  if category == 'dresses' or category == 'upper_body':
161
  neck_mask = (parse_array == 18).astype(np.float32)
162
  neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.uint16), iterations=1)
@@ -176,4 +164,4 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
176
  mask = Image.fromarray(inpaint_mask.astype(np.uint8) * 255)
177
  mask_gray = Image.fromarray(inpaint_mask.astype(np.uint8) * 127)
178
 
179
- return mask, mask_gray
 
24
  }
25
 
26
  def extend_arm_mask(wrist, elbow, scale):
27
+ wrist = elbow + scale * (wrist - elbow)
28
+ return wrist
29
 
30
  def hole_fill(img):
31
+ img = np.pad(img[1:-1, 1:-1], pad_width = 1, mode = 'constant', constant_values=0)
32
  img_copy = img.copy()
33
  mask = np.zeros((img.shape[0] + 2, img.shape[1] + 2), dtype=np.uint8)
34
 
 
51
 
52
  return refine_mask
53
 
54
+ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint: dict, width=384,height=512):
55
  im_parse = model_parse.resize((width, height), Image.NEAREST)
56
  parse_array = np.array(im_parse)
57
 
 
60
  elif model_type == 'dc':
61
  arm_width = 45
62
  else:
63
+ raise ValueError("model_type must be \'hd\' or \'dc\'!")
64
 
65
+ parse_head = (parse_array == 1).astype(np.float32) + \
66
+ (parse_array == 3).astype(np.float32) + \
67
+ (parse_array == 11).astype(np.float32)
 
68
 
69
  parser_mask_fixed = (parse_array == label_map["left_shoe"]).astype(np.float32) + \
70
  (parse_array == label_map["right_shoe"]).astype(np.float32) + \
71
+ (parse_array == label_map["hat"]).astype(np.float32) + \
72
+ (parse_array == label_map["sunglasses"]).astype(np.float32) + \
73
+ (parse_array == label_map["bag"]).astype(np.float32)
74
 
75
  parser_mask_changeable = (parse_array == label_map["background"]).astype(np.float32)
76
 
77
+ arms_left = (parse_array == 14).astype(np.float32)
78
+ arms_right = (parse_array == 15).astype(np.float32)
 
 
 
 
 
 
 
 
 
 
79
 
80
+ if category == 'dresses':
81
+ parse_mask = (parse_array == 7).astype(np.float32) + \
82
+ (parse_array == 4).astype(np.float32) + \
83
+ (parse_array == 5).astype(np.float32) + \
84
+ (parse_array == 6).astype(np.float32)
85
 
86
+ parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
 
87
 
88
  elif category == 'upper_body':
89
+ parse_mask = (parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32)
90
+ parser_mask_fixed_lower_cloth = (parse_array == label_map["skirt"]).astype(np.float32) + \
91
+ (parse_array == label_map["pants"]).astype(np.float32)
92
+ parser_mask_fixed += parser_mask_fixed_lower_cloth
93
  parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
 
94
  elif category == 'lower_body':
95
+ parse_mask = (parse_array == 6).astype(np.float32) + \
96
+ (parse_array == 12).astype(np.float32) + \
97
+ (parse_array == 13).astype(np.float32) + \
98
+ (parse_array == 5).astype(np.float32)
 
99
  parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
100
+ (parse_array == 14).astype(np.float32) + \
101
+ (parse_array == 15).astype(np.float32)
 
102
  parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
 
103
  else:
104
+ raise NotImplementedError
105
 
106
  # Load pose points
107
  pose_data = keypoint["pose_keypoints_2d"]
 
112
  im_arms_right = Image.new('L', (width, height))
113
  arms_draw_left = ImageDraw.Draw(im_arms_left)
114
  arms_draw_right = ImageDraw.Draw(im_arms_right)
 
115
  if category == 'dresses' or category == 'upper_body':
116
  shoulder_right = np.multiply(tuple(pose_data[2][:2]), height / 512.0)
117
  shoulder_left = np.multiply(tuple(pose_data[5][:2]), height / 512.0)
 
123
  size_left = [shoulder_left[0] - ARM_LINE_WIDTH // 2, shoulder_left[1] - ARM_LINE_WIDTH // 2, shoulder_left[0] + ARM_LINE_WIDTH // 2, shoulder_left[1] + ARM_LINE_WIDTH // 2]
124
  size_right = [shoulder_right[0] - ARM_LINE_WIDTH // 2, shoulder_right[1] - ARM_LINE_WIDTH // 2, shoulder_right[0] + ARM_LINE_WIDTH // 2,
125
  shoulder_right[1] + ARM_LINE_WIDTH // 2]
126
+
127
 
128
  if wrist_right[0] <= 1. and wrist_right[1] <= 1.:
129
  im_arms_right = arms_right
 
144
  parser_mask_fixed += hands_left + hands_right
145
 
146
  parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
147
+ parse_mask = cv2.dilate(parse_mask, np.ones((5, 5), np.uint16), iterations=5)
 
 
148
  if category == 'dresses' or category == 'upper_body':
149
  neck_mask = (parse_array == 18).astype(np.float32)
150
  neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.uint16), iterations=1)
 
164
  mask = Image.fromarray(inpaint_mask.astype(np.uint8) * 255)
165
  mask_gray = Image.fromarray(inpaint_mask.astype(np.uint8) * 127)
166
 
167
+ return mask, mask_gray