Skip to content

Commit

Permalink
Added spatial bbox helper (#171)
Browse files Browse the repository at this point in the history
Summary:
- [x] I have read CONTRIBUTING.md to understand how to contribute to this repository :)

Computes the bbox that encloses a white box in a black background for any augmentation.

### Image
```bash
python -m unittest discover -s augly/tests/image_tests/ -p "*_test.py"
# Or `python -m unittest discover -s augly/tests/image_tests/ -p "*.py"` to run pytorch test too (must install `torchvision` to run)
```
```
Ran 82 tests in 53.014s

OK (skipped=5)
```

## Other testing

Colab notebook testing the bbox helper → https://colab.research.google.com/drive/1g_0I6f_bv4Wsna6l9jjZrOJ62a4dpu8U#scrollTo=yUczCe6FU9Bs

Pull Request resolved: #171

Reviewed By: jbitton

Differential Revision: D32951437

Pulled By: zpapakipos

fbshipit-source-id: 7b3532a5e679e08f60949dd29e8929806e1557e3
  • Loading branch information
membriux authored and facebook-github-bot committed Dec 9, 2021
1 parent e9afb6f commit a725275
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion augly/image/utils/bboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import math
from typing import List, Optional, Tuple
from typing import Callable, List, Optional, Tuple

import augly.image.utils as imutils
import numpy as np
from PIL import Image, ImageDraw


def crop_bboxes_helper(
Expand Down Expand Up @@ -444,6 +445,37 @@ def get_enclosing_bbox(
)


def spatial_bbox_helper(
bbox: Tuple[float, float, float, float],
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),
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)

return (min_x / aug_w, min_y / aug_h, max_x / aug_w, max_y / aug_h)


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

0 comments on commit a725275

Please sign in to comment.