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

Add convenient implementations of __len__ for CBAOIs #541

Merged
merged 1 commit into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion changelogs/master/20191113_iterable_augmentables.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simplified Access to Coordinates and Items in Augmentables #495
# Simplified Access to Coordinates and Items in Augmentables #495 #541

* Added module `imgaug.augmentables.base`.
* Added interface `imgaug.augmentables.base.IAugmentable`, implemented by
Expand All @@ -8,6 +8,10 @@
(keypoints, bounding boxes, polygons, line strings), e.g.
`bbsoi = BoundingBoxesOnImage(bbs, shape=...); for bb in bbsoi: ...`.
would iterate now over `bbs`.
* Added implementations of `__len__` methods to coordinate-based `*OnImage`
instances, e.g.
`bbsoi = BoundingBoxesOnImage(bbs, shape=...); print(len(bbsoi))`
would now print the number of bounding boxes in `bbsoi`.
* Added ability to iterate over coordinates of `BoundingBox` (top-left,
bottom-right), `Polygon` and `LineString` via `for xy in obj: ...`.
* Added ability to access coordinates of `BoundingBox`, `Polygon` and
Expand Down
11 changes: 11 additions & 0 deletions imgaug/augmentables/bbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,17 @@ def __iter__(self):
"""
return iter(self.bounding_boxes)

def __len__(self):
"""Get the number of items in this instance.

Returns
-------
int
Number of items in this instance.

"""
return len(self.items)

def __repr__(self):
return self.__str__()

Expand Down
11 changes: 11 additions & 0 deletions imgaug/augmentables/kps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,17 @@ def __iter__(self):
"""
return iter(self.items)

def __len__(self):
"""Get the number of items in this instance.

Returns
-------
int
Number of items in this instance.

"""
return len(self.items)

def __repr__(self):
return self.__str__()

Expand Down
11 changes: 11 additions & 0 deletions imgaug/augmentables/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,17 @@ def __iter__(self):
"""
return iter(self.line_strings)

def __len__(self):
"""Get the number of items in this instance.

Returns
-------
int
Number of items in this instance.

"""
return len(self.items)

def __repr__(self):
return self.__str__()

Expand Down
11 changes: 11 additions & 0 deletions imgaug/augmentables/polys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,17 @@ def __iter__(self):
"""
return iter(self.polygons)

def __len__(self):
"""Get the number of items in this instance.

Returns
-------
int
Number of items in this instance.

"""
return len(self.items)

def __repr__(self):
return self.__str__()

Expand Down
6 changes: 6 additions & 0 deletions test/augmentables/test_bbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,12 @@ def test___iter___empty(self):
i += 1
assert i == 0

def test___len__(self):
cbas = [ia.BoundingBox(x1=0, y1=0, x2=2, y2=2),
ia.BoundingBox(x1=1, y1=2, x2=3, y2=4)]
cbasoi = ia.BoundingBoxesOnImage(cbas, shape=(40, 50, 3))
assert len(cbasoi) == 2

def test_string_conversion(self):
bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51)
Expand Down
6 changes: 6 additions & 0 deletions test/augmentables/test_kps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,12 @@ def test___iter___empty(self):
i += 1
assert i == 0

def test___len__(self):
cbas = [ia.Keypoint(x=1, y=2),
ia.Keypoint(x=3, y=4)]
cbasoi = ia.KeypointsOnImage(cbas, shape=(40, 50, 3))
assert len(cbasoi) == 2

def test_string_conversion(self):
kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))
Expand Down
6 changes: 6 additions & 0 deletions test/augmentables/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,12 @@ def test___iter___empty(self):
i += 1
assert i == 0

def test___len__(self):
cbas = [ia.LineString([(0, 0), (1, 1)]),
ia.LineString([(1, 2), (3, 4)])]
cbasoi = ia.LineStringsOnImage(cbas, shape=(40, 50, 3))
assert len(cbasoi) == 2

def test___repr__(self):
def _func(obj):
return obj.__repr__()
Expand Down
8 changes: 8 additions & 0 deletions test/augmentables/test_polys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2839,6 +2839,14 @@ def test_with_zero_polygons(self):
assert i == 0


class TestPolygonsOnImage___len__(unittest.TestCase):
def test_with_two_polygons(self):
cbas = [ia.Polygon([(0, 0), (1, 0), (1, 1)]),
ia.Polygon([(1, 0), (2, 2), (1.5, 3)])]
cbasoi = ia.PolygonsOnImage(cbas, shape=(40, 50, 3))
assert len(cbasoi) == 2


class TestPolygonsOnImage___repr___and___str__(unittest.TestCase):
def test_with_zero_polygons(self):
poly_oi = ia.PolygonsOnImage([], shape=(10, 11, 3))
Expand Down