IoU and NMS Utils¶
supervision.detection.utils.iou_and_nms.OverlapFilter
¶
Bases: Enum
Enum specifying the strategy for filtering overlapping detections.
Attributes:
| Name | Type | Description |
|---|---|---|
NONE |
Do not filter detections based on overlap. |
|
NON_MAX_SUPPRESSION |
Filter detections using non-max suppression. This means, detections that overlap by more than a set threshold will be discarded, except for the one with the highest confidence. |
|
NON_MAX_MERGE |
Merge detections with non-max merging. This means, detections that overlap by more than a set threshold will be merged into a single detection. |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.OverlapMetric
¶
Bases: Enum
Enum specifying the metric for measuring overlap between detections.
Attributes:
| Name | Type | Description |
|---|---|---|
IOU |
Intersection over Union. A region-overlap metric that compares two shapes (usually bounding boxes or masks) by normalising the shared area with the area of their union. |
|
IOS |
Intersection over Smaller, a region-overlap metric that compares two shapes (usually bounding boxes or masks) by normalising the shared area with the smaller of the two shapes. |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_iou(box_true: list[float] | npt.NDArray[np.number], box_detection: list[float] | npt.NDArray[np.number], overlap_metric: OverlapMetric | str = OverlapMetric.IOU) -> float
¶
Compute overlap metric between two bounding boxes.
Supports standard IOU (intersection-over-union) and IOS
(intersection-over-smaller-area) metrics. Returns the overlap value in range
[0, 1]. Integer coordinates are subtracted before conversion to floating
point so large origins remain precise while area products avoid integer overflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[float] | NDArray[number]
|
Ground truth box in format
|
required |
|
list[float] | NDArray[number]
|
Detected box in format
|
required |
|
OverlapMetric | str
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
float
|
Overlap value between boxes in |
Raises:
| Type | Description |
|---|---|
TypeError
|
If either box contains complex coordinates. |
ValueError
|
If |
Examples:
>>> import supervision as sv
>>> box_true = [100, 100, 200, 200]
>>> box_detection = [150, 150, 250, 250]
>>> sv.box_iou(box_true, box_detection, overlap_metric=sv.OverlapMetric.IOU)
0.142857...
>>> sv.box_iou(box_true, box_detection, overlap_metric=sv.OverlapMetric.IOS)
0.25
Source code in src/supervision/detection/utils/iou_and_nms.py
126 127 128 129 130 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 | |
supervision.detection.utils.iou_and_nms.box_iou_batch(boxes_true: npt.NDArray[np.number], boxes_detection: npt.NDArray[np.number], overlap_metric: OverlapMetric | str = OverlapMetric.IOU) -> npt.NDArray[np.float32]
¶
Compute pairwise overlap scores between batches of bounding boxes.
Supports standard IOU (intersection-over-union) and IOS
(intersection-over-smaller-area) metrics for all boxes_true and
boxes_detection pairs. Returns a matrix of overlap values in range
[0, 1], matching each box from the first batch to each from the second.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
Array of reference boxes in
shape |
required |
|
NDArray[number]
|
Array of detected boxes in
shape |
required |
|
OverlapMetric | str
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[float32]
|
Overlap matrix of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> boxes_true = np.array([
... [100, 100, 200, 200],
... [300, 300, 400, 400]
... ])
>>> boxes_detection = np.array([
... [150, 150, 250, 250],
... [320, 320, 420, 420]
... ])
>>> sv.box_iou_batch(
... boxes_true, boxes_detection, overlap_metric=sv.OverlapMetric.IOU
... )
array([[0.14285..., 0. ],
[0. , 0.47058...]], dtype=float32)
>>> sv.box_iou_batch(
... boxes_true, boxes_detection, overlap_metric=sv.OverlapMetric.IOS
... )
array([[0.25, 0. ],
[0. , 0.64]], dtype=float32)
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_iou_batch_with_jaccard(boxes_true: Sequence[Sequence[float]], boxes_detection: Sequence[Sequence[float]], is_crowd: Sequence[bool]) -> npt.NDArray[np.float64]
¶
Calculate the intersection over union (IoU) between detection bounding boxes (dt) and ground-truth bounding boxes (gt). Reference: https://github.com/rafaelpadilla/review_object_detection_metrics
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Sequence[Sequence[float]]
|
Sequence of ground-truth bounding boxes in the format [x, y, width, height]. |
required |
|
Sequence[Sequence[float]]
|
Sequence of detection bounding boxes in the format [x, y, width, height]. |
required |
|
Sequence[bool]
|
Sequence indicating if each ground-truth bounding box is a crowd region or not. |
required |
Note
This function expects bounding boxes in [x, y, width, height] format
(COCO convention). All other batch IoU functions in this module use
[x_min, y_min, x_max, y_max].
NaN coordinates propagate silently: if any box value is NaN, the
corresponding IoU values will be NaN.
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Array of IoU values of shape |
NDArray[float64]
|
where row |
NDArray[float64]
|
boxes, and column |
NDArray[float64]
|
box |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> boxes_true = [
... [10, 20, 30, 40], # x, y, w, h
... [15, 25, 35, 45]
... ]
>>> boxes_detection = [
... [12, 22, 28, 38],
... [16, 26, 36, 46]
... ]
>>> is_crowd = [False, False]
>>> ious = sv.box_iou_batch_with_jaccard(
... boxes_true=boxes_true,
... boxes_detection=boxes_detection,
... is_crowd=is_crowd
... )
>>> ious # doctest: +ELLIPSIS
array([[0.886..., 0.496...],
[0.4 ..., 0.862...]])
Source code in src/supervision/detection/utils/iou_and_nms.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
supervision.detection.utils.iou_and_nms.mask_iou_batch(masks_true: npt.NDArray[Any] | CompactMask, masks_detection: npt.NDArray[Any] | CompactMask, overlap_metric: OverlapMetric = OverlapMetric.IOU, memory_limit: int = 1024 * 5) -> npt.NDArray[np.floating]
¶
Compute Intersection over Union (IoU) of two sets of masks - masks_true and
masks_detection.
Accepts both dense (N, H, W) boolean arrays and
:class:~supervision.detection.compact_mask.CompactMask objects.
When both inputs are :class:~supervision.detection.compact_mask.CompactMask,
the computation uses
:func:~supervision.detection.utils.iou_and_nms.compact_mask_iou_batch
to avoid materialising
full (N, H, W) arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[Any] | CompactMask
|
3D |
required |
|
NDArray[Any] | CompactMask
|
3D |
required |
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
Memory limit in MB, default is 1024 * 5 MB (5GB).
Controls chunking of |
1024 * 5
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Pairwise IoU of masks from |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> masks_true = np.zeros((1, 4, 4), dtype=bool)
>>> masks_true[:, :2, :2] = True
>>> masks_detection = np.zeros((1, 4, 4), dtype=bool)
>>> masks_detection[:, :3, :3] = True
>>> sv.mask_iou_batch(masks_true, masks_detection)
array([[0.44444445]])
Source code in src/supervision/detection/utils/iou_and_nms.py
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 | |
supervision.detection.utils.iou_and_nms.oriented_box_iou_batch(boxes_true: npt.NDArray[np.number], boxes_detection: npt.NDArray[np.number], overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.floating]
¶
Compute pairwise overlap scores between two sets of oriented bounding boxes using
the configured overlap_metric.
Overlap areas are computed exactly via convex-polygon intersection, gated by a cheap axis-aligned envelope pre-filter — no rasterization is involved, so the result is exact (free of pixel-quantization error) and independent of the coordinate magnitudes.
boxes_true and boxes_detection are expected to be in
((x1, y1), (x2, y2), (x3, y3), (x4, y4)) format.
Note
Inputs must be convex quads with finite coordinates. Self-intersecting
or non-convex polygons produce undefined results. NaN or Inf coordinates
propagate silently as 0.0 — validate inputs before calling if needed.
When boxes_true is boxes_detection (the same Python object, not just
equal values), the function computes only the upper triangle of the
matrix and mirrors it. This optimization is used automatically by the
NMS/NMM callers that pass the same array twice. A defensive .copy()
at the call site would disable the optimization silently — see the
NMS caller comment for context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A |
required |
|
NDArray[number]
|
A |
required |
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of oriented boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Overlap matrix of shape |
NDArray[floating]
|
score between |
NDArray[floating]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> a = np.array([[[0, 0], [2, 0], [2, 2], [0, 2]]], dtype=np.float32)
>>> b = np.array([[[1, 0], [3, 0], [3, 2], [1, 2]]], dtype=np.float32)
>>> sv.oriented_box_iou_batch(a, b) # doctest: +ELLIPSIS
array([[0.333...]])
Source code in src/supervision/detection/utils/iou_and_nms.py
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | |
supervision.detection.utils.iou_and_nms.box_non_max_suppression(predictions: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.bool_]
¶
Perform Non-Maximum Suppression (NMS) on object detection predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of object detection predictions in
the format of |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0, 0, 4, 4, 0.9, 0],
... [0, 0, 4, 4, 0.8, 0],
... ])
>>> sv.box_non_max_suppression(predictions, iou_threshold=0.5)
array([ True, False])
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_soft_non_max_suppression(predictions: npt.NDArray[np.floating], sigma: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.floating]
¶
Perform Soft Non-Maximum Suppression (Soft-NMS) on object detection predictions.
Unlike
:func:~supervision.detection.utils.iou_and_nms.box_non_max_suppression,
which discards overlapping boxes outright,
Soft-NMS keeps every detection and instead rescales its confidence by
score *= exp(-iou**2 / sigma) for each higher-scoring, same-category
overlap — the caller decides whether and where to threshold the result.
A smaller sigma produces a stronger decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of object detection predictions in
the format of |
required |
|
float
|
Controls the strength of the confidence decay; must be greater
than |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
An array containing the updated (decayed) confidence scores, in the
same order as the input |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0, 0, 4, 4, 0.9, 0],
... [0, 0, 4, 4, 0.8, 0],
... ])
>>> sv.box_soft_non_max_suppression(predictions, sigma=0.5)
array([0.9 , 0.10826823])
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_non_max_suppression(predictions: npt.NDArray[np.floating], masks: npt.NDArray[Any] | CompactMask, iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU, mask_dimension: int = 640) -> npt.NDArray[np.bool_]
¶
Perform Non-Maximum Suppression (NMS) on segmentation predictions.
IoU is computed exactly on the full-resolution masks for both dense and
:class:~supervision.detection.compact_mask.CompactMask inputs. The
mask_dimension parameter is kept for backward compatibility but is no
longer used — dense masks are not resized before IoU computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
A 2D array of object detection predictions in
the format of |
required |
|
NDArray[Any] | CompactMask
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
Deprecated, no longer used. Kept for backward compatibility. |
640
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0, 0, 4, 4, 0.9, 0],
... [0, 0, 4, 4, 0.8, 0],
... ])
>>> masks = np.zeros((2, 4, 4), dtype=bool)
>>> masks[:, :2, :2] = True
>>> sv.mask_non_max_suppression(predictions, masks, iou_threshold=0.5)
array([ True, False])
Source code in src/supervision/detection/utils/iou_and_nms.py
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 | |
supervision.detection.utils.iou_and_nms.mask_soft_non_max_suppression(predictions: npt.NDArray[np.floating], masks: npt.NDArray[Any] | CompactMask, sigma: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU, mask_dimension: int = 640) -> npt.NDArray[np.floating]
¶
Perform Soft Non-Maximum Suppression (Soft-NMS) on segmentation predictions.
Unlike
:func:~supervision.detection.utils.iou_and_nms.mask_non_max_suppression,
which discards overlapping masks outright,
Soft-NMS keeps every detection and instead rescales its confidence by
score *= exp(-iou**2 / sigma) for each higher-scoring, same-category
overlap — the caller decides whether and where to threshold the result.
A smaller sigma produces a stronger decay.
The 3rd positional parameter here is sigma, not iou_threshold as in
:func:~supervision.detection.utils.iou_and_nms.mask_non_max_suppression —
Soft-NMS has no threshold to suppress at, only a decay strength, so the two
signatures intentionally diverge at that
position.
IoU is computed exactly on the full-resolution masks for both dense and
:class:~supervision.detection.compact_mask.CompactMask inputs. The
mask_dimension parameter is kept for signature parity with
:func:~supervision.detection.utils.iou_and_nms.mask_non_max_suppression but
is not used — dense masks are not resized
before IoU computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
A 2D array of object detection predictions in
the format of |
required |
|
NDArray[Any] | CompactMask
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
Controls the strength of the confidence decay; must be greater
than |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
Deprecated, unused. Kept for signature parity with
:func: |
640
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
An array containing the updated (decayed) confidence scores, in the
same order as the input |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0, 0, 4, 4, 0.9, 0],
... [0, 0, 4, 4, 0.8, 0],
... ])
>>> masks = np.zeros((2, 4, 4), dtype=bool)
>>> masks[:, :2, :2] = True
>>> sv.mask_soft_non_max_suppression(predictions, masks, sigma=0.5)
array([0.9 , 0.10826823])
Source code in src/supervision/detection/utils/iou_and_nms.py
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 | |
supervision.detection.utils.iou_and_nms.box_non_max_merge(predictions: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> list[list[int]]
¶
Apply greedy version of non-maximum merging per category to avoid detecting too many overlapping bounding boxes for a given object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
list[list[int]]: Groups of prediction indices be merged. Each group may have 1 or more elements. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0, 0, 4, 4, 0.9, 0],
... [0, 0, 4, 4, 0.8, 0],
... ])
>>> sv.box_non_max_merge(predictions, iou_threshold=0.5)
[[0, 1]]
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_weighted_box_fusion(predictions: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU, num_models: int | None = None) -> npt.NDArray[np.floating]
¶
Fuse overlapping object detection boxes with Weighted Box Fusion (WBF).
Unlike :func:~supervision.detection.utils.iou_and_nms.box_non_max_suppression,
which discards overlapping boxes, and
:func:~supervision.detection.utils.iou_and_nms.box_non_max_merge, which only
returns groups of overlapping indices, WBF replaces each group of overlapping
boxes with a single box whose coordinates are the confidence-weighted average
of the group and whose score is the group's mean confidence. This keeps
information from every box in a cluster instead of dropping all but one, which
typically produces better localized boxes when combining predictions from
several models or augmentations. Based on Solovyev et al., "Weighted Boxes
Fusion: Ensembling boxes from different object detection models" (2019).
Boxes are clustered sequentially within each class (across all boxes when
there is no class column). They are visited in descending confidence order,
and each box is compared with the current fused box of every existing
cluster. The box joins the cluster with the highest overlap only when that
overlap is strictly greater than iou_threshold, and that cluster's fused
box is then recomputed; otherwise the box starts a new cluster. Because a
fused box moves as boxes join it, a box can join a cluster even when its
overlap with the cluster's highest-scoring box alone is too low, unlike the
seed-anchored grouping of
:func:~supervision.detection.utils.iou_and_nms.box_non_max_merge.
Warning
The fused score is the mean confidence of the cluster, not its maximum,
so it can be lower than the score of the best box in the cluster. For
example, a 0.95 detection fused with an overlapping 0.15 box scores
0.55, where
:func:~supervision.detection.utils.iou_and_nms.box_non_max_suppression
would keep 0.95. Revisit any downstream confidence filter tuned for
non-maximum suppression before switching to WBF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of shape |
required |
|
float
|
The overlap threshold used to decide whether a box joins an existing cluster, measured against that cluster's current fused box. The comparison is exclusive, as in the WBF paper: the overlap must be strictly greater than this value, so an overlap exactly equal to it keeps the box out of the cluster. Defaults to 0.5. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
|
int | None
|
Number of models (or test-time augmentations) whose
predictions were concatenated into |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
An array of fused predictions with the same number of columns as the
input ( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0.0, 0.0, 10.0, 10.0, 0.9, 0],
... [1.0, 1.0, 11.0, 11.0, 0.8, 0],
... ])
>>> fused = sv.box_weighted_box_fusion(predictions, iou_threshold=0.5)
>>> fused[:, 4]
array([0.85])
Rescale scores when fusing the predictions of two models. The second box was predicted by only one model, so its score is halved:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0.0, 0.0, 10.0, 10.0, 0.9, 0],
... [50.0, 50.0, 60.0, 60.0, 0.9, 0],
... [1.0, 1.0, 11.0, 11.0, 0.8, 0],
... ])
>>> sv.box_weighted_box_fusion(predictions)[:, 4]
array([0.9 , 0.85])
>>> sv.box_weighted_box_fusion(predictions, num_models=2)[:, 4]
array([0.85, 0.45])
Source code in src/supervision/detection/utils/iou_and_nms.py
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 | |
supervision.detection.utils.iou_and_nms.mask_non_max_merge(predictions: npt.NDArray[np.floating], masks: npt.NDArray[Any] | CompactMask, iou_threshold: float = 0.5, *args: Any, overlap_metric: OverlapMetric = OverlapMetric.IOU, mask_dimension: int = 640) -> list[list[int]]
¶
Perform Non-Maximum Merging (NMM) on segmentation predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
A 2D array of object detection predictions in
the format of |
required |
|
NDArray[Any] | CompactMask
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum merging. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
Deprecated in |
640
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
A list of groups of prediction indices. Each inner list contains
the indices of predictions whose masks overlap above |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If more than five positional arguments are passed. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> predictions = np.array([
... [0, 0, 4, 4, 0.9, 0],
... [0, 0, 4, 4, 0.8, 0],
... ])
>>> masks = np.zeros((2, 4, 4), dtype=bool)
>>> masks[:, :2, :2] = True
>>> sv.mask_non_max_merge(predictions, masks, iou_threshold=0.5)
[[0, 1]]
Source code in src/supervision/detection/utils/iou_and_nms.py
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 | |
supervision.detection.utils.iou_and_nms.oriented_box_non_max_suppression(predictions: npt.NDArray[np.floating], oriented_boxes: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.bool_]
¶
Perform Non-Maximum Suppression on oriented bounding box predictions.
Overlap is computed via
:func:~supervision.detection.utils.iou_and_nms.oriented_box_iou_batch on
the four corners of each box, so detections whose axis-aligned bounding boxes
overlap heavily but whose oriented bodies do not are kept — unlike
:func:~supervision.detection.utils.iou_and_nms.box_non_max_suppression,
which would suppress them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of object detection predictions in the
format |
required |
|
NDArray[floating]
|
Array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of oriented boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A boolean array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> oriented_boxes = np.array([
... [[10, 10], [50, 10], [50, 30], [10, 30]],
... [[11, 11], [51, 11], [51, 31], [11, 31]],
... ], dtype=np.float32)
>>> predictions = np.array([
... [10, 10, 50, 30, 0.9, 0],
... [11, 11, 51, 31, 0.8, 0],
... ], dtype=np.float32)
>>> keep = sv.oriented_box_non_max_suppression(
... predictions=predictions,
... oriented_boxes=oriented_boxes,
... iou_threshold=0.5,
... )
>>> keep
array([ True, False])
Source code in src/supervision/detection/utils/iou_and_nms.py
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 | |
supervision.detection.utils.iou_and_nms.oriented_box_non_max_merge(predictions: npt.NDArray[np.floating], oriented_boxes: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> list[list[int]]
¶
Perform Non-Maximum Merging on oriented bounding box predictions, grouped per category.
Mirrors :func:~supervision.detection.utils.iou_and_nms.box_non_max_merge
but uses oriented-box IoU, so groups of rotated detections sharing the same
body — rather than the same
axis-aligned bounding box — are merged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of shape |
required |
|
NDArray[floating]
|
Array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum merging. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of oriented boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
Groups of prediction indices to be merged. Each group may have 1 or more elements. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> oriented_boxes = np.array([
... [[10, 10], [50, 10], [50, 30], [10, 30]],
... [[11, 11], [51, 11], [51, 31], [11, 31]],
... ], dtype=np.float32)
>>> predictions = np.array([
... [10, 10, 50, 30, 0.9, 0],
... [11, 11, 51, 31, 0.8, 0],
... ], dtype=np.float32)
>>> groups = sv.oriented_box_non_max_merge(
... predictions=predictions,
... oriented_boxes=oriented_boxes,
... iou_threshold=0.5,
... )
>>> len(groups)
1
Source code in src/supervision/detection/utils/iou_and_nms.py
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 | |