Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added spatial bbox helper #171

Closed
wants to merge 16 commits into from
31 changes: 30 additions & 1 deletion augly/image/utils/bboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import math
from typing import List, Optional, Tuple
from PIL import Image, ImageDraw
from typing import Callable, List, Optional, Tuple

import augly.image.utils as imutils
import numpy as np
membriux marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -444,6 +445,34 @@ def get_enclosing_bbox(
)


def spatial_bbox_helper(
bbox: Tuple, src_w: int, src_h: int, aug_function: Callable, **kwargs
) -> Tuple:
"""
Computes the bbox that encloses the transformed bbox in the image transformed by
`aug_function`. This helper can be used to compute the transformed bbox for any
augmentation which doesn't affect the color of the source image (e.g. any spatial
augmentation).
"""
dummy_image = Image.new("RGB", (src_w, src_h))
draw = ImageDraw.Draw(dummy_image)
draw.rectangle(
[bbox[0] * src_w, bbox[1] * src_h, bbox[2] * src_w, bbox[3] * src_h],
membriux marked this conversation as resolved.
Show resolved Hide resolved
fill="white",
)

aug_image = aug_function(dummy_image, **kwargs)
aug_w, aug_h = aug_image.size
array_image = np.array(aug_image)

white_y, white_x, _ = np.where(array_image > 0)
min_x, max_x = np.min(white_x), np.max(white_x)
min_y, max_y = np.min(white_y), np.max(white_y)

new_bbox = (min_x / aug_w, min_y / aug_h, max_x / aug_w, max_y / aug_h)
return new_bbox
membriux marked this conversation as resolved.
Show resolved Hide resolved


def vflip_bboxes_helper(bbox: Tuple, **kwargs) -> Tuple:
"""
Analogous to hflip, when the src image is vertically flipped, the bounding box also
Expand Down