Transform keypoint annotations of an image. If a keypoint is transformed out of image boundary, it will be marked "unlabeled" (visibility=0) Args: keypoints (list[float]): Nx3 float in Detectron2's Dataset format. Each point is represented by (x, y, visibility).
(keypoints, transforms, image_size, keypoint_hflip_indices=None)
| 317 | |
| 318 | |
| 319 | def transform_keypoint_annotations(keypoints, transforms, image_size, keypoint_hflip_indices=None): |
| 320 | """ |
| 321 | Transform keypoint annotations of an image. |
| 322 | If a keypoint is transformed out of image boundary, it will be marked "unlabeled" (visibility=0) |
| 323 | |
| 324 | Args: |
| 325 | keypoints (list[float]): Nx3 float in Detectron2's Dataset format. |
| 326 | Each point is represented by (x, y, visibility). |
| 327 | transforms (TransformList): |
| 328 | image_size (tuple): the height, width of the transformed image |
| 329 | keypoint_hflip_indices (ndarray[int]): see `create_keypoint_hflip_indices`. |
| 330 | When `transforms` includes horizontal flip, will use the index |
| 331 | mapping to flip keypoints. |
| 332 | """ |
| 333 | # (N*3,) -> (N, 3) |
| 334 | keypoints = np.asarray(keypoints, dtype="float64").reshape(-1, 3) |
| 335 | keypoints_xy = transforms.apply_coords(keypoints[:, :2]) |
| 336 | |
| 337 | # Set all out-of-boundary points to "unlabeled" |
| 338 | inside = (keypoints_xy >= np.array([0, 0])) & (keypoints_xy <= np.array(image_size[::-1])) |
| 339 | inside = inside.all(axis=1) |
| 340 | keypoints[:, :2] = keypoints_xy |
| 341 | keypoints[:, 2][~inside] = 0 |
| 342 | |
| 343 | # This assumes that HorizFlipTransform is the only one that does flip |
| 344 | do_hflip = sum(isinstance(t, T.HFlipTransform) for t in transforms.transforms) % 2 == 1 |
| 345 | |
| 346 | # Alternative way: check if probe points was horizontally flipped. |
| 347 | # probe = np.asarray([[0.0, 0.0], [image_width, 0.0]]) |
| 348 | # probe_aug = transforms.apply_coords(probe.copy()) |
| 349 | # do_hflip = np.sign(probe[1][0] - probe[0][0]) != np.sign(probe_aug[1][0] - probe_aug[0][0]) # noqa |
| 350 | |
| 351 | # If flipped, swap each keypoint with its opposite-handed equivalent |
| 352 | if do_hflip: |
| 353 | assert keypoint_hflip_indices is not None |
| 354 | keypoints = keypoints[keypoint_hflip_indices, :] |
| 355 | |
| 356 | # Maintain COCO convention that if visibility == 0 (unlabeled), then x, y = 0 |
| 357 | keypoints[keypoints[:, 2] == 0] = 0 |
| 358 | return keypoints |
| 359 | |
| 360 | |
| 361 | def annotations_to_instances(annos, image_size, mask_format="polygon"): |
no test coverage detected