Spaces:
Sleeping
Sleeping
Update utils_mask.py
Browse files- utils_mask.py +33 -25
utils_mask.py
CHANGED
|
@@ -62,53 +62,58 @@ 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["
|
| 72 |
-
(parse_array == label_map["
|
| 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 |
-
parse_mask_upper = (parse_array ==
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Fill gaps between the legs
|
| 88 |
parse_mask_legs = (parse_array == label_map["left_leg"]).astype(np.float32) + \
|
| 89 |
-
|
| 90 |
parse_mask_legs_dilated = cv2.dilate(parse_mask_legs.astype(np.uint8), np.ones((5, 5), np.uint8), iterations=6)
|
| 91 |
parse_mask_lower = np.maximum(parse_mask_lower, parse_mask_legs_dilated)
|
| 92 |
|
| 93 |
# Combine upper and filled lower body masks
|
| 94 |
parse_mask = np.logical_or(parse_mask_upper, parse_mask_lower)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
elif category == 'upper_body':
|
| 99 |
-
parse_mask = (parse_array ==
|
|
|
|
| 100 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
|
|
|
| 101 |
elif category == 'lower_body':
|
| 102 |
-
parse_mask = (parse_array ==
|
| 103 |
-
(parse_array ==
|
| 104 |
-
(parse_array ==
|
| 105 |
-
(parse_array ==
|
|
|
|
| 106 |
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 107 |
-
(parse_array ==
|
| 108 |
-
(parse_array ==
|
|
|
|
| 109 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
|
|
|
| 110 |
else:
|
| 111 |
-
raise NotImplementedError
|
| 112 |
|
| 113 |
# Load pose points
|
| 114 |
pose_data = keypoint["pose_keypoints_2d"]
|
|
@@ -119,6 +124,7 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 119 |
im_arms_right = Image.new('L', (width, height))
|
| 120 |
arms_draw_left = ImageDraw.Draw(im_arms_left)
|
| 121 |
arms_draw_right = ImageDraw.Draw(im_arms_right)
|
|
|
|
| 122 |
if category == 'dresses' or category == 'upper_body':
|
| 123 |
shoulder_right = np.multiply(tuple(pose_data[2][:2]), height / 512.0)
|
| 124 |
shoulder_left = np.multiply(tuple(pose_data[5][:2]), height / 512.0)
|
|
@@ -150,7 +156,9 @@ 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, np.ones((5, 5), np.
|
|
|
|
|
|
|
| 154 |
if category == 'dresses' or category == 'upper_body':
|
| 155 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 156 |
neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.uint16), iterations=1)
|
|
|
|
| 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 |
+
parse_mask_upper = (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 82 |
+
(parse_array == label_map["dress"]).astype(np.float32)
|
| 83 |
+
|
| 84 |
+
parse_mask_lower = (parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 85 |
+
(parse_array == label_map["pants"]).astype(np.float32) + \
|
| 86 |
+
(parse_array == label_map["left_leg"]).astype(np.float32) + \
|
| 87 |
+
(parse_array == label_map["right_leg"]).astype(np.float32)
|
| 88 |
|
| 89 |
# Fill gaps between the legs
|
| 90 |
parse_mask_legs = (parse_array == label_map["left_leg"]).astype(np.float32) + \
|
| 91 |
+
(parse_array == label_map["right_leg"]).astype(np.float32)
|
| 92 |
parse_mask_legs_dilated = cv2.dilate(parse_mask_legs.astype(np.uint8), np.ones((5, 5), np.uint8), iterations=6)
|
| 93 |
parse_mask_lower = np.maximum(parse_mask_lower, parse_mask_legs_dilated)
|
| 94 |
|
| 95 |
# Combine upper and filled lower body masks
|
| 96 |
parse_mask = np.logical_or(parse_mask_upper, parse_mask_lower)
|
| 97 |
|
|
|
|
|
|
|
| 98 |
elif category == 'upper_body':
|
| 99 |
+
parse_mask = (parse_array == label_map["upper_clothes"]).astype(np.float32)
|
| 100 |
+
|
| 101 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 102 |
+
|
| 103 |
elif category == 'lower_body':
|
| 104 |
+
parse_mask = (parse_array == label_map["pants"]).astype(np.float32) + \
|
| 105 |
+
(parse_array == label_map["skirt"]).astype(np.float32) + \
|
| 106 |
+
(parse_array == label_map["left_leg"]).astype(np.float32) + \
|
| 107 |
+
(parse_array == label_map["right_leg"]).astype(np.float32)
|
| 108 |
+
|
| 109 |
parser_mask_fixed += (parse_array == label_map["upper_clothes"]).astype(np.float32) + \
|
| 110 |
+
(parse_array == label_map["left_arm"]).astype(np.float32) + \
|
| 111 |
+
(parse_array == label_map["right_arm"]).astype(np.float32)
|
| 112 |
+
|
| 113 |
parser_mask_changeable += np.logical_and(parse_array, np.logical_not(parser_mask_fixed))
|
| 114 |
+
|
| 115 |
else:
|
| 116 |
+
raise NotImplementedError("Category not implemented")
|
| 117 |
|
| 118 |
# Load pose points
|
| 119 |
pose_data = keypoint["pose_keypoints_2d"]
|
|
|
|
| 124 |
im_arms_right = Image.new('L', (width, height))
|
| 125 |
arms_draw_left = ImageDraw.Draw(im_arms_left)
|
| 126 |
arms_draw_right = ImageDraw.Draw(im_arms_right)
|
| 127 |
+
|
| 128 |
if category == 'dresses' or category == 'upper_body':
|
| 129 |
shoulder_right = np.multiply(tuple(pose_data[2][:2]), height / 512.0)
|
| 130 |
shoulder_left = np.multiply(tuple(pose_data[5][:2]), height / 512.0)
|
|
|
|
| 156 |
parser_mask_fixed += hands_left + hands_right
|
| 157 |
|
| 158 |
parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
|
| 159 |
+
parse_mask = cv2.dilate(parse_mask.astype(np.uint8), np.ones((5, 5), np.uint8), iterations=5)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
if category == 'dresses' or category == 'upper_body':
|
| 163 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 164 |
neck_mask = cv2.dilate(neck_mask, np.ones((5, 5), np.uint16), iterations=1)
|