Boxes Utils¶
supervision.detection.utils.boxes.move_boxes(xyxy: npt.NDArray[np.number], offset: npt.NDArray[np.integer]) -> npt.NDArray[np.number]
¶
Move axis-aligned boxes by an (x, y) offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
An array of shape |
required |
|
NDArray[integer]
|
An array of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
Repositioned bounding boxes. Integer inputs produce |
NDArray[number]
|
the translated coordinates fit; otherwise, they produce |
NDArray[number]
|
Floating-point inputs preserve NumPy's existing addition behavior. |
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 src/supervision/detection/utils/boxes.py
supervision.detection.utils.boxes.scale_boxes(xyxy: npt.NDArray[np.number], factor: float) -> npt.NDArray[np.floating]
¶
Scale the dimensions of bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
An integer or floating-point array of shape |
required |
|
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[floating]
|
Scaled bounding boxes. Integer input arrays produce |
NDArray[floating]
|
floating-point inputs preserve their existing dtype. |
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 src/supervision/detection/utils/boxes.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |
supervision.detection.utils.boxes.clip_boxes(xyxy: npt.NDArray[np.number], resolution_wh: tuple[int, int]) -> npt.NDArray[np.number]
¶
Clips bounding boxes coordinates to fit within the frame resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A numpy array of shape |
required |
|
tuple[int, int]
|
A tuple of the form
|
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, 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 src/supervision/detection/utils/boxes.py
supervision.detection.utils.boxes.pad_boxes(xyxy: npt.NDArray[np.number], px: int, py: int | None = None) -> npt.NDArray[np.number]
¶
Pads bounding boxes coordinates with a constant padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A numpy array of shape |
required |
|
int
|
The padding value to be added to both the left and right sides of each bounding box. |
required |
|
int | None
|
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[number]
|
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 src/supervision/detection/utils/boxes.py
supervision.detection.utils.boxes.denormalize_boxes(xyxy: npt.NDArray[np.number], resolution_wh: tuple[int, int], normalization_factor: float = 1.0) -> npt.NDArray[np.number]
¶
Convert normalized bounding box coordinates to absolute pixel coordinates.
Multiplies each bounding box coordinate by image size and divides by
normalization_factor, mapping values from normalized [0, normalization_factor]
to absolute pixel values for a given resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
Normalized bounding boxes of shape |
required |
|
tuple[int, int]
|
Target image resolution as |
required |
|
float
|
Maximum value of input coordinate range.
Defaults to |
1.0
|
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
Array of shape |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> xyxy = np.array([
... [0.1, 0.2, 0.5, 0.6],
... [0.3, 0.4, 0.7, 0.8],
... [0.2, 0.1, 0.6, 0.5]
... ])
>>> sv.denormalize_boxes(xyxy, (1280, 720))
array([[128., 144., 640., 432.],
[384., 288., 896., 576.],
[256., 72., 768., 360.]])
>>> xyxy = np.array([
... [256., 128., 768., 640.]
... ])
>>> sv.denormalize_boxes(xyxy, (1280, 720), normalization_factor=1024.0)
array([[320., 90., 960., 450.]])
Source code in src/supervision/detection/utils/boxes.py
supervision.detection.utils.boxes.xyxyxyxy_to_xyxy(xyxyxyxy: npt.NDArray[np.number]) -> npt.NDArray[np.number]
¶
Convert oriented bounding box corners to axis-aligned bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
OBB corner coordinates with shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
Axis-aligned bounding boxes as an array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> corners = np.array([
... [[0, 0], [10, 0], [10, 5], [0, 5]],
... [[5, 5], [15, 5], [15, 10], [5, 10]],
... ], dtype=np.float32)
>>> sv.xyxyxyxy_to_xyxy(corners)
array([[ 0., 0., 10., 5.],
[ 5., 5., 15., 10.]], dtype=float32)