Detection Utils¶
supervision.detection.utils.box_iou_batch(boxes_true, boxes_detection)
¶
Compute Intersection over Union (IoU) of two sets of bounding boxes -
boxes_true
and boxes_detection
. Both sets
of boxes are expected to be in (x_min, y_min, x_max, y_max)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
2D |
required |
|
ndarray
|
2D |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Pairwise IoU of boxes from |
Source code in supervision/detection/utils.py
supervision.detection.utils.mask_iou_batch(masks_true, masks_detection, memory_limit=1024 * 5)
¶
Compute Intersection over Union (IoU) of two sets of masks -
masks_true
and masks_detection
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
3D |
required |
|
ndarray
|
3D |
required |
|
int
|
memory limit in MB, default is 1024 * 5 MB (5GB). |
1024 * 5
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Pairwise IoU of masks from |
Source code in supervision/detection/utils.py
supervision.detection.utils.oriented_box_iou_batch(boxes_true, boxes_detection)
¶
Compute Intersection over Union (IoU) of two sets of oriented bounding boxes -
boxes_true
and boxes_detection
. Both sets of boxes are expected to be in
((x1, y1), (x2, y2), (x3, y3), (x4, y4))
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
a |
required |
|
ndarray
|
a |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Pairwise IoU of boxes from |
Source code in supervision/detection/utils.py
supervision.detection.utils.polygon_to_mask(polygon, resolution_wh)
¶
Generate a mask from a polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
The polygon for which the mask should be generated, given as a list of vertices. |
required |
|
Tuple[int, int]
|
The width and height of the desired resolution. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The generated 2D mask, where the polygon is marked with
|
Source code in supervision/detection/utils.py
supervision.detection.utils.mask_to_xyxy(masks)
¶
Converts a 3D np.array
of 2D bool masks into a 2D np.array
of bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A 3D |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 2D |
Source code in supervision/detection/utils.py
supervision.detection.utils.mask_to_polygons(mask)
¶
Converts a binary mask to a list of polygons.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A binary mask represented as a 2D NumPy array of
shape |
required |
Returns:
Type | Description |
---|---|
List[ndarray]
|
List[np.ndarray]: A list of polygons, where each polygon is represented by a
NumPy array of shape |
Source code in supervision/detection/utils.py
supervision.detection.utils.polygon_to_xyxy(polygon)
¶
Converts a polygon represented by a NumPy array into a bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A polygon represented by a NumPy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 1D NumPy array containing the bounding box
|
Source code in supervision/detection/utils.py
supervision.detection.utils.filter_polygons_by_area(polygons, min_area=None, max_area=None)
¶
Filters a list of polygons based on their area.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
List[ndarray]
|
A list of polygons, where each polygon is
represented by a NumPy array of shape |
required |
|
Optional[float]
|
The minimum area threshold. Only polygons with an area greater than or equal to this value will be included in the output. If set to None, no minimum area constraint will be applied. |
None
|
|
Optional[float]
|
The maximum area threshold. Only polygons with an area less than or equal to this value will be included in the output. If set to None, no maximum area constraint will be applied. |
None
|
Returns:
Type | Description |
---|---|
List[ndarray]
|
List[np.ndarray]: A new list of polygons containing only those with areas within the specified thresholds. |
Source code in supervision/detection/utils.py
supervision.detection.utils.move_boxes(xyxy, offset)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[float64]
|
An array of shape |
required |
|
array
|
An array of shape |
required |
Returns:
Type | Description |
---|---|
NDArray[float64]
|
npt.NDArray[np.float64]: Repositioned bounding boxes. |
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 10, 20, 20],
[30, 30, 40, 40]
])
offset = np.array([5, 5])
sv.move_boxes(xyxy=xyxy, offset=offset)
# array([
# [15, 15, 25, 25],
# [35, 35, 45, 45]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.move_masks(masks, offset, resolution_wh)
¶
Offset the masks in an array by the specified (x, y) amount.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
A 3D array of binary masks corresponding to the
predictions. Shape: |
required |
|
NDArray[int32]
|
An array of shape |
required |
|
Tuple[int, int]
|
The width and height of the desired mask resolution. |
required |
Returns:
Type | Description |
---|---|
NDArray[bool_]
|
(npt.NDArray[np.bool_]) repositioned masks, optionally padded to the specified shape. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([[[False, False, False, False],
[False, True, True, False],
[False, True, True, False],
[False, False, False, False]]], dtype=bool)
offset = np.array([1, 1])
sv.move_masks(mask, offset, resolution_wh=(4, 4))
# array([[[False, False, False, False],
# [False, False, False, False],
# [False, False, True, True],
# [False, False, True, True]]], dtype=bool)
offset = np.array([-2, 2])
sv.move_masks(mask, offset, resolution_wh=(4, 4))
# array([[[False, False, False, False],
# [False, False, False, False],
# [False, False, False, False],
# [True, False, False, False]]], dtype=bool)
Source code in supervision/detection/utils.py
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 |
|
supervision.detection.utils.scale_boxes(xyxy, factor)
¶
Scale the dimensions of bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[float64]
|
An array of shape |
required |
|
float
|
A float value representing the factor by which the box dimensions are scaled. A factor greater than 1 enlarges the boxes, while a factor less than 1 shrinks them. |
required |
Returns:
Type | Description |
---|---|
NDArray[float64]
|
npt.NDArray[np.float64]: Scaled bounding boxes. |
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 10, 20, 20],
[30, 30, 40, 40]
])
sv.scale_boxes(xyxy=xyxy, factor=1.5)
# array([
# [ 7.5, 7.5, 22.5, 22.5],
# [27.5, 27.5, 42.5, 42.5]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.clip_boxes(xyxy, resolution_wh)
¶
Clips bounding boxes coordinates to fit within the frame resolution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
|
Tuple[int, int]
|
A tuple of the form |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 20, 300, 200],
[15, 25, 350, 450],
[-10, -20, 30, 40]
])
sv.clip_boxes(xyxy=xyxy, resolution_wh=(320, 240))
# array([
# [ 10, 20, 300, 200],
# [ 15, 25, 320, 240],
# [ 0, 0, 30, 40]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.pad_boxes(xyxy, px, py=None)
¶
Pads bounding boxes coordinates with a constant padding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
|
int
|
The padding value to be added to both the left and right sides of each bounding box. |
required |
|
Optional[int]
|
The padding value to be added to both the top and bottom
sides of each bounding box. If not provided, |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 20, 30, 40],
[15, 25, 35, 45]
])
sv.pad_boxes(xyxy=xyxy, px=5, py=10)
# array([
# [ 5, 10, 35, 50],
# [10, 15, 40, 55]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.xywh_to_xyxy(xywh)
¶
Converts bounding box coordinates from (x, y, width, height)
format to (x_min, y_min, x_max, y_max)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xywh = np.array([
[10, 20, 30, 40],
[15, 25, 35, 45]
])
sv.xywh_to_xyxy(xywh=xywh)
# array([
# [10, 20, 40, 60],
# [15, 25, 50, 70]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.xyxy_to_xywh(xyxy)
¶
Converts bounding box coordinates from (x_min, y_min, x_max, y_max)
format to (x, y, width, height)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 20, 40, 60],
[15, 25, 50, 70]
])
sv.xyxy_to_xywh(xyxy=xyxy)
# array([
# [10, 20, 30, 40],
# [15, 25, 35, 45]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.xyxy_to_xcycarh(xyxy)
¶
Converts bounding box coordinates from (x_min, y_min, x_max, y_max)
into measurement space to format (center x, center y, aspect ratio, height)
,
where the aspect ratio is width / height
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
Bounding box in format |
required |
Returns:
np.ndarray: Bounding box in format
(center x, center y, aspect ratio, height)
. Shape (N, 4)
.
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 20, 40, 60],
[15, 25, 50, 70]
])
sv.xyxy_to_xcycarh(xyxy=xyxy)
# array([
# [25. , 40. , 0.75, 40. ],
# [32.5 , 47.5 , 0.77777778, 45. ]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.xcycwh_to_xyxy(xcycwh)
¶
Converts bounding box coordinates from (center_x, center_y, width, height)
format to (x_min, y_min, x_max, y_max)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xcycwh = np.array([
[50, 50, 20, 30],
[30, 40, 10, 15]
])
sv.xcycwh_to_xyxy(xcycwh=xcycwh)
# array([
# [40, 35, 60, 65],
# [25, 32.5, 35, 47.5]
# ])
Source code in supervision/detection/utils.py
supervision.detection.utils.contains_holes(mask)
¶
Checks if the binary mask contains holes (background pixels fully enclosed by foreground pixels).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
2D binary mask where |
required |
Returns:
Type | Description |
---|---|
bool
|
True if holes are detected, False otherwise. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]).astype(bool)
sv.contains_holes(mask=mask)
# True
mask = np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]).astype(bool)
sv.contains_holes(mask=mask)
# False
Source code in supervision/detection/utils.py
supervision.detection.utils.contains_multiple_segments(mask, connectivity=4)
¶
Checks if the binary mask contains multiple unconnected foreground segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
2D binary mask where |
required |
|
int)
|
Default: 4 is 4-way connectivity, which means that foreground pixels are the part of the same segment/component if their edges touch. Alternatively: 8 for 8-way connectivity, when foreground pixels are connected by their edges or corners touch. |
4
|
Returns:
Type | Description |
---|---|
bool
|
True when the mask contains multiple not connected components, False otherwise. |
Raises:
Type | Description |
---|---|
ValueError
|
If connectivity(int) parameter value is not 4 or 8. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 0]
]).astype(bool)
sv.contains_multiple_segments(mask=mask, connectivity=4)
# True
mask = np.array([
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0]
]).astype(bool)
sv.contains_multiple_segments(mask=mask, connectivity=4)
# False
Source code in supervision/detection/utils.py
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 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 |
|