Add unit tests for key_points - skeleton & annotators (#2099)
* Refactor key point annotator tests to use reusable fixtures for test clarity and maintainability * Apply suggestions from code review * fix(pre_commit): 🎨 auto format pre-commit hooks * Move `assert_image_mostly_same` helper to a shared module and update imports in tests * Update test/key_points/test_skeletons.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
pre-commit-ci[bot]
Copilot
parent
b25556dae9
commit
5ced8c4ea0
@@ -31,7 +31,7 @@ from supervision.annotators.utils import ColorLookup
|
||||
from supervision.detection.core import Detections
|
||||
from supervision.draw.color import Color
|
||||
from supervision.geometry.core import Position
|
||||
from test.helpers import _create_detections
|
||||
from test.helpers import _create_detections, assert_image_mostly_same
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -58,36 +58,6 @@ def gradient_image() -> np.ndarray:
|
||||
return image
|
||||
|
||||
|
||||
def assert_image_mostly_same(
|
||||
original: np.ndarray, annotated: np.ndarray, similarity_threshold: float = 0.9
|
||||
) -> None:
|
||||
"""
|
||||
Assert that the annotated image is mostly the same as the original.
|
||||
|
||||
Args:
|
||||
original: Original image
|
||||
annotated: Annotated image
|
||||
similarity_threshold:
|
||||
Minimum percentage of pixels that should be the same (0.0 to 1.0)
|
||||
"""
|
||||
# Check that images have the same shape
|
||||
assert original.shape == annotated.shape
|
||||
|
||||
# Calculate number of identical pixels
|
||||
identical_pixels = np.sum(np.all(original == annotated, axis=-1))
|
||||
total_pixels = original.shape[0] * original.shape[1]
|
||||
similarity = identical_pixels / total_pixels
|
||||
|
||||
# Check that at least similarity_threshold of pixels are identical
|
||||
assert similarity >= similarity_threshold, (
|
||||
f"Images are only {similarity:.1%} similar, "
|
||||
f"which is below the {similarity_threshold:.1%} threshold"
|
||||
)
|
||||
|
||||
# Check that the image is not completely identical
|
||||
assert not np.array_equal(original, annotated), "Images are completely identical"
|
||||
|
||||
|
||||
class TestBoxAnnotator:
|
||||
"""Tests for BoxAnnotator class"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user