Detection Utils¶
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 |
---|---|---|---|
boxes_true |
ndarray
|
2D |
required |
boxes_detection |
ndarray
|
2D |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Pairwise IoU of boxes from |
Source code in supervision/detection/utils.py
Compute Intersection over Union (IoU) of two sets of masks -
masks_true
and masks_detection
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks_true |
ndarray
|
3D |
required |
masks_detection |
ndarray
|
3D |
required |
memory_limit |
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
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 |
---|---|---|---|
boxes_true |
ndarray
|
a |
required |
boxes_detection |
ndarray
|
a |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Pairwise IoU of boxes from |
Source code in supervision/detection/utils.py
Generate a mask from a polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polygon |
ndarray
|
The polygon for which the mask should be generated, given as a list of vertices. |
required |
resolution_wh |
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
Converts a 3D np.array
of 2D bool masks into a 2D np.array
of bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks |
ndarray
|
A 3D |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 2D |
Source code in supervision/detection/utils.py
Converts a binary mask to a list of polygons.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
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
Converts a polygon represented by a NumPy array into a bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polygon |
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
Filters a list of polygons based on their area.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polygons |
List[ndarray]
|
A list of polygons, where each polygon is
represented by a NumPy array of shape |
required |
min_area |
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
|
max_area |
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
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
NDArray[float64]
|
An array of shape |
required |
offset |
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
Offset the masks in an array by the specified (x, y) amount.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks |
NDArray[bool_]
|
A 3D array of binary masks corresponding to the
predictions. Shape: |
required |
offset |
NDArray[int32]
|
An array of shape |
required |
resolution_wh |
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. |
Source code in supervision/detection/utils.py
Scale the dimensions of bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
NDArray[float64]
|
An array of shape |
required |
factor |
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
Clips bounding boxes coordinates to fit within the frame resolution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
ndarray
|
A numpy array of shape |
required |
resolution_wh |
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
Pads bounding boxes coordinates with a constant padding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
ndarray
|
A numpy array of shape |
required |
px |
int
|
The padding value to be added to both the left and right sides of each bounding box. |
required |
py |
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
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 |
---|---|---|---|
xywh |
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
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 |
---|---|---|---|
xcycwh |
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
Checks if the binary mask contains holes (background pixels fully enclosed by foreground pixels).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
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
Checks if the binary mask contains multiple unconnected foreground segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
NDArray[bool_]
|
2D binary mask where |
required |
connectivity |
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