* Detections.from_inference works on RLE-encoded masks * Apply suggestions from code review * fix: harden RLE handling in from_inference and decoder * lint: fix cv2.fillPoly color type in polygon_to_mask * fix: resize RLE mask to image dims when size mismatches * fix: pass RLE counts directly in coco_annotations_to_masks * test: document mixed RLE + box-only batch misalignment * test: add compressed RLE iscrowd case to coco_annotations_to_detections * fix: cast polygon mask to bool in process_roboflow_result * fix: log warning when RLE decode fails in process_roboflow_result * fix: replace assert with ValueError in rle_to_mask * test: add bytes invalid UTF-8 case to rle_to_mask tests * docs: note rle_to_mask dtype change from uint8 to bool in changelog * refactor: tighten rle_to_mask NDArray input type to np.integer[Any] * refactor: drop mask_to_rle overloads; cast at call site * docs: clarify COCO column-major RLE order in rle_to_mask/mask_to_rle * refactor: update @deprecated annotations and docstrings for mask_to_rle/rle_to_mask; add pydeprecate dependency * refactor: replace mask_to_rle body with `void` function to suppress unused argument warnings --------- Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
536 lines
17 KiB
Python
536 lines
17 KiB
Python
from __future__ import annotations
|
|
|
|
from contextlib import ExitStack as DoesNotRaise
|
|
|
|
import numpy as np
|
|
import numpy.typing as npt
|
|
import pytest
|
|
|
|
from supervision.detection.utils.converters import (
|
|
_decode_coco_rle_string,
|
|
_encode_coco_rle_string,
|
|
mask_to_rle,
|
|
rle_to_mask,
|
|
xcycwh_to_xyxy,
|
|
xywh_to_xyxy,
|
|
xyxy_to_mask,
|
|
xyxy_to_xcycarh,
|
|
xyxy_to_xywh,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("xywh", "expected_result"),
|
|
[
|
|
(np.array([[10, 20, 30, 40]]), np.array([[10, 20, 40, 60]])), # standard case
|
|
(np.array([[0, 0, 0, 0]]), np.array([[0, 0, 0, 0]])), # zero size bounding box
|
|
(
|
|
np.array([[50, 50, 100, 100]]),
|
|
np.array([[50, 50, 150, 150]]),
|
|
), # large bounding box
|
|
(
|
|
np.array([[-10, -20, 30, 40]]),
|
|
np.array([[-10, -20, 20, 20]]),
|
|
), # negative coordinates
|
|
(np.array([[50, 50, 0, 30]]), np.array([[50, 50, 50, 80]])), # zero width
|
|
(np.array([[50, 50, 20, 0]]), np.array([[50, 50, 70, 50]])), # zero height
|
|
(np.array([]).reshape(0, 4), np.array([]).reshape(0, 4)), # empty array
|
|
],
|
|
)
|
|
def test_xywh_to_xyxy(xywh: np.ndarray, expected_result: np.ndarray) -> None:
|
|
result = xywh_to_xyxy(xywh)
|
|
np.testing.assert_array_equal(result, expected_result)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("xyxy", "expected_result"),
|
|
[
|
|
(np.array([[10, 20, 40, 60]]), np.array([[10, 20, 30, 40]])), # standard case
|
|
(np.array([[0, 0, 0, 0]]), np.array([[0, 0, 0, 0]])), # zero size bounding box
|
|
(
|
|
np.array([[50, 50, 150, 150]]),
|
|
np.array([[50, 50, 100, 100]]),
|
|
), # large bounding box
|
|
(
|
|
np.array([[-10, -20, 20, 20]]),
|
|
np.array([[-10, -20, 30, 40]]),
|
|
), # negative coordinates
|
|
(np.array([[50, 50, 50, 80]]), np.array([[50, 50, 0, 30]])), # zero width
|
|
(np.array([[50, 50, 70, 50]]), np.array([[50, 50, 20, 0]])), # zero height
|
|
(np.array([]).reshape(0, 4), np.array([]).reshape(0, 4)), # empty array
|
|
],
|
|
)
|
|
def test_xyxy_to_xywh(xyxy: np.ndarray, expected_result: np.ndarray) -> None:
|
|
result = xyxy_to_xywh(xyxy)
|
|
np.testing.assert_array_equal(result, expected_result)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("xyxy", "expected_result"),
|
|
[
|
|
# Empty and zero cases
|
|
(np.array([]).reshape(0, 4), np.array([]).reshape(0, 4)), # empty array
|
|
(
|
|
np.array([[0, 0, 0, 0]]),
|
|
np.array([[0, 0, 0.0, 0]]),
|
|
), # zero size bounding box
|
|
(
|
|
np.array([[10, 10, 10, 10]]),
|
|
np.array([[10, 10, 0.0, 0]]),
|
|
), # point (x1=x2, y1=y2)
|
|
# Zero width/height cases
|
|
(np.array([[50, 50, 80, 50]]), np.array([[65, 50, 0.0, 0]])), # zero height
|
|
(np.array([[50, 50, 50, 80]]), np.array([[50, 65, 0.0, 30]])), # zero width
|
|
# Standard cases
|
|
(np.array([[10, 20, 40, 60]]), np.array([[25, 40, 0.75, 40]])), # standard case
|
|
(
|
|
np.array([[-30, -40, -10, -20]]),
|
|
np.array([[-20, -30, 1.0, 20]]),
|
|
), # all negative values
|
|
(
|
|
np.array([[0.1, 0.2, 0.4, 0.6]]),
|
|
np.array([[0.25, 0.4, 0.75, 0.4]]),
|
|
), # values between 0-1
|
|
# Different aspect ratios
|
|
(
|
|
np.array([[10, 20, 50, 100]]),
|
|
np.array([[30, 60, 0.5, 80]]),
|
|
), # tall rectangle (height > width)
|
|
(
|
|
np.array([[20, 10, 100, 50]]),
|
|
np.array([[60, 30, 2.0, 40]]),
|
|
), # wide rectangle (width > height)
|
|
(
|
|
np.array([[50, 50, 150, 150]]),
|
|
np.array([[100, 100, 1.0, 100]]),
|
|
), # height == width
|
|
# Multiple boxes in one array
|
|
(
|
|
np.array([[0, 0, 0, 0], [10, 20, 40, 60]]),
|
|
np.array([[0, 0, 0.0, 0], [25, 40, 0.75, 40]]),
|
|
), # one zero-sized box and one normal box
|
|
],
|
|
)
|
|
def test_xyxy_to_xcycarh(xyxy: np.ndarray, expected_result: np.ndarray) -> None:
|
|
result = xyxy_to_xcycarh(xyxy)
|
|
np.testing.assert_allclose(result, expected_result)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("xcycwh", "expected_result"),
|
|
[
|
|
(np.array([[50, 50, 20, 30]]), np.array([[40, 35, 60, 65]])), # standard case
|
|
(np.array([[0, 0, 0, 0]]), np.array([[0, 0, 0, 0]])), # zero size bounding box
|
|
(
|
|
np.array([[50, 50, 100, 100]]),
|
|
np.array([[0, 0, 100, 100]]),
|
|
), # large bounding box centered at (50, 50)
|
|
(
|
|
np.array([[-10, -10, 20, 30]]),
|
|
np.array([[-20, -25, 0, 5]]),
|
|
), # negative coordinates
|
|
(np.array([[50, 50, 0, 30]]), np.array([[50, 35, 50, 65]])), # zero width
|
|
(np.array([[50, 50, 20, 0]]), np.array([[40, 50, 60, 50]])), # zero height
|
|
(np.array([]).reshape(0, 4), np.array([]).reshape(0, 4)), # empty array
|
|
],
|
|
)
|
|
def test_xcycwh_to_xyxy(xcycwh: np.ndarray, expected_result: np.ndarray) -> None:
|
|
result = xcycwh_to_xyxy(xcycwh)
|
|
np.testing.assert_array_equal(result, expected_result)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("boxes", "resolution_wh", "expected"),
|
|
[
|
|
# 0) Empty input
|
|
(
|
|
np.array([], dtype=float).reshape(0, 4),
|
|
(5, 4),
|
|
np.array([], dtype=bool).reshape(0, 4, 5),
|
|
),
|
|
# 1) Single pixel box
|
|
(
|
|
np.array([[2, 1, 2, 1]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[False, False, False, False, False],
|
|
[False, False, True, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 2) Horizontal line, inclusive bounds
|
|
(
|
|
np.array([[1, 2, 3, 2]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, True, True, True, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 3) Vertical line, inclusive bounds
|
|
(
|
|
np.array([[3, 0, 3, 2]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[False, False, False, True, False],
|
|
[False, False, False, True, False],
|
|
[False, False, False, True, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 4) Proper rectangle fill
|
|
(
|
|
np.array([[1, 1, 3, 2]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[False, False, False, False, False],
|
|
[False, True, True, True, False],
|
|
[False, True, True, True, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 5) Negative coordinates clipped to [0, 0]
|
|
(
|
|
np.array([[-2, -1, 1, 1]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[True, True, False, False, False],
|
|
[True, True, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 6) Overflow coordinates clipped to width-1 and height-1
|
|
(
|
|
np.array([[3, 2, 10, 10]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, True, True],
|
|
[False, False, False, True, True],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 7) Invalid box where max < min after ints, mask stays empty
|
|
(
|
|
np.array([[3, 2, 1, 4]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 8) Fractional coordinates are floored by int conversion
|
|
# (0.2,0.2)-(2.8,1.9) -> (0,0)-(2,1)
|
|
(
|
|
np.array([[0.2, 0.2, 2.8, 1.9]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
[
|
|
[True, True, True, False, False],
|
|
[True, True, True, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
]
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
# 9) Multiple boxes, separate masks
|
|
(
|
|
np.array([[0, 0, 1, 0], [2, 1, 4, 3]], dtype=float),
|
|
(5, 4),
|
|
np.array(
|
|
[
|
|
# Box 0: row 0, cols 0..1
|
|
[
|
|
[True, True, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
[False, False, False, False, False],
|
|
],
|
|
# Box 1: rows 1..3, cols 2..4
|
|
[
|
|
[False, False, False, False, False],
|
|
[False, False, True, True, True],
|
|
[False, False, True, True, True],
|
|
[False, False, True, True, True],
|
|
],
|
|
],
|
|
dtype=bool,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
def test_xyxy_to_mask(boxes: np.ndarray, resolution_wh, expected: np.ndarray) -> None:
|
|
result = xyxy_to_mask(boxes, resolution_wh)
|
|
assert result.dtype == np.bool_
|
|
assert result.shape == expected.shape
|
|
np.testing.assert_array_equal(result, expected)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"counts",
|
|
[
|
|
[5, 2, 2, 2, 5],
|
|
[0, 16],
|
|
[9],
|
|
[0, 5, 5, 5, 5, 5],
|
|
[6, 3, 2, 1, 1, 1, 2, 3, 6],
|
|
[3, 1, 2, 4, 2, 1, 3],
|
|
],
|
|
)
|
|
def test_coco_rle_encode_decode_round_trip(counts: list[int]) -> None:
|
|
encoded = _encode_coco_rle_string(counts)
|
|
decoded = _decode_coco_rle_string(encoded)
|
|
assert decoded == counts
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mask", "compressed", "expected_rle", "exception"),
|
|
[
|
|
(
|
|
np.zeros((3, 3)).astype(bool),
|
|
False,
|
|
[9],
|
|
DoesNotRaise(),
|
|
), # mask with background only (mask with only False values)
|
|
(
|
|
np.ones((3, 3)).astype(bool),
|
|
False,
|
|
[0, 9],
|
|
DoesNotRaise(),
|
|
), # mask with foreground only (mask with only True values)
|
|
(
|
|
np.array(
|
|
[
|
|
[0, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 0],
|
|
[0, 1, 0, 1, 0],
|
|
[0, 1, 1, 1, 0],
|
|
[0, 0, 0, 0, 0],
|
|
]
|
|
).astype(bool),
|
|
False,
|
|
[6, 3, 2, 1, 1, 1, 2, 3, 6],
|
|
DoesNotRaise(),
|
|
), # mask where foreground object has hole
|
|
(
|
|
np.array(
|
|
[
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
]
|
|
).astype(bool),
|
|
False,
|
|
[0, 5, 5, 5, 5, 5],
|
|
DoesNotRaise(),
|
|
), # mask where foreground consists of 3 separate components
|
|
(
|
|
np.array(
|
|
[
|
|
[False, False, False, False],
|
|
[False, True, True, False],
|
|
[False, True, True, False],
|
|
[False, False, False, False],
|
|
]
|
|
),
|
|
True,
|
|
"52203",
|
|
DoesNotRaise(),
|
|
), # compressed RLE string
|
|
(
|
|
np.array([[[]]]).astype(bool),
|
|
False,
|
|
None,
|
|
pytest.raises(AssertionError, match="Input mask must be 2D"),
|
|
), # raises AssertionError because mask dimensionality is not 2D
|
|
(
|
|
np.array([[]]).astype(bool),
|
|
False,
|
|
None,
|
|
pytest.raises(AssertionError, match="Input mask cannot be empty"),
|
|
), # raises AssertionError because mask is empty
|
|
],
|
|
)
|
|
def test_mask_to_rle(
|
|
mask: npt.NDArray[np.bool_],
|
|
compressed: bool,
|
|
expected_rle: list[int] | str | None,
|
|
exception: Exception,
|
|
) -> None:
|
|
with exception:
|
|
result = mask_to_rle(mask=mask, compressed=compressed)
|
|
assert result == expected_rle
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("rle", "resolution_wh", "expected_mask", "exception"),
|
|
[
|
|
(
|
|
np.array([9]),
|
|
[3, 3],
|
|
np.zeros((3, 3)).astype(bool),
|
|
DoesNotRaise(),
|
|
), # mask with background only (mask with only False values); rle as array
|
|
(
|
|
[9],
|
|
[3, 3],
|
|
np.zeros((3, 3)).astype(bool),
|
|
DoesNotRaise(),
|
|
), # mask with background only (mask with only False values); rle as list
|
|
(
|
|
np.array([0, 9]),
|
|
[3, 3],
|
|
np.ones((3, 3)).astype(bool),
|
|
DoesNotRaise(),
|
|
), # mask with foreground only (mask with only True values)
|
|
(
|
|
np.array([6, 3, 2, 1, 1, 1, 2, 3, 6]),
|
|
[5, 5],
|
|
np.array(
|
|
[
|
|
[0, 0, 0, 0, 0],
|
|
[0, 1, 1, 1, 0],
|
|
[0, 1, 0, 1, 0],
|
|
[0, 1, 1, 1, 0],
|
|
[0, 0, 0, 0, 0],
|
|
]
|
|
).astype(bool),
|
|
DoesNotRaise(),
|
|
), # mask where foreground object has hole
|
|
(
|
|
np.array([0, 5, 5, 5, 5, 5]),
|
|
[5, 5],
|
|
np.array(
|
|
[
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
[1, 0, 1, 0, 1],
|
|
]
|
|
).astype(bool),
|
|
DoesNotRaise(),
|
|
), # mask where foreground consists of 3 separate components
|
|
(
|
|
np.array([0, 5, 5, 5, 5, 5]),
|
|
[2, 2],
|
|
None,
|
|
pytest.raises(ValueError, match="sum of the number of pixels in the RLE"),
|
|
), # raises ValueError because number of pixels in RLE does not match
|
|
# number of pixels in expected mask (width x height).
|
|
(
|
|
b"3124OM1",
|
|
[4, 4],
|
|
np.array(
|
|
[
|
|
[0, 0, 1, 1],
|
|
[0, 0, 1, 1],
|
|
[0, 1, 1, 0],
|
|
[1, 1, 0, 0],
|
|
]
|
|
).astype(bool),
|
|
DoesNotRaise(),
|
|
), # compressed RLE bytes
|
|
(
|
|
"52203",
|
|
[4, 4],
|
|
np.array(
|
|
[
|
|
[0, 0, 0, 0],
|
|
[0, 1, 1, 0],
|
|
[0, 1, 1, 0],
|
|
[0, 0, 0, 0],
|
|
]
|
|
).astype(bool),
|
|
DoesNotRaise(),
|
|
), # compressed RLE string
|
|
(
|
|
"!",
|
|
[4, 4],
|
|
None,
|
|
pytest.raises(ValueError, match="Malformed compressed RLE string"),
|
|
), # malformed compressed RLE string with invalid character
|
|
(
|
|
"52P",
|
|
[4, 4],
|
|
None,
|
|
pytest.raises(ValueError, match="Malformed compressed RLE string"),
|
|
), # malformed compressed RLE: unterminated continuation byte
|
|
(
|
|
b"\xff\xfe",
|
|
[4, 4],
|
|
None,
|
|
pytest.raises(UnicodeDecodeError),
|
|
), # bytes with invalid UTF-8 sequence raises UnicodeDecodeError
|
|
],
|
|
)
|
|
def test_rle_to_mask(
|
|
rle: npt.NDArray[np.int_],
|
|
resolution_wh: tuple[int, int],
|
|
expected_mask: npt.NDArray[np.bool_],
|
|
exception: Exception,
|
|
) -> None:
|
|
with exception:
|
|
result = rle_to_mask(rle=rle, resolution_wh=resolution_wh)
|
|
assert np.all(result == expected_mask)
|
|
|
|
|
|
def test_mask_rle_compressed_round_trip() -> None:
|
|
mask = np.array(
|
|
[
|
|
[False, False, False, False],
|
|
[False, True, True, False],
|
|
[False, True, True, False],
|
|
[False, False, False, False],
|
|
]
|
|
)
|
|
compressed = mask_to_rle(mask, compressed=True)
|
|
recovered = rle_to_mask(compressed, (4, 4))
|
|
np.testing.assert_array_equal(mask, recovered)
|