Files
supervision/tests/draw/test_utils.py
T
97f5c0ca53 perf(annotators): skip corner circles when drawing square label backgrounds (#2346)
Both rounded-rectangle helpers (LabelAnnotator.draw_rounded_rectangle and the
public draw_rounded_rectangle in draw/utils) always drew two rectangles plus
four corner circles, even when border_radius is 0, which is the default for
LabelAnnotator and VertexLabelAnnotator. With a zero radius that is six cv2
calls per label per frame (the four circles are zero-radius no-ops) where one
fill rectangle does the same thing.

Add a square-corner fast path to both helpers. Output is pixel identical; only
the redundant calls go away. On a 1080p frame with 100 labels LabelAnnotator
drops from ~2.1 ms to ~1.3 ms (about 1.6x), and the rounded-rectangle call
itself is ~2.8x faster at radius 0. The radius > 0 path is unchanged.

Adds tests pinning square output to a plain rectangle for both helpers (the
public draw/utils function had no tests before).

- Rename LabelAnnotator.draw_rounded_rectangle to _draw_rounded_rectangle
  (accidentally public static method — now signals internal)
- Expand draw/utils.py border_radius docstring: document <= 0 and
  clamp-to-zero fast-path behaviour
- Add crash-era comment to both test files: border_radius < 0 previously
  raised cv2.error; fast path silently draws square corners instead
- Add clamped-to-zero test in both test files: positive radius on a
  1px-wide box clamps to 0 and triggers the fast path
- Strengthen positive-radius assertion: full center-row check + all four
  corners unpainted (replaces two-pixel spot check)
- Add pytest.param(id=) slugs to all parametrize decorators per
  CONTRIBUTING.md convention
- Add Google-style docstring with Args, Returns, Example to
  `LabelAnnotator.draw_rounded_rectangle` (was undocumented @staticmethod)
- Rename `testdraw_*` → `test_draw_*` in `TestLabelAnnotator` to restore
  consistent test naming broken by the earlier private-rename commit
- Expand `draw/utils.draw_rounded_rectangle` border_radius docstring:
  note that negative values previously raised `cv2.error` and now draw
  square corners silently; drop "as a fast path" implementation detail
- Add `Example:` block to `draw/utils.draw_rounded_rectangle`

---------

Co-authored-by: jirka <6035284+Borda@users.noreply.github.com>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
2026-07-01 01:25:25 +02:00

138 lines
4.6 KiB
Python

import cv2
import numpy as np
import pytest
from supervision.draw.color import Color
from supervision.draw.utils import draw_image, draw_rounded_rectangle
from supervision.geometry.core import Rect
def test_draw_image_invalid_path_raises_oserror(tmp_path) -> None:
"""Existing but undecodable image files raise OSError."""
invalid_image_path = tmp_path / "invalid_image.dat"
invalid_image_path.write_bytes(b"not an image")
scene = np.zeros((100, 100, 3), dtype=np.uint8)
rect = Rect(x=0, y=0, width=100, height=100)
with pytest.raises(OSError, match="Could not decode image path"):
draw_image(
scene=scene,
image=str(invalid_image_path),
opacity=1.0,
rect=rect,
)
def test_draw_image_valid_image(tmp_path) -> None:
"""Valid image files are decoded and drawn onto the scene."""
image = np.zeros((100, 100, 3), dtype=np.uint8)
image_path = tmp_path / "image.png"
cv2.imwrite(str(image_path), image)
scene = np.zeros((100, 100, 3), dtype=np.uint8)
rect = Rect(x=0, y=0, width=100, height=100)
result = draw_image(
scene=scene,
image=str(image_path),
opacity=1.0,
rect=rect,
)
assert isinstance(result, np.ndarray)
def test_draw_image_grayscale_file_raises_value_error(tmp_path) -> None:
"""Grayscale image files raise ValueError before channel access."""
image = np.zeros((100, 100), dtype=np.uint8)
image_path = tmp_path / "grayscale.png"
cv2.imwrite(str(image_path), image)
scene = np.zeros((100, 100, 3), dtype=np.uint8)
rect = Rect(x=0, y=0, width=100, height=100)
with pytest.raises(ValueError, match="3 or 4 channels"):
draw_image(
scene=scene,
image=str(image_path),
opacity=1.0,
rect=rect,
)
def test_draw_image_grayscale_array_raises_value_error() -> None:
"""Grayscale image arrays raise ValueError before channel access."""
image = np.zeros((100, 100), dtype=np.uint8)
scene = np.zeros((100, 100, 3), dtype=np.uint8)
rect = Rect(x=0, y=0, width=100, height=100)
with pytest.raises(ValueError, match="3 or 4 channels"):
draw_image(
scene=scene,
image=image,
opacity=1.0,
rect=rect,
)
@pytest.mark.parametrize(
"border_radius",
[
pytest.param(0, id="radius-zero"),
pytest.param(-5, id="radius-negative"),
],
)
def test_draw_rounded_rectangle_square_matches_plain_rectangle(
border_radius: int,
) -> None:
"""Non-positive border_radius fills exactly the same pixels as a plain box.
For border_radius < 0: previously raised cv2.error: radius >= 0 in
function 'circle'; fast path now silently draws square corners instead.
"""
rect = Rect(x=20, y=30, width=120, height=80)
scene = np.full((150, 200, 3), 17, dtype=np.uint8)
result = draw_rounded_rectangle(scene.copy(), rect, Color.RED, border_radius)
expected = scene.copy()
expected[30:111, 20:141] = Color.RED.as_bgr()
assert np.array_equal(result, expected)
def test_draw_rounded_rectangle_clamped_to_zero_acts_as_square() -> None:
"""A positive border_radius clamped to 0 by a degenerate box draws square corners.
1px-wide box: min(10, 1 // 2) = min(10, 0) = 0 → fast path fires even
though the caller passed a positive radius.
"""
rect = Rect(x=10, y=10, width=1, height=20)
scene = np.full((50, 50, 3), 17, dtype=np.uint8)
result = draw_rounded_rectangle(scene.copy(), rect, Color.RED, border_radius=10)
expected = scene.copy()
expected[10:31, 10:12] = Color.RED.as_bgr()
assert np.array_equal(result, expected)
def test_draw_rounded_rectangle_positive_radius_rounds_corners() -> None:
"""A positive border radius leaves the extreme corners unpainted."""
rect = Rect(x=20, y=30, width=120, height=80)
scene = np.zeros((150, 200, 3), dtype=np.uint8)
result = draw_rounded_rectangle(scene.copy(), rect, Color.RED, border_radius=15)
red = np.array(Color.RED.as_bgr(), dtype=np.uint8)
bg = np.zeros(3, dtype=np.uint8)
# center row is fully filled between the inner rectangle bounds
center_y = (30 + 110) // 2 # 70; 40px from each y edge, well past border_radius=15
assert np.all(result[center_y, 35:126] == red)
# all four extreme corners stay background (clipped by border_radius=15)
assert np.array_equal(result[30, 20], bg) # top-left
assert np.array_equal(result[30, 140], bg) # top-right
assert np.array_equal(result[110, 20], bg) # bottom-left
assert np.array_equal(result[110, 140], bg) # bottom-right