From 5ac3999aeb3947f24e33cb47fc30fdf3e58527ab Mon Sep 17 00:00:00 2001 From: Andrey Blazejuk Date: Thu, 17 Oct 2024 13:58:37 -0300 Subject: [PATCH] Validate that polygon has at least 3 vertices in geometry utils.py --- supervision/geometry/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/supervision/geometry/utils.py b/supervision/geometry/utils.py index 8a0ca35c..580dc0c5 100644 --- a/supervision/geometry/utils.py +++ b/supervision/geometry/utils.py @@ -15,6 +15,9 @@ def get_polygon_center(polygon: np.ndarray) -> Point: Returns: Point: The center of the polygon, represented as a Point object with x and y attributes. + + Raises: + ValueError: If the polygon has less than 3 vertices. Examples: ```python @@ -30,6 +33,9 @@ def get_polygon_center(polygon: np.ndarray) -> Point: # This is one of the 3 candidate algorithms considered for centroid calculation. # For a more detailed discussion, see PR #1084 and commit eb33176 + if len(polygon) < 3: + raise ValueError("Polygon must have at least 3 vertices.") + shift_polygon = np.roll(polygon, -1, axis=0) signed_areas = np.cross(polygon, shift_polygon) / 2 if signed_areas.sum() == 0: