Skip to content

Commit

Permalink
assert bbox values at construction
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnv1 committed Oct 26, 2022
1 parent 354ad05 commit 823f19b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lapixdl/formats/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class BBox:
cls: int
score: float | None = None

def __post_init__(self):
if self.upper_left_x < 0 or self.upper_left_y < 0:
raise ValueError(f'The upper left (x, y) should be positive values. Got ({self.upper_left_x}, {self.upper_left_y})')

if self.width <= 0:
raise ValueError(f'The width should be bigger than zero. Got {self.width}')

if self.height <= 0:
raise ValueError(f'The height should be bigger than zero. Got {self.height}')

@property
def upper_left_point(self) -> tuple[int, int]:
"""Tuple[int, int]: (X,Y) of the upper left point of the Bounding Box."""
Expand Down
14 changes: 14 additions & 0 deletions tests/formats/annotation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def test_bbox():
assert bbox.slice_y == slice(0, 14)


def test_invalid_bbox():
with pytest.raises(ValueError):
BBox(0, -1, 1, 1, 0)

with pytest.raises(ValueError):
BBox(-1, 0, 1, 1, 0)

with pytest.raises(ValueError):
BBox(0, 0, 0, 1, 0)

with pytest.raises(ValueError):
BBox(0, 0, 1, 0, 0)


def test_bbox_intersection_and_union_area_with():
bbox_A = BBox(0, 0, 10, 15, 0)
bbox_B = BBox(5, 5, 20, 25, 0)
Expand Down

0 comments on commit 823f19b

Please sign in to comment.