Converters Utils¶
supervision.detection.utils.converters.xyxy_to_xywh(xyxy: npt.NDArray[np.number]) -> npt.NDArray[np.number]
¶
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[number]
|
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 src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.xywh_to_xyxy(xywh: npt.NDArray[np.number]) -> npt.NDArray[np.number]
¶
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[number]
|
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 src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.xyxy_to_xcycarh(xyxy: npt.NDArray[np.number]) -> npt.NDArray[np.floating]
¶
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) # doctest: +ELLIPSIS
array([[25. , 40. , 0.75 , 40. ],
[32.5 , 47.5 , 0.77..., 45. ]])
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.xcycwh_to_xyxy(xcycwh: npt.NDArray[np.number]) -> npt.NDArray[np.number]
¶
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[number]
|
np.ndarray: A numpy array of shape |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> xcycwh = np.array([
... [50.0, 50.0, 20.0, 30.0],
... [30.0, 40.0, 10.0, 15.0]
... ])
>>> sv.xcycwh_to_xyxy(xcycwh=xcycwh)
array([[40. , 35. , 60. , 65. ],
[25. , 32.5, 35. , 47.5]])
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.xyxy_to_polygons(box: npt.NDArray[np.number]) -> npt.NDArray[np.number]
¶
Convert an array of boxes to an array of polygons. Retains the input datatype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
An array of boxes (N, 4), where each box is represented as a
list of four coordinates in the format |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
np.ndarray: An array of polygons (N, 4, 2), where each polygon is
represented as a list of four coordinates in the format |
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.mask_to_xyxy(masks: npt.NDArray[np.bool_]) -> npt.NDArray[np.int_]
¶
Converts a 3D np.array of 2D bool masks into a 2D np.array of bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[bool_]
|
A 3D |
required |
Returns:
| Type | Description |
|---|---|
NDArray[int_]
|
A 2D |
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.mask_to_polygons(mask: npt.NDArray[np.bool_]) -> list[npt.NDArray[np.int32]]
¶
Converts a binary mask to a list of polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[bool_]
|
A binary mask represented as a 2D NumPy array of shape |
required |
Returns:
| Type | Description |
|---|---|
list[NDArray[int32]]
|
A list of polygons, where each polygon is represented by a NumPy array
of shape |
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.polygon_to_mask(polygon: npt.NDArray[np.number], resolution_wh: tuple[int, int]) -> npt.NDArray[np.uint8]
¶
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[uint8]
|
np.ndarray: The generated 2D mask, where the polygon is marked with
|
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.polygon_to_xyxy(polygon: npt.NDArray[np.number]) -> npt.NDArray[np.number]
¶
Converts a polygon represented by a NumPy array into a bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A polygon represented by a NumPy array of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
A 1D NumPy array containing the bounding box
|
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.xyxy_to_mask(boxes: npt.NDArray[np.number], resolution_wh: tuple[int, int]) -> npt.NDArray[np.bool_]
¶
Converts a 2D np.ndarray of bounding boxes into a 3D np.ndarray of bool masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A 2D |
required |
|
tuple[int, int]
|
A tuple |
required |
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A 3D |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> boxes = np.array([[0, 0, 2, 2]])
>>> sv.xyxy_to_mask(boxes, (5, 5))
array([[[ True, True, True, False, False],
[ True, True, True, False, False],
[ True, True, True, False, False],
[False, False, False, False, False],
[False, False, False, False, False]]])
>>> boxes = np.array([[0, 0, 1, 1], [3, 3, 4, 4]])
>>> sv.xyxy_to_mask(boxes, (5, 5))
array([[[ True, True, False, False, False],
[ True, True, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False]],
<BLANKLINE>
[[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, True, True],
[False, False, False, True, True]]])