albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

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

Open vedal opened this issue 3 years ago • 3 comments

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

vedal avatar Feb 23 '22 13:02 vedal

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)

Dipet avatar Feb 28 '22 09:02 Dipet

@Dipet That seems to work! Thanks a lot 💯

vedal avatar Mar 07 '22 12:03 vedal

Should probably not close if its a bug

vedal avatar Mar 07 '22 12:03 vedal