Detection Utils¶
box_iou_batch
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
mask_iou_batch
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
box_non_max_suppression
Perform Non-Maximum Suppression (NMS) on object detection predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
An array of object detection predictions in
the format of |
required |
iou_threshold |
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A boolean array indicating which predictions to keep after n on-maximum suppression. |
Raises:
Type | Description |
---|---|
AssertionError
|
If |
Source code in supervision/detection/utils.py
mask_non_max_suppression
Perform Non-Maximum Suppression (NMS) on segmentation predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
A 2D array of object detection predictions in
the format of |
required |
masks |
ndarray
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
iou_threshold |
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
mask_dimension |
int
|
The dimension to which the masks should be resized before computing IOU values. Defaults to 640. |
640
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
Type | Description |
---|---|
AssertionError
|
If |
Source code in supervision/detection/utils.py
polygon_to_mask
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
mask_to_xyxy
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
mask_to_polygons
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
polygon_to_xyxy
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
filter_polygons_by_area
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
move_boxes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
ndarray
|
An array of shape |
required |
offset |
array
|
An array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Repositioned bounding boxes. |
Example
Source code in supervision/detection/utils.py
scale_boxes
Scale the dimensions of bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
ndarray
|
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
|
np.ndarray: Scaled bounding boxes. |