Skip to content

Commit 388fcc4

Browse files
authored
✨ add getting bounding box for multiple polygons (#100)
1 parent 134b0f2 commit 388fcc4

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

mindee/fields/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Dict, List, Optional, TypeVar
22

3-
from mindee.geometry import Polygon, get_bbox_as_polygon
3+
from mindee.geometry import Polygon, get_bounding_box
44

55
TypePrediction = Dict[str, Any]
66

@@ -52,7 +52,7 @@ def _set_bbox(self, abstract_prediction: TypePrediction) -> None:
5252
except KeyError:
5353
pass
5454
if self.polygon:
55-
self.bbox = get_bbox_as_polygon(self.polygon)
55+
self.bbox = get_bounding_box(self.polygon)
5656

5757
def __eq__(self, other: Any) -> bool:
5858
if not isinstance(other, Field):

mindee/geometry.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
Point = Tuple[float, float]
66
Polygon = Sequence[Point]
7-
BoundingBox = Tuple[float, float, float, float]
7+
BBox = Tuple[float, float, float, float]
88
Quadrilateral = Tuple[Point, Point, Point, Point]
99

1010

11-
def get_bbox_as_polygon(polygon: Polygon) -> Quadrilateral:
11+
def get_bounding_box(polygon: Polygon) -> Quadrilateral:
1212
"""
1313
Given a sequence of points, calculate a polygon that encompasses all points.
1414
@@ -19,7 +19,7 @@ def get_bbox_as_polygon(polygon: Polygon) -> Quadrilateral:
1919
return (x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)
2020

2121

22-
def get_bbox(polygon: Polygon) -> BoundingBox:
22+
def get_bbox(polygon: Polygon) -> BBox:
2323
"""
2424
Given a list of points, calculate a bounding box that encompasses all points.
2525
@@ -33,6 +33,20 @@ def get_bbox(polygon: Polygon) -> BoundingBox:
3333
return x_min, y_min, x_max, y_max
3434

3535

36+
def get_bounding_box_for_polygons(vertices: Sequence[Polygon]) -> Quadrilateral:
37+
"""
38+
Given a list of polygons, calculate a polygon that encompasses all polygons.
39+
40+
:param vertices: List of polygons
41+
:return: Quadrilateral
42+
"""
43+
y_min = min(y for v in vertices for _, y in v)
44+
y_max = max(y for v in vertices for _, y in v)
45+
x_min = min(x for v in vertices for x, _ in v)
46+
x_max = max(x for v in vertices for x, _ in v)
47+
return (x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)
48+
49+
3650
def get_centroid(polygon: Polygon) -> Point:
3751
"""
3852
Get the central point (centroid) given a list of points.

tests/test_geometry.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ def test_bbox(rectangle_a, rectangle_b, quadrangle_a):
2727
assert geometry.get_bbox(quadrangle_a) == (0.205, 0.407, 0.381, 0.43)
2828

2929

30-
def test_bbox_polygon(rectangle_a, rectangle_b, quadrangle_a):
31-
assert geometry.get_bbox_as_polygon(rectangle_a) == (
30+
def test_bounding_box_single_polygon(rectangle_a, rectangle_b, quadrangle_a):
31+
assert geometry.get_bounding_box(rectangle_a) == (
3232
(0.123, 0.53),
3333
(0.175, 0.53),
3434
(0.175, 0.546),
3535
(0.123, 0.546),
3636
)
37-
assert geometry.get_bbox_as_polygon(rectangle_b) == (
37+
assert geometry.get_bounding_box(rectangle_b) == (
3838
(0.124, 0.535),
3939
(0.19, 0.535),
4040
(0.19, 0.546),
4141
(0.124, 0.546),
4242
)
43-
assert geometry.get_bbox_as_polygon(quadrangle_a) == (
43+
assert geometry.get_bounding_box(quadrangle_a) == (
4444
(0.205, 0.407),
4545
(0.381, 0.407),
4646
(0.381, 0.43),
@@ -80,3 +80,12 @@ def test_is_point_in_polygon_x(rectangle_a, rectangle_b, quadrangle_a):
8080

8181
def test_get_centroid(rectangle_a):
8282
assert geometry.get_centroid(rectangle_a) == (0.149, 0.538)
83+
84+
85+
def test_bounding_box_several_polygons(rectangle_b, quadrangle_a):
86+
assert geometry.get_bounding_box_for_polygons((rectangle_b, quadrangle_a)) == (
87+
(0.124, 0.407),
88+
(0.381, 0.407),
89+
(0.381, 0.546),
90+
(0.124, 0.546),
91+
)

0 commit comments

Comments
 (0)