Files
wehub-resource-sync 9194ef5abd
Docs/Test Workflow / Test docs build (push) Failing after 0s
Check links & references / links-check (push) Failing after 1s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.10) (push) Failing after 0s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.11) (push) Failing after 0s
PR Conflict Labeler / main (push) Failing after 2s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.12) (push) Failing after 2s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.13) (push) Failing after 0s
Pytest/Test Workflow / Build this Package (push) Failing after 5s
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.10) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.11) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.12) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.13) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.10) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.11) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.12) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.13) (push) Has been cancelled
Pytest/Test Workflow / testing-guardian (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:06:10 +08:00

86 lines
2.7 KiB
Python

import numpy as np
import pytest
from supervision.geometry.core import Point
from supervision.geometry.utils import get_polygon_center
def generate_test_polygon(n: int) -> np.ndarray:
"""
Generate a semicircle with a given number of points.
Parameters:
n (int): amount of points in polygon
Returns:
Polygon: test polygon in the form of a semicircle.
Examples:
```python
from supervision.geometry.utils import get_polygon_center
import numpy as np
test_polygon = generate_test_data(1000)
get_polygon_center(test_polygon)
Point(x=500, y=1212)
```
"""
r: int = n // 2
x_axis = np.linspace(0, 2 * r, n)
y_axis = (r**2 - (x_axis - r) ** 2) ** 0.5 + 2 * r
polygon = np.array([x_axis, y_axis]).T
return polygon
@pytest.mark.parametrize(
("polygon", "expected_result"),
[
(generate_test_polygon(10), Point(x=5.0, y=12.0)),
(generate_test_polygon(50), Point(x=25.0, y=61.0)),
(generate_test_polygon(100), Point(x=50.0, y=121.0)),
(generate_test_polygon(1000), Point(x=500.0, y=1212.0)),
(generate_test_polygon(3000), Point(x=1500.0, y=3637.0)),
(generate_test_polygon(10000), Point(x=5000.0, y=12122.0)),
(generate_test_polygon(20000), Point(x=10000.0, y=24244.0)),
(generate_test_polygon(50000), Point(x=25000.0, y=60610.0)),
],
)
def test_get_polygon_center(polygon: np.ndarray, expected_result: Point) -> None:
"""
Verify that get_polygon_center correctly calculates the centroid of a polygon.
Scenario: Calculating the center point (centroid) of various polygons.
Expected: The returned `Point` correctly represents the average position of all
polygon vertices, which is used for placing labels or markers at the center
of detected objects.
"""
result = get_polygon_center(polygon)
assert result == expected_result
def test_get_polygon_center_no_deprecation_warning() -> None:
"""Regression for #2384: get_polygon_center must not fire DeprecationWarning."""
import warnings
polygon = np.array([[0, 0], [0, 2], [2, 2], [2, 0]], dtype=float)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
get_polygon_center(polygon=polygon)
def test_get_polygon_center_does_not_call_np_cross(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Guard against reintroducing np.cross, deprecated for 2-D input in NumPy 2.0."""
def _raise(*args, **kwargs):
raise AssertionError("np.cross must not be called on 2-D vectors")
monkeypatch.setattr(np, "cross", _raise)
result = get_polygon_center(generate_test_polygon(100))
assert result == Point(x=50.0, y=121.0)