Skip to content

Commit

Permalink
Type area as float instead of int (#1320)
Browse files Browse the repository at this point in the history
Bounding boxes are collections of floats, see https://github.com/albumentations-team/albumentations/blob/3904898e3bfe476137a75cad6ae3d1015e3d468e/albumentations/core/transforms_interface.py#L29:

```python
BoxInternalType = Tuple[float, float, float, float]
BoxType = Union[BoxInternalType, Tuple[float, float, float, float, Any]]
```

Denormalizing does not (and should not) convert anything to `int`.
The resulting `x_min`, `y_min`, `x_max`, `y_max` are not `int` and almost never whole numbers.
Therefore, the value returned from `calculate_bbox_area` will almost never be a whole number.

In all cases, the returned value's type will be `float`.
  • Loading branch information
jangop committed Oct 25, 2022
1 parent 3904898 commit cb39781
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions albumentations/core/bbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,22 @@ def denormalize_bboxes(bboxes: Sequence[BoxType], rows: int, cols: int) -> List[
return [denormalize_bbox(bbox, rows, cols) for bbox in bboxes]


def calculate_bbox_area(bbox: BoxType, rows: int, cols: int) -> int:
"""Calculate the area of a bounding box in pixels.
def calculate_bbox_area(bbox: BoxType, rows: int, cols: int) -> float:
"""Calculate the area of a bounding box in (fractional) pixels.
Args:
bbox: A bounding box `(x_min, y_min, x_max, y_max)`.
rows: Image height.
cols: Image width.
Return:
Area of a bounding box in pixels.
Area in (fractional) pixels of the (denormalized) bounding box.
"""
bbox = denormalize_bbox(bbox, rows, cols)
x_min, y_min, x_max, y_max = bbox[:4]
area = (x_max - x_min) * (y_max - y_min)
return cast(int, area)
return area


def filter_bboxes_by_visibility(
Expand Down

0 comments on commit cb39781

Please sign in to comment.