Generate a CropTransform so that the cropping region contains the center of the given instance. Args: crop_size (tuple): h, w in pixels image_size (tuple): h, w instance (dict): an annotation dict of one instance, in Detectron2's dataset format.
(crop_size, image_size, instance)
| 505 | |
| 506 | |
| 507 | def gen_crop_transform_with_instance(crop_size, image_size, instance): |
| 508 | """ |
| 509 | Generate a CropTransform so that the cropping region contains |
| 510 | the center of the given instance. |
| 511 | |
| 512 | Args: |
| 513 | crop_size (tuple): h, w in pixels |
| 514 | image_size (tuple): h, w |
| 515 | instance (dict): an annotation dict of one instance, in Detectron2's |
| 516 | dataset format. |
| 517 | """ |
| 518 | crop_size = np.asarray(crop_size, dtype=np.int32) |
| 519 | bbox = BoxMode.convert(instance["bbox"], instance["bbox_mode"], BoxMode.XYXY_ABS) |
| 520 | center_yx = (bbox[1] + bbox[3]) * 0.5, (bbox[0] + bbox[2]) * 0.5 |
| 521 | assert ( |
| 522 | image_size[0] >= center_yx[0] and image_size[1] >= center_yx[1] |
| 523 | ), "The annotation bounding box is outside of the image!" |
| 524 | assert ( |
| 525 | image_size[0] >= crop_size[0] and image_size[1] >= crop_size[1] |
| 526 | ), "Crop size is larger than image size!" |
| 527 | |
| 528 | min_yx = np.maximum(np.floor(center_yx).astype(np.int32) - crop_size, 0) |
| 529 | max_yx = np.maximum(np.asarray(image_size, dtype=np.int32) - crop_size, 0) |
| 530 | max_yx = np.minimum(max_yx, np.ceil(center_yx).astype(np.int32)) |
| 531 | |
| 532 | y0 = np.random.randint(min_yx[0], max_yx[0] + 1) |
| 533 | x0 = np.random.randint(min_yx[1], max_yx[1] + 1) |
| 534 | return T.CropTransform(x0, y0, crop_size[1], crop_size[0]) |
| 535 | |
| 536 | |
| 537 | def check_metadata_consistency(key, dataset_names): |