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
36 changes: 35 additions & 1 deletion augly/image/utils/bboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import math
from typing import List, Optional, Tuple
from PIL import Image
membriux marked this conversation as resolved.
Show resolved Hide resolved
from typing import Callable, List, Optional, Tuple

import augly.image.utils as imutils
import numpy as np


def create_test_image(w: int, h: int, bbox: Tuple) -> Image.Image:
"""
Create dummy test image to help spatial_bbox_helper.
"""
image = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(image)
draw.rectangle([bbox[0] * w, bbox[1] * h, bbox[2] * w, bbox[3] * h], fill="white")
return image


def crop_bboxes_helper(
bbox: Tuple, x1: float, y1: float, x2: float, y2: float, **kwargs
) -> Tuple:
Expand Down Expand Up @@ -444,6 +455,29 @@ 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 = create_test_image(w=src_w, h=src_h, bbox=bbox)
membriux marked this conversation as resolved.
Show resolved Hide resolved

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