Spaces:
Sleeping
Sleeping
File size: 13,729 Bytes
5ac1897 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
from lib.kits.basic import *
# Copied from: https://github.com/shubham-goel/4D-Humans/blob/6ec79656a23c33237c724742ca2a0ec00b398b53/hmr2/datasets/utils.py#L663-L944
def crop_to_hips(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray) -> Tuple:
"""
Extreme cropping: Crop the box up to the hip locations.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
lower_body_keypoints = [10, 11, 13, 14, 19, 20, 21, 22, 23, 24, 25+0, 25+1, 25+4, 25+5]
keypoints_2d[lower_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def crop_to_shoulders(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box up to the shoulder locations.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
lower_body_keypoints = [3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 19, 20, 21, 22, 23, 24] + [25 + i for i in [0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 14, 15, 16]]
keypoints_2d[lower_body_keypoints, :] = 0
center, scale = get_bbox(keypoints_2d)
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.2 * scale[0]
height = 1.2 * scale[1]
return center_x, center_y, width, height
def crop_to_head(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the head.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
lower_body_keypoints = [3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 19, 20, 21, 22, 23, 24] + [25 + i for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16]]
keypoints_2d[lower_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.3 * scale[0]
height = 1.3 * scale[1]
return center_x, center_y, width, height
def crop_torso_only(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the torso.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
nontorso_body_keypoints = [0, 3, 4, 6, 7, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] + [25 + i for i in [0, 1, 4, 5, 6, 7, 10, 11, 13, 17, 18]]
keypoints_2d[nontorso_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def crop_rightarm_only(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the right arm.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
nonrightarm_body_keypoints = [0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] + [25 + i for i in [0, 1, 2, 3, 4, 5, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
keypoints_2d[nonrightarm_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def crop_leftarm_only(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the left arm.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
nonleftarm_body_keypoints = [0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] + [25 + i for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 18]]
keypoints_2d[nonleftarm_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def crop_legs_only(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the legs.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
nonlegs_body_keypoints = [0, 1, 2, 3, 4, 5, 6, 7, 15, 16, 17, 18] + [25 + i for i in [6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18]]
keypoints_2d[nonlegs_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def crop_rightleg_only(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the right leg.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
nonrightleg_body_keypoints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] + [25 + i for i in [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
keypoints_2d[nonrightleg_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def crop_leftleg_only(center_x: float, center_y: float, width: float, height: float, keypoints_2d: np.ndarray):
"""
Extreme cropping: Crop the box and keep on only the left leg.
Args:
center_x (float): x coordinate of the bounding box center.
center_y (float): y coordinate of the bounding box center.
width (float): Bounding box width.
height (float): Bounding box height.
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
center_x (float): x coordinate of the new bounding box center.
center_y (float): y coordinate of the new bounding box center.
width (float): New bounding box width.
height (float): New bounding box height.
"""
keypoints_2d = keypoints_2d.copy()
nonleftleg_body_keypoints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 16, 17, 18, 22, 23, 24] + [25 + i for i in [0, 1, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
keypoints_2d[nonleftleg_body_keypoints, :] = 0
if keypoints_2d[:, -1].sum() > 1:
center, scale = get_bbox(keypoints_2d)
center_x = center[0]
center_y = center[1]
width = 1.1 * scale[0]
height = 1.1 * scale[1]
return center_x, center_y, width, height
def full_body(keypoints_2d: np.ndarray) -> bool:
"""
Check if all main body joints are visible.
Args:
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
bool: True if all main body joints are visible.
"""
body_keypoints_openpose = [2, 3, 4, 5, 6, 7, 10, 11, 13, 14]
body_keypoints = [25 + i for i in [8, 7, 6, 9, 10, 11, 1, 0, 4, 5]]
return (np.maximum(keypoints_2d[body_keypoints, -1], keypoints_2d[body_keypoints_openpose, -1]) > 0).sum() == len(body_keypoints)
def upper_body(keypoints_2d: np.ndarray):
"""
Check if all upper body joints are visible.
Args:
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
Returns:
bool: True if all main body joints are visible.
"""
lower_body_keypoints_openpose = [10, 11, 13, 14]
lower_body_keypoints = [25 + i for i in [1, 0, 4, 5]]
upper_body_keypoints_openpose = [0, 1, 15, 16, 17, 18]
upper_body_keypoints = [25+8, 25+9, 25+12, 25+13, 25+17, 25+18]
return ((keypoints_2d[lower_body_keypoints + lower_body_keypoints_openpose, -1] > 0).sum() == 0)\
and ((keypoints_2d[upper_body_keypoints + upper_body_keypoints_openpose, -1] > 0).sum() >= 2)
def get_bbox(keypoints_2d: np.ndarray, rescale: float = 1.2) -> Tuple:
"""
Get center and scale for bounding box from openpose detections.
Args:
keypoints_2d (np.ndarray): Array of shape (N, 3) containing 2D keypoint locations.
rescale (float): Scale factor to rescale bounding boxes computed from the keypoints.
Returns:
center (np.ndarray): Array of shape (2,) containing the new bounding box center.
scale (float): New bounding box scale.
"""
valid = keypoints_2d[:,-1] > 0
valid_keypoints = keypoints_2d[valid][:,:-1]
center = 0.5 * (valid_keypoints.max(axis=0) + valid_keypoints.min(axis=0))
bbox_size = (valid_keypoints.max(axis=0) - valid_keypoints.min(axis=0))
# adjust bounding box tightness
scale = bbox_size
scale *= rescale
return center, scale
|