Spaces:
Sleeping
Sleeping
Update utils_mask.py
Browse files- utils_mask.py +27 -37
utils_mask.py
CHANGED
|
@@ -62,48 +62,39 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 62 |
else:
|
| 63 |
raise ValueError("model_type must be 'hd' or 'dc'!")
|
| 64 |
|
| 65 |
-
parse_head = (parse_array ==
|
| 66 |
-
(parse_array ==
|
| 67 |
-
(parse_array ==
|
| 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["sunglasses"]).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 ==
|
| 78 |
-
arms_right = (parse_array ==
|
| 79 |
|
| 80 |
if category == 'dresses':
|
| 81 |
-
#
|
| 82 |
-
parse_mask_upper = (parse_array ==
|
| 83 |
parser_mask_fixed_lower_cloth = (parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 84 |
-
(parse_array == label_map["pants"]).astype(np.float32)
|
| 85 |
-
parser_mask_fixed += parser_mask_fixed_lower_cloth
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
(parse_array == 5).astype(np.float32)
|
| 93 |
-
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 94 |
-
(parse_array == label_map["left_arm"]).astype(np.float32) + \
|
| 95 |
-
(parse_array == label_map["right_arm"]).astype(np.float32)
|
| 96 |
-
|
| 97 |
-
# Include parse_mask_legs in parser_mask_changeable
|
| 98 |
-
parser_mask_changeable = np.logical_or(parser_mask_changeable, parse_mask_legs)
|
| 99 |
-
|
| 100 |
-
parse_mask_legs = cv2.dilate(parse_mask_legs.astype(np.uint8), np.ones((6, 6), np.uint8), iterations=6)
|
| 101 |
-
|
| 102 |
-
# Combine the upper body mask with the leg mask
|
| 103 |
-
parse_mask = np.logical_and(parser_mask_changeable, np.logical_not(parse_mask))
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
|
|
|
|
|
|
| 107 |
|
| 108 |
elif category == 'upper_body':
|
| 109 |
parse_mask = (parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32)
|
|
@@ -111,15 +102,14 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 111 |
(parse_array == label_map["pants"]).astype(np.float32)
|
| 112 |
parser_mask_fixed += parser_mask_fixed_lower_cloth
|
| 113 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 114 |
-
|
| 115 |
elif category == 'lower_body':
|
| 116 |
parse_mask = (parse_array == 6).astype(np.float32) + \
|
| 117 |
(parse_array == 12).astype(np.float32) + \
|
| 118 |
(parse_array == 13).astype(np.float32) + \
|
| 119 |
(parse_array == 5).astype(np.float32)
|
| 120 |
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 121 |
-
(parse_array ==
|
| 122 |
-
(parse_array ==
|
| 123 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 124 |
else:
|
| 125 |
raise NotImplementedError
|
|
@@ -144,7 +134,7 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 144 |
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]
|
| 145 |
size_right = [shoulder_right[0] - ARM_LINE_WIDTH // 2, shoulder_right[1] - ARM_LINE_WIDTH // 2, shoulder_right[0] + ARM_LINE_WIDTH // 2,
|
| 146 |
shoulder_right[1] + ARM_LINE_WIDTH // 2]
|
| 147 |
-
|
| 148 |
if wrist_right[0] <= 1. and wrist_right[1] <= 1.:
|
| 149 |
im_arms_right = arms_right
|
| 150 |
else:
|
|
@@ -164,13 +154,13 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 164 |
parser_mask_fixed += hands_left + hands_right
|
| 165 |
|
| 166 |
parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
|
| 167 |
-
parse_mask = cv2.dilate(parse_mask, np.ones((5, 5), np.
|
| 168 |
if category == 'dresses' or category == 'upper_body':
|
| 169 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 170 |
-
neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.
|
| 171 |
neck_mask = np.logical_and(neck_mask, np.logical_not(parse_head))
|
| 172 |
parse_mask = np.logical_or(parse_mask, neck_mask)
|
| 173 |
-
arm_mask = cv2.dilate(np.logical_or(im_arms_left, im_arms_right).astype('float32'), np.ones((5, 5), np.
|
| 174 |
parse_mask += np.logical_or(parse_mask, arm_mask)
|
| 175 |
|
| 176 |
parse_mask = np.logical_and(parser_mask_changeable, np.logical_not(parse_mask))
|
|
|
|
| 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 |
+
# Initial dress mask for the upper body
|
| 82 |
+
parse_mask_upper = np.logical_or((parse_array == label_map["upper_clothes"]), (parse_array == label_map["dress"])).astype(np.float32)
|
| 83 |
parser_mask_fixed_lower_cloth = (parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 84 |
+
(parse_array == label_map["pants"]).astype(np.float32)
|
| 85 |
+
parser_mask_fixed += parser_mask_fixed_lower_cloth
|
| 86 |
+
|
| 87 |
+
# Create a mask for the legs (including skirts and pants)
|
| 88 |
+
parse_mask_legs = (parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 89 |
+
(parse_array == label_map["pants"]).astype(np.float32) + \
|
| 90 |
+
(parse_array == label_map["left_leg"]).astype(np.float32) + \
|
| 91 |
+
(parse_array == label_map["right_leg"]).astype(np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Dilate the leg mask to ensure coverage and fill gaps
|
| 94 |
+
parse_mask_legs = cv2.dilate(parse_mask_legs.astype(np.uint8), np.ones((6, 6), np.uint8), iterations=6)
|
| 95 |
|
| 96 |
+
# Combine the upper body mask with the leg mask
|
| 97 |
+
parse_mask = np.maximum(parse_mask_upper, parse_mask_legs)
|
| 98 |
|
| 99 |
elif category == 'upper_body':
|
| 100 |
parse_mask = (parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32)
|
|
|
|
| 102 |
(parse_array == label_map["pants"]).astype(np.float32)
|
| 103 |
parser_mask_fixed += parser_mask_fixed_lower_cloth
|
| 104 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
|
|
|
| 105 |
elif category == 'lower_body':
|
| 106 |
parse_mask = (parse_array == 6).astype(np.float32) + \
|
| 107 |
(parse_array == 12).astype(np.float32) + \
|
| 108 |
(parse_array == 13).astype(np.float32) + \
|
| 109 |
(parse_array == 5).astype(np.float32)
|
| 110 |
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 111 |
+
(parse_array == 14).astype(np.float32) + \
|
| 112 |
+
(parse_array == 15).astype(np.float32)
|
| 113 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 114 |
else:
|
| 115 |
raise NotImplementedError
|
|
|
|
| 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
|
| 140 |
else:
|
|
|
|
| 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, np.ones((5, 5), np.uint16), iterations=5)
|
| 158 |
if category == 'dresses' or category == 'upper_body':
|
| 159 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 160 |
+
neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.uint16), iterations=1)
|
| 161 |
neck_mask = np.logical_and(neck_mask, np.logical_not(parse_head))
|
| 162 |
parse_mask = np.logical_or(parse_mask, neck_mask)
|
| 163 |
+
arm_mask = cv2.dilate(np.logical_or(im_arms_left, im_arms_right).astype('float32'), np.ones((5, 5), np.uint16), iterations=4)
|
| 164 |
parse_mask += np.logical_or(parse_mask, arm_mask)
|
| 165 |
|
| 166 |
parse_mask = np.logical_and(parser_mask_changeable, np.logical_not(parse_mask))
|