Spaces:
Sleeping
Sleeping
Update utils_mask.py
Browse files- utils_mask.py +33 -23
utils_mask.py
CHANGED
|
@@ -62,51 +62,61 @@ 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["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 ==
|
| 78 |
-
arms_right = (parse_array ==
|
| 79 |
|
| 80 |
if category == 'dresses':
|
| 81 |
-
#
|
| 82 |
parse_mask_upper = (parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
parse_mask_legs = 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 leg mask
|
| 94 |
parse_mask = np.maximum(parse_mask_upper, parse_mask_legs)
|
| 95 |
|
| 96 |
-
|
| 97 |
elif category == 'upper_body':
|
| 98 |
parse_mask = (parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32)
|
| 99 |
parser_mask_fixed_lower_cloth = (parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 100 |
(parse_array == label_map["pants"]).astype(np.float32)
|
| 101 |
parser_mask_fixed += parser_mask_fixed_lower_cloth
|
|
|
|
|
|
|
| 102 |
elif category == 'lower_body':
|
| 103 |
parse_mask = (parse_array == 6).astype(np.float32) + \
|
| 104 |
(parse_array == 12).astype(np.float32) + \
|
| 105 |
(parse_array == 13).astype(np.float32) + \
|
| 106 |
(parse_array == 5).astype(np.float32)
|
| 107 |
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 108 |
-
(parse_array ==
|
| 109 |
-
(parse_array ==
|
|
|
|
| 110 |
else:
|
| 111 |
raise NotImplementedError
|
| 112 |
|
|
@@ -130,7 +140,7 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 130 |
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]
|
| 131 |
size_right = [shoulder_right[0] - ARM_LINE_WIDTH // 2, shoulder_right[1] - ARM_LINE_WIDTH // 2, shoulder_right[0] + ARM_LINE_WIDTH // 2,
|
| 132 |
shoulder_right[1] + ARM_LINE_WIDTH // 2]
|
| 133 |
-
|
| 134 |
if wrist_right[0] <= 1. and wrist_right[1] <= 1.:
|
| 135 |
im_arms_right = arms_right
|
| 136 |
else:
|
|
@@ -150,10 +160,10 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 150 |
parser_mask_fixed += hands_left + hands_right
|
| 151 |
|
| 152 |
parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
|
| 153 |
-
parse_mask = cv2.dilate(parse_mask
|
| 154 |
if category == 'dresses' or category == 'upper_body':
|
| 155 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 156 |
-
neck_mask = cv2.dilate(neck_mask
|
| 157 |
neck_mask = np.logical_and(neck_mask, np.logical_not(parse_head))
|
| 158 |
parse_mask = np.logical_or(parse_mask, neck_mask)
|
| 159 |
arm_mask = cv2.dilate(np.logical_or(im_arms_left, im_arms_right).astype('float32'), np.ones((5, 5), np.uint8), iterations=4)
|
|
|
|
| 62 |
else:
|
| 63 |
raise ValueError("model_type must be 'hd' or 'dc'!")
|
| 64 |
|
| 65 |
+
parse_head = (parse_array == label_map["hat"]).astype(np.float32) + \
|
| 66 |
+
(parse_array == label_map["hair"]).astype(np.float32) + \
|
| 67 |
+
(parse_array == label_map["head"]).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["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 == 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 |
+
# Combine upper body category logic
|
| 82 |
parse_mask_upper = (parse_array == 4).astype(np.float32) + (parse_array == 7).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 |
+
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 87 |
+
|
| 88 |
+
# Combine lower body category logic
|
| 89 |
+
parse_mask_legs = (parse_array == 6).astype(np.float32) + \
|
| 90 |
+
(parse_array == 12).astype(np.float32) + \
|
| 91 |
+
(parse_array == 13).astype(np.float32) + \
|
| 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 |
+
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 97 |
+
|
| 98 |
+
# Fill gaps between legs
|
| 99 |
parse_mask_legs = cv2.dilate(parse_mask_legs.astype(np.uint8), np.ones((6, 6), np.uint8), iterations=6)
|
| 100 |
+
|
| 101 |
# Combine the upper body mask with the leg mask
|
| 102 |
parse_mask = np.maximum(parse_mask_upper, parse_mask_legs)
|
| 103 |
|
|
|
|
| 104 |
elif category == 'upper_body':
|
| 105 |
parse_mask = (parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32)
|
| 106 |
parser_mask_fixed_lower_cloth = (parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 107 |
(parse_array == label_map["pants"]).astype(np.float32)
|
| 108 |
parser_mask_fixed += parser_mask_fixed_lower_cloth
|
| 109 |
+
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 110 |
+
|
| 111 |
elif category == 'lower_body':
|
| 112 |
parse_mask = (parse_array == 6).astype(np.float32) + \
|
| 113 |
(parse_array == 12).astype(np.float32) + \
|
| 114 |
(parse_array == 13).astype(np.float32) + \
|
| 115 |
(parse_array == 5).astype(np.float32)
|
| 116 |
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 117 |
+
(parse_array == label_map["left_arm"]).astype(np.float32) + \
|
| 118 |
+
(parse_array == label_map["right_arm"]).astype(np.float32)
|
| 119 |
+
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 120 |
else:
|
| 121 |
raise NotImplementedError
|
| 122 |
|
|
|
|
| 140 |
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]
|
| 141 |
size_right = [shoulder_right[0] - ARM_LINE_WIDTH // 2, shoulder_right[1] - ARM_LINE_WIDTH // 2, shoulder_right[0] + ARM_LINE_WIDTH // 2,
|
| 142 |
shoulder_right[1] + ARM_LINE_WIDTH // 2]
|
| 143 |
+
|
| 144 |
if wrist_right[0] <= 1. and wrist_right[1] <= 1.:
|
| 145 |
im_arms_right = arms_right
|
| 146 |
else:
|
|
|
|
| 160 |
parser_mask_fixed += hands_left + hands_right
|
| 161 |
|
| 162 |
parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
|
| 163 |
+
parse_mask = cv2.dilate(parse_mask, np.ones((5, 5), np.uint8), iterations=5)
|
| 164 |
if category == 'dresses' or category == 'upper_body':
|
| 165 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 166 |
+
neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.uint8), iterations=1)
|
| 167 |
neck_mask = np.logical_and(neck_mask, np.logical_not(parse_head))
|
| 168 |
parse_mask = np.logical_or(parse_mask, neck_mask)
|
| 169 |
arm_mask = cv2.dilate(np.logical_or(im_arms_left, im_arms_right).astype('float32'), np.ones((5, 5), np.uint8), iterations=4)
|