IoU and NMS Utils¶
supervision.detection.utils.iou.OverlapFilter
¶
Bases: Enum
Enum specifying the strategy for filtering overlapping detections.
Attributes:
| Name | Type | Description |
|---|---|---|
KEEP_ALL |
Do not filter detections based on overlap; keep every detection.
|
|
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.py
supervision.detection.utils.iou.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.py
supervision.detection.utils.iou.OverlapOn
¶
Bases: Enum
Enum specifying the geometry used to measure overlap between detections.
Attributes:
| Name | Type | Description |
|---|---|---|
BOX |
The detection's own box: its oriented box when
|
|
MASK |
The detection's mask. Requires |
Example
>>> OverlapOn.from_value("MASK")
<OverlapOn.MASK: 'mask'>
Source code in src/supervision/detection/utils/iou.py
Methods:¶
from_value(value: OverlapOn | str) -> OverlapOn
classmethod
¶
Return the member for an enum member or a case-insensitive string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
OverlapOn | str
|
A member of this enum or its string value. |
required |
Returns:
| Type | Description |
|---|---|
OverlapOn
|
The matching member. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/supervision/detection/utils/iou.py
supervision.detection.utils.iou.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.py
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
supervision.detection.utils.iou.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.py
supervision.detection.utils.iou.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.py
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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
supervision.detection.utils.iou.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.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 |
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.py
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 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 1657 1658 1659 1660 1661 1662 1663 1664 1665 | |
supervision.detection.utils.iou.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.py
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 878 879 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 | |
supervision.detection.utils.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.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.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.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.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.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.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.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.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/nms.py
supervision.detection.utils.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 predictions with Weighted Box Fusion.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.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/nms.py
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 | |
supervision.detection.utils.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.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.nms.oriented_box_soft_non_max_suppression(predictions: npt.NDArray[np.floating], oriented_boxes: npt.NDArray[np.floating], sigma: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.floating]
¶
Perform Soft Non-Maximum Suppression on oriented bounding box predictions.
Source code in src/supervision/detection/utils/nms.py
supervision.detection.utils.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.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 |
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/nms.py
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 | |