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.floating], box_detection: list[float] | npt.NDArray[np.floating], 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].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[float] | NDArray[floating]
|
Ground truth box in format
|
required |
|
list[float] | NDArray[floating]
|
Detected box in format
|
required |
|
OverlapMetric | str
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
float
|
Overlap value between boxes in |
Raises:
| Type | Description |
|---|---|
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
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
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
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
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 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 | |
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: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
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | |
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 via
cv2.intersectConvexConvex. 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
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | |
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 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
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 | |
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 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
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
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
|
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
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 | |
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.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
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 1314 1315 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 | |
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: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: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
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 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 | |
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: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
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 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 | |