Match Detections¶
Match two sv.Detections objects into one-to-one pairs by greedy, highest-IoU-first assignment. The function returns index arrays, so the result composes with sv.Detections slicing.
import supervision as sv
matched_pairs, unmatched_a, unmatched_b = sv.match_detections(
detections_a,
detections_b,
iou_threshold=0.5,
class_agnostic=False,
)
matched_pairs has shape (M, 2): column 0 indexes detections_a, column 1 indexes detections_b. unmatched_a and unmatched_b hold the indices of the remaining detections on each side.
Matching is greedy, highest-IoU-first, and one-to-one; it is not a globally optimal assignment. With class_agnostic=False (the default), both inputs must provide class_id. Set class_agnostic=True to match on geometry only.
supervision.detection.utils.matching.match_detections(detections_a: Detections, detections_b: Detections, iou_threshold: float = 0.5, class_agnostic: bool = False) -> tuple[npt.NDArray[np.int64], npt.NDArray[np.int64], npt.NDArray[np.int64]]
¶
Match detections from two sources into one-to-one pairs.
The assignment is greedy and highest-IoU-first, identical to the matcher
used by the metrics modules: each detection from detections_a can match
at most one detection from detections_b, and vice versa. It does not
compute a globally optimal assignment. Pairs below iou_threshold are
never matched, and by default a pair also requires equal class_id
values. confidence is ignored; callers that want metric-style score
ordering should sort their detections first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Detections
|
First set of detections. |
required |
|
Detections
|
Second set of detections. |
required |
|
float
|
Minimum IoU required for a pair. Defaults to 0.5. |
0.5
|
|
bool
|
When True, matching ignores
|
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Returns:
| Type | Description |
|---|---|
NDArray[int64]
|
tuple[np.ndarray, np.ndarray, np.ndarray]: A tuple of |
NDArray[int64]
|
|
NDArray[int64]
|
has shape |
tuple[NDArray[int64], NDArray[int64], NDArray[int64]]
|
column 1 indexing |
tuple[NDArray[int64], NDArray[int64], NDArray[int64]]
|
|
Examples:
>>> import numpy as np
>>> from supervision.detection.core import Detections
>>> a = Detections(
... xyxy=np.array([[0, 0, 10, 10]], dtype=np.float32),
... class_id=np.array([0]),
... )
>>> b = Detections(
... xyxy=np.array([[0, 0, 10, 10], [50, 50, 60, 60]], dtype=np.float32),
... class_id=np.array([0, 1]),
... )
>>> matched_pairs, unmatched_a, unmatched_b = match_detections(a, b)
>>> matched_pairs.tolist()
[[0, 0]]
>>> unmatched_a.tolist()
[]
>>> unmatched_b.tolist()
[1]
Source code in src/supervision/detection/utils/matching.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |