albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
14.26k stars 1.65k forks source link

Question: possible to guarantee no keypoints are dropped in crop? #1128

Open vedal opened 2 years ago

vedal commented 2 years ago

Hi, thanks for a great and useful library!

Is there a way to ensure that no keypoints are dropped, similar to RandomSizedBBoxSafeCrop for bboxes? I tried making a bbox by hand from keypoint coordinates and use RandomSizedBBoxSafeCrop with both keypoint and bbox input, but I get the error: [NotImplementedError: Method apply_to_keypoint is not implemented in class RandomSizedBBoxSafeCrop]()

It makes perfect sense, but I'm wondering if I missed a similar feature for keypoints. I noticed the remove_invisible arg, but in my case I cannot lose keypoints

Best

Dipet commented 2 years ago

Unfortunately, I do not remember other such augs that could ensure keypoints don't be dropped. That RandomSizedBBoxSafeCrop does not implement apply_to_keypoint looks like a bug. As a hotfix you could create this aug and then assign method like this:

import numpy as np
import albumentations as A

img = np.empty([100, 100, 3], dtype=np.uint8)
bboxes = [[10, 10, 20, 20, 1]]
keypoints = [[15, 15]]

crop_aug = A.RandomSizedBBoxSafeCrop(50, 50)
pipeline = A.Compose(
    [crop_aug], bbox_params=A.BboxParams("pascal_voc"), keypoint_params=A.KeypointParams("xy")
)

def apply_to_keypoint(keypoint, crop_height=0, crop_width=0, h_start=0, w_start=0, rows=0, cols=0, **params):
    keypoint = A.keypoint_random_crop(keypoint, crop_height, crop_width, h_start, w_start, rows, cols)
    scale_x = crop_aug.width / crop_width
    scale_y = crop_aug.height / crop_height
    keypoint = A.keypoint_scale(keypoint, scale_x, scale_y)
    return keypoint

crop_aug.apply_to_keypoint = apply_to_keypoint

res = pipeline(image=img, keypoints=keypoints, bboxes=bboxes)
vedal commented 2 years ago

@Dipet That seems to work! Thanks a lot 💯

vedal commented 2 years ago

Should probably not close if its a bug