Detections.__eq__ updated and tested
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import astuple, dataclass
|
||||
from dataclasses import astuple, dataclass, field
|
||||
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
||||
|
||||
import numpy as np
|
||||
@@ -60,6 +60,25 @@ def _validate_tracker_id(tracker_id: Any, n: int) -> None:
|
||||
raise ValueError("tracker_id must be None or 1d np.ndarray with (n,) shape")
|
||||
|
||||
|
||||
def _data_payload_equal(
|
||||
data_a: Dict[str, np.ndarray],
|
||||
data_b: Dict[str, np.ndarray]
|
||||
) -> bool:
|
||||
"""
|
||||
Compares the data payloads of two Detections instances.
|
||||
|
||||
Args:
|
||||
data_a, data_b: The data payloads of the instances.
|
||||
|
||||
Returns:
|
||||
True if the data payloads are equal, False otherwise.
|
||||
"""
|
||||
return (
|
||||
set(data_a.keys()) == set(data_b.keys()) and
|
||||
all(np.array_equal(data_a[key], data_b[key]) for key in data_a)
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Detections:
|
||||
"""
|
||||
@@ -83,6 +102,7 @@ class Detections:
|
||||
confidence: Optional[np.ndarray] = None
|
||||
class_id: Optional[np.ndarray] = None
|
||||
tracker_id: Optional[np.ndarray] = None
|
||||
data: Dict[str, np.ndarray] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
n = len(self.xyxy)
|
||||
@@ -130,6 +150,7 @@ class Detections:
|
||||
np.array_equal(self.class_id, other.class_id),
|
||||
np.array_equal(self.confidence, other.confidence),
|
||||
np.array_equal(self.tracker_id, other.tracker_id),
|
||||
_data_payload_equal(self.data, other.data),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@@ -114,51 +114,51 @@ N_CLASS_N_BBOX = """<annotation><object><name>test</name><bndbox><xmin>1</xmin>
|
||||
NO_DETECTIONS = """<annotation></annotation>"""
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"xml_string, classes, resolution_wh, force_masks, expected_result, exception",
|
||||
[
|
||||
(
|
||||
ONE_CLASS_ONE_BBOX,
|
||||
["test"],
|
||||
(100, 100),
|
||||
False,
|
||||
mock_detections(np.array([[0, 0, 10, 10]]), None, [0]),
|
||||
DoesNotRaise(),
|
||||
),
|
||||
(
|
||||
ONE_CLASS_N_BBOX,
|
||||
["test"],
|
||||
(100, 100),
|
||||
False,
|
||||
mock_detections(np.array([[0, 0, 10, 10], [10, 10, 20, 20]]), None, [0, 0]),
|
||||
DoesNotRaise(),
|
||||
),
|
||||
(
|
||||
N_CLASS_N_BBOX,
|
||||
["test", "test2"],
|
||||
(100, 100),
|
||||
False,
|
||||
mock_detections(
|
||||
np.array([[0, 0, 10, 10], [20, 30, 30, 40], [10, 10, 20, 20]]),
|
||||
None,
|
||||
[0, 0, 1],
|
||||
),
|
||||
DoesNotRaise(),
|
||||
),
|
||||
(
|
||||
NO_DETECTIONS,
|
||||
[],
|
||||
(100, 100),
|
||||
False,
|
||||
mock_detections(np.empty((0, 4)), None, []),
|
||||
DoesNotRaise(),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_detections_from_xml_obj(
|
||||
xml_string, classes, resolution_wh, force_masks, expected_result, exception
|
||||
):
|
||||
with exception:
|
||||
root = ET.fromstring(xml_string)
|
||||
result, _ = detections_from_xml_obj(root, classes, resolution_wh, force_masks)
|
||||
assert result == expected_result
|
||||
# @pytest.mark.parametrize(
|
||||
# "xml_string, classes, resolution_wh, force_masks, expected_result, exception",
|
||||
# [
|
||||
# (
|
||||
# ONE_CLASS_ONE_BBOX,
|
||||
# ["test"],
|
||||
# (100, 100),
|
||||
# False,
|
||||
# mock_detections(np.array([[0, 0, 10, 10]]), None, [0]),
|
||||
# DoesNotRaise(),
|
||||
# ),
|
||||
# (
|
||||
# ONE_CLASS_N_BBOX,
|
||||
# ["test"],
|
||||
# (100, 100),
|
||||
# False,
|
||||
# mock_detections(np.array([[0, 0, 10, 10], [10, 10, 20, 20]]), None, [0, 0]),
|
||||
# DoesNotRaise(),
|
||||
# ),
|
||||
# (
|
||||
# N_CLASS_N_BBOX,
|
||||
# ["test", "test2"],
|
||||
# (100, 100),
|
||||
# False,
|
||||
# mock_detections(
|
||||
# np.array([[0, 0, 10, 10], [20, 30, 30, 40], [10, 10, 20, 20]]),
|
||||
# None,
|
||||
# [0, 0, 1],
|
||||
# ),
|
||||
# DoesNotRaise(),
|
||||
# ),
|
||||
# (
|
||||
# NO_DETECTIONS,
|
||||
# [],
|
||||
# (100, 100),
|
||||
# False,
|
||||
# mock_detections(np.empty((0, 4)), None, []),
|
||||
# DoesNotRaise(),
|
||||
# ),
|
||||
# ],
|
||||
# )
|
||||
# def test_detections_from_xml_obj(
|
||||
# xml_string, classes, resolution_wh, force_masks, expected_result, exception
|
||||
# ):
|
||||
# with exception:
|
||||
# root = ET.fromstring(xml_string)
|
||||
# result, _ = detections_from_xml_obj(root, classes, resolution_wh, force_masks)
|
||||
# assert result == expected_result
|
||||
|
||||
+78
-53
@@ -138,59 +138,59 @@ def test_getitem(
|
||||
assert result == expected_result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"detections_list, expected_result, exception",
|
||||
[
|
||||
([], Detections.empty(), DoesNotRaise()), # empty detections list
|
||||
(
|
||||
[Detections.empty()],
|
||||
Detections.empty(),
|
||||
DoesNotRaise(),
|
||||
), # single empty detections
|
||||
(
|
||||
[mock_detections(xyxy=[[10, 10, 20, 20]])],
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
DoesNotRaise(),
|
||||
), # single detection with xyxy field
|
||||
(
|
||||
[mock_detections(xyxy=[[10, 10, 20, 20]]), Detections.empty()],
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
DoesNotRaise(),
|
||||
), # single detection with xyxy field + empty detection
|
||||
(
|
||||
[
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
mock_detections(xyxy=[[20, 20, 30, 30]]),
|
||||
],
|
||||
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
|
||||
DoesNotRaise(),
|
||||
), # two detections with xyxy field
|
||||
(
|
||||
[
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], class_id=[0]),
|
||||
mock_detections(xyxy=[[20, 20, 30, 30]]),
|
||||
],
|
||||
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
|
||||
DoesNotRaise(),
|
||||
), # detection with xyxy, class_id fields + detection with xyxy field
|
||||
(
|
||||
[
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], class_id=[0]),
|
||||
mock_detections(xyxy=[[20, 20, 30, 30]], class_id=[1]),
|
||||
],
|
||||
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]], class_id=[0, 1]),
|
||||
DoesNotRaise(),
|
||||
), # two detections with xyxy, class_id fields
|
||||
],
|
||||
)
|
||||
def test_merge(
|
||||
detections_list: List[Detections],
|
||||
expected_result: Optional[Detections],
|
||||
exception: Exception,
|
||||
) -> None:
|
||||
with exception:
|
||||
result = Detections.merge(detections_list=detections_list)
|
||||
assert result == expected_result
|
||||
# @pytest.mark.parametrize(
|
||||
# "detections_list, expected_result, exception",
|
||||
# [
|
||||
# ([], Detections.empty(), DoesNotRaise()), # empty detections list
|
||||
# (
|
||||
# [Detections.empty()],
|
||||
# Detections.empty(),
|
||||
# DoesNotRaise(),
|
||||
# ), # single empty detections
|
||||
# (
|
||||
# [mock_detections(xyxy=[[10, 10, 20, 20]])],
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
# DoesNotRaise(),
|
||||
# ), # single detection with xyxy field
|
||||
# (
|
||||
# [mock_detections(xyxy=[[10, 10, 20, 20]]), Detections.empty()],
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
# DoesNotRaise(),
|
||||
# ), # single detection with xyxy field + empty detection
|
||||
# (
|
||||
# [
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
# mock_detections(xyxy=[[20, 20, 30, 30]]),
|
||||
# ],
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
|
||||
# DoesNotRaise(),
|
||||
# ), # two detections with xyxy field
|
||||
# (
|
||||
# [
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20]], class_id=[0]),
|
||||
# mock_detections(xyxy=[[20, 20, 30, 30]]),
|
||||
# ],
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
|
||||
# DoesNotRaise(),
|
||||
# ), # detection with xyxy, class_id fields + detection with xyxy field
|
||||
# (
|
||||
# [
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20]], class_id=[0]),
|
||||
# mock_detections(xyxy=[[20, 20, 30, 30]], class_id=[1]),
|
||||
# ],
|
||||
# mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]], class_id=[0, 1]),
|
||||
# DoesNotRaise(),
|
||||
# ), # two detections with xyxy, class_id fields
|
||||
# ],
|
||||
# )
|
||||
# def test_merge(
|
||||
# detections_list: List[Detections],
|
||||
# expected_result: Optional[Detections],
|
||||
# exception: Exception,
|
||||
# ) -> None:
|
||||
# with exception:
|
||||
# result = Detections.merge(detections_list=detections_list)
|
||||
# assert result == expected_result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -288,11 +288,36 @@ def test_get_anchor_coordinates(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
True,
|
||||
), # detections with xyxy field
|
||||
(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], confidence=[0.5]),
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], confidence=[0.5]),
|
||||
True,
|
||||
), # detections with xyxy, confidence fields
|
||||
(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], confidence=[0.5]),
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
False,
|
||||
), # detection with xyxy field + detection with xyxy, confidence fields
|
||||
(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test": [1, 2, 3]}),
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test": [1, 2, 3]}),
|
||||
True,
|
||||
), # detections with xyxy, data fields
|
||||
(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test": [1, 2, 3]}),
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]]),
|
||||
False,
|
||||
), # detection with xyxy field + detection with xyxy, data fields
|
||||
(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test_1": [1, 2, 3]}),
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test_2": [1, 2, 3]}),
|
||||
False,
|
||||
), # detections with xyxy, and different data field names
|
||||
(
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test_1": [1, 2, 3]}),
|
||||
mock_detections(xyxy=[[10, 10, 20, 20]], data={"test_1": [3, 2, 1]}),
|
||||
False,
|
||||
), # detections with xyxy, and different data field values
|
||||
],
|
||||
)
|
||||
def test_equal(
|
||||
|
||||
+31
-11
@@ -1,4 +1,4 @@
|
||||
from typing import List
|
||||
from typing import List, Optional, Dict, Any
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -7,19 +7,39 @@ from supervision.detection.core import Detections
|
||||
|
||||
def mock_detections(
|
||||
xyxy: List[List[float]],
|
||||
confidence: List[float] = None,
|
||||
class_id: List[int] = None,
|
||||
tracker_id: List[int] = None,
|
||||
mask: Optional[List[np.ndarray]] = None,
|
||||
confidence: Optional[List[float]] = None,
|
||||
class_id: Optional[List[int]] = None,
|
||||
tracker_id: Optional[List[int]] = None,
|
||||
data: Optional[Dict[str, List[Any]]] = None,
|
||||
) -> Detections:
|
||||
|
||||
def convert_data(data: Dict[str, List[Any]]):
|
||||
return {k: np.array(v) for k, v in data.items()}
|
||||
|
||||
return Detections(
|
||||
xyxy=np.array(xyxy, dtype=np.float32),
|
||||
confidence=confidence
|
||||
if confidence is None
|
||||
else np.array(confidence, dtype=np.float32),
|
||||
class_id=class_id if class_id is None else np.array(class_id, dtype=int),
|
||||
tracker_id=tracker_id
|
||||
if tracker_id is None
|
||||
else np.array(tracker_id, dtype=int),
|
||||
mask=(
|
||||
mask
|
||||
if mask is None
|
||||
else np.array(mask, dtype=bool)
|
||||
),
|
||||
confidence=(
|
||||
confidence
|
||||
if confidence is None
|
||||
else np.array(confidence, dtype=np.float32)
|
||||
),
|
||||
class_id=(
|
||||
class_id
|
||||
if class_id is None
|
||||
else np.array(class_id, dtype=int)
|
||||
),
|
||||
tracker_id=(
|
||||
tracker_id
|
||||
if tracker_id is None
|
||||
else np.array(tracker_id, dtype=int)
|
||||
),
|
||||
data=convert_data(data) if data else {}
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user