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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
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
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
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
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 |
|
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
import numpy as np
import supervision as sv
boxes = np.array([[10, 10, 20, 20], [30, 30, 40, 40]])
offset = np.array([5, 5])
moved_box = sv.move_boxes(boxes, offset)
print(moved_box)
# np.array([
# [15, 15, 25, 25],
# [35, 35, 45, 45]
# ])
Source code in supervision/detection/utils.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
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. |
Example
import numpy as np
import supervision as sv
boxes = np.array([[10, 10, 20, 20], [30, 30, 40, 40]])
factor = 1.5
scaled_bb = sv.scale_boxes(boxes, factor)
print(scaled_bb)
# np.array([
# [ 7.5, 7.5, 22.5, 22.5],
# [27.5, 27.5, 42.5, 42.5]
# ])
Source code in supervision/detection/utils.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|