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[number]
|
A numpy array of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
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[number]
|
A numpy array of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
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[number]
|
Bounding box in format |
required |
Returns:
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[number]
|
A numpy array of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
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[number]
|
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]
|
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[number]
|
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]
|
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]]])
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.rle_to_mask(rle: npt.NDArray[np.integer[Any]] | list[int] | str | bytes, resolution_wh: tuple[int, int]) -> npt.NDArray[np.bool_]
¶
Converts a COCO run-length encoding (RLE) to a binary mask.
Implements the COCO RLE format used by pycocotools: pixels are counted
in column-major (Fortran) order — top-to-bottom within each column,
left-to-right across columns. This is the opposite of the row-major order
used by NumPy's default 'C' layout. Passing RLE data produced by a
different row-major convention will yield an incorrect mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[integer[Any]] | list[int] | str | bytes
|
The COCO RLE data in one of the following formats:
|
required |
|
tuple[int, int]
|
The width (w) and height (h) of the desired binary mask. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
The generated 2D Boolean mask of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sum of pixels encoded in RLE differs from the number of pixels in the expected mask (computed based on resolution_wh). |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> mask = sv.rle_to_mask([5, 2, 2, 2, 5], (4, 4))
>>> mask # doctest: +NORMALIZE_WHITESPACE
array([[False, False, False, False],
[False, True, True, False],
[False, True, True, False],
[False, False, False, False]])
>>> mask = sv.rle_to_mask("52203", (4, 4))
>>> mask # doctest: +NORMALIZE_WHITESPACE
array([[False, False, False, False],
[False, True, True, False],
[False, True, True, False],
[False, False, False, False]])
Source code in src/supervision/detection/utils/converters.py
supervision.detection.utils.converters.mask_to_rle(mask: npt.NDArray[np.bool_], compressed: bool = False) -> list[int] | str
¶
Converts a binary mask into a COCO run-length encoding (RLE).
Produces RLE in the COCO format used by pycocotools: pixels are counted
in column-major (Fortran) order — top-to-bottom within each column,
left-to-right across columns. The output is directly compatible with
pycocotools.mask.decode and COCO annotation JSON files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[bool_]
|
2D binary mask where |
required |
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
list[int] | str
|
The COCO run-length encoded mask. When |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If input mask is not 2D or is empty. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> mask = np.array([
... [True, True, True, True],
... [True, True, True, True],
... [True, True, True, True],
... [True, True, True, True],
... ])
>>> rle = sv.mask_to_rle(mask)
>>> [int(x) for x in rle]
[0, 16]
>>> 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],
... ])
>>> rle = sv.mask_to_rle(mask)
>>> [int(x) for x in rle]
[5, 2, 2, 2, 5]
>>> sv.mask_to_rle(mask, compressed=True)
'52203'

Source code in src/supervision/detection/utils/converters.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |