| 7 | |
| 8 | |
| 9 | def transform_instance_annotations( |
| 10 | annotation, transforms, image_size, keypoint_hflip_indices=None, clip_by_image=False, filter_out_image=False): |
| 11 | if isinstance(transforms, (tuple, list)): |
| 12 | transforms = T.TransformList(transforms) |
| 13 | # bbox is 1d (per-instance bounding box) |
| 14 | bbox = BoxMode.convert(annotation["bbox"], annotation["bbox_mode"], BoxMode.XYXY_ABS) |
| 15 | bbox = transforms.apply_box(np.array([bbox])) |
| 16 | if len(bbox) == 0: |
| 17 | return None |
| 18 | bbox = bbox[0] |
| 19 | if clip_by_image: |
| 20 | bbox = bbox.clip(min=(0, 0, 0, 0), max=list(image_size + image_size)[::-1]) |
| 21 | if not clip_by_image and filter_out_image: |
| 22 | min_size = 1.0 |
| 23 | transformed_bbox = bbox.clip(min=(0, 0, 0, 0), max=list(image_size + image_size)[::-1]) |
| 24 | if (transformed_bbox[2] - transformed_bbox[0] < min_size) or (transformed_bbox[3] - transformed_bbox[1] < min_size): |
| 25 | transformed_bbox |
| 26 | return None |
| 27 | |
| 28 | annotation["bbox"] = bbox |
| 29 | annotation["bbox_mode"] = BoxMode.XYXY_ABS |
| 30 | if "keypoints" in annotation: |
| 31 | keypoints = transform_keypoint_annotations( |
| 32 | annotation["keypoints"], transforms, image_size, keypoint_hflip_indices |
| 33 | ) |
| 34 | annotation["keypoints"] = keypoints |
| 35 | return annotation |
| 36 | |
| 37 | |
| 38 | def annotations_to_instances(annos, image_size): |