API reference
The reference is generated from the package's public docstrings, but organized around the way you use the library rather than dumped into one long page.
Main model
Bbox
Bases: BaseModel
A class to represent a Bbox (inherits from Pydantic BaseModel).
The bbox is stored in Pascal_VOC format: top-left, bottom-right with a top-left origin (PIL coord system). (meaning that top < bottom)
The bottom and right edges are considered excluded from the Bbox for compatibility with array slicing and PIL image cropping features (in case of Int Bboxes).
Attributes:
| Name | Type | Description |
|---|---|---|
left |
float
|
The left coordinate of the bounding box. |
top |
float
|
The top coordinate of the bounding box. |
right |
float
|
The right coordinate of the bounding box. |
bottom |
float
|
The bottom coordinate of the bounding box. |
from_tlbr
classmethod
Initializes the bounding box from top-left and bottom-right coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tlbr
|
Sequence[float]
|
A sequence containing the top-left and bottom-right coordinates of the bounding box in the format (left, top, right, bottom). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The Bbox instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the length of the sequence is not 4, or if the Bbox is not valid
(ie |
Examples:
from_tlwh
classmethod
Initializes the bounding box from top-left and width-height coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tlwh
|
Sequence[float]
|
A sequence containing the top-left and width-height coordinates of the bounding box in the format (left, top, width, height). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The Bbox instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the length of the sequence is not 4, or if the Bbox is not valid
(ie |
Examples:
from_cwh
classmethod
Initializes the bounding box from center and width-height coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cwh
|
Sequence[float]
|
A sequence containing the center and width-height coordinates of the bounding box in the format (center_x, center_y, width, height). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The Bbox instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the length of the sequence is not 4, or if the Bbox is not valid
(ie |
Examples:
to_tlbr
Returns the bounding box coordinates in Top-Left, Bottom-Right format.
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: The bounding box coordinates (x_min, y_min, x_max, y_max). x_min and y_min are the coordinates of the top-left corner of the bounding box. x_max and y_max are the coordinates of the bottom-right corner of the bounding box. |
to_list
Returns the bounding box coordinates in Top-Left, Bottom-Right format.
Returns:
| Type | Description |
|---|---|
List[float]
|
List[float]: The bounding box coordinates [x_min, y_min, x_max, y_max]. x_min and y_min are the coordinates of the top-left corner of the bounding box. x_max and y_max are the coordinates of the bottom-right corner of the bounding box. |
to_norm_tlbr
Returns the bounding box coordinates in Top-Left, Bottom-Right format, normalized based on the image dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_w
|
int
|
The image width in pixels. |
required |
img_h
|
int
|
The image height in pixels. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: The bounding box coordinates (x_min, y_min, x_max, y_max). x_min and y_min are the coordinates of the top-left corner of the bounding box. x_max and y_max are the coordinates of the bottom-right corner of the bounding box. All the returned values are NORMALIZED based on the image dimensions. |
to_tlwh
Returns the bounding box coordinates in Top-Left, Width-Height format.
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: The bounding box coordinates (x_min, y_min, width, height). x_min and y_min are coordinates of the top-left corner of the bounding box. |
to_norm_tlwh
Returns the bounding box coordinates in Top-Left, Width-Height format, normalized based on the image dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_w
|
int
|
The image width in pixels. |
required |
img_h
|
int
|
The image height in pixels. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: The bounding box coordinates [x_min, y_min, width, height]. x_min and y_min are the coordinates of the top-left corner of the bounding box. All the returned values are NORMALIZED based on the image dimensions. |
to_cwh
Returns the bounding box coordinates in Center, Width-Height format.
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: The bounding box coordinates (x_center, y_center, width, height). |
to_norm_cwh
Returns the bounding box coordinates in Center, Width-Height format, normalized based on the image dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_w
|
int
|
The image width in pixels. |
required |
img_h
|
int
|
The image height in pixels. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: The NORMALIZED bounding box coordinates (x_center, y_center, width, height). |
to_polygon
to_polygon() -> Tuple[
Tuple[float, float],
Tuple[float, float],
Tuple[float, float],
Tuple[float, float],
]
Returns the bounding box corners as points.
Returns:
| Type | Description |
|---|---|
Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float]]
|
The corners coordinates in (x, y) format.
The order is |
to_int_tuple
to_int_tuple(
rounding_method: RoundingMethod = RoundingMethod.ROUND,
) -> Tuple[int, int, int, int]
Returns the bounding box coordinates in Top-Left, Bottom-Right format, with values rounded to int.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rounding_method
|
RoundingMethod
|
The rounding method to use. Defaults to
|
ROUND
|
Returns:
| Type | Description |
|---|---|
Tuple[int, int, int, int]
|
Tuple[int, int, int, int]: The coordinates as integers. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a wrong rounding method is provided. |
shift
Return a shifted Bbox by the specified horizontal and vertical amounts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
horizontal_shift
|
float
|
The amount to shift the bounding box horizontally. Defaults to 0. |
0
|
vertical_shift
|
float
|
The amount to shift the bounding box vertically. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The shifted Bbox instance. |
scale
Return a scaled Bbox by the specified scale factor. The scaling will be from the center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale_factor
|
float
|
The factor to scale the bounding box by. Width and height will be scaled by this factor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The scaled Bbox instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the scale is strictly negative. |
scale_area
Return a scaled Bbox such that new area/old area == scale_factor.
The scaling will be from the center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale_factor
|
float
|
The factor to scale the bounding box by. The area will be scaled by this factor. (Meaning width and height will be scaled by the square root of this factor.) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The scaled Bbox instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the scale is strictly negative. |
expand_uniform
Return an expanded Bbox by the specified padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
padding
|
float
|
The amount to expand the bounding box by. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The expanded Bbox instance. |
expand
Return an expanded Bbox by the specified padding for each side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
float
|
The amount to expand the left side of the bounding box by. Defaults to 0. |
0
|
top
|
float
|
The amount to expand the top side of the bounding box by. Defaults to 0. |
0
|
right
|
float
|
The amount to expand the right side of the bounding box by. Defaults to 0. |
0
|
bottom
|
float
|
The amount to expand the bottom side of the bounding box by. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The expanded Bbox instance. |
pad_to_aspect_ratio
Returns a padded Bbox to achieve the target aspect ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_ratio
|
float
|
The target aspect ratio. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
A Bbox instance padded to the correct ratio. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If target_ratio is <= 0. |
clip_to_img
Returns a clipped Bbox to the image dimensions.
Remember that the bottom and right edges are considered excluded from the bbox, so
Bbox(left=-10, top=-20, right=100, bottom=120).clip_to_img(img_w=32, img_h=64)
returns Bbox(left=0, top=0, right=32, bottom=64)
Note that this method can return a zero area bounding box if the bbox is completely out of the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_w
|
int
|
The image width in pixels. |
required |
img_h
|
int
|
The image height in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The clipped Bbox. |
overlaps
Checks if the current bounding box overlaps with another bounding box.
Two bboxes are considered as overlapping if they intersect with a non-zero area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Bbox
|
The other bounding box to check for overlap. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the bounding boxes overlap, False otherwise. |
contains_point
Checks if a point is inside the bounding box. Note that point on the right or bottom edges are not considered inside.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x-coordinate of the point. |
required |
y
|
float
|
The y-coordinate of the point. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the point is inside the bounding box, False otherwise. |
contains
Checks if the other Bbox is contained by this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Bbox
|
The other bounding box. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if this bounding box contains the other one. |
is_inside
Checks if this Bbox is contained by the other one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Bbox
|
The other bounding box. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if this bounding box is contained by the other one. |
union
intersection
Calculates the intersection with another Bbox. If the resulting Bbox is not valid (ie
left > right or top > bottom, returns None.
This operation can return a zero area Bbox if the two bounding boxes are just touching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Bbox
|
The other bounding box to calculate the intersection with. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Bbox]
|
Optional[Bbox]: The intersection of the two bounding boxes if valid. |
iou
Calculates the Intersection over Union (IoU) with another bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Bbox
|
The other Bbox. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The IoU between the two bounding boxes. |
distance_to_point
Calculates the distance from the bounding box to a point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x-coordinate of the point. |
required |
y
|
float
|
The y-coordinate of the point. |
required |
dist
|
DistanceMetric
|
The distance metric to use. Defaults to |
L2
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The distance from the bounding box to the point. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a wrong distance metric is provided. |
distance_to_bbox
Calculates the distance between the edges of this Bbox and another Bbox. Distance is zero if the boxes overlap or touch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Bbox
|
The other bounding box. |
required |
dist
|
DistanceMetric
|
The distance metric to use. Defaults to |
L2
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The distance between the two bounding boxes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a wrong distance metric is provided. |
Enums
DistanceMetric
Bases: Enum
RoundingMethod
Bases: Enum
Utilities
nms
nms(
bboxes: List[Bbox],
scores: List[float],
iou_threshold: float = 0.5,
) -> List[Tuple[Bbox, float]]
Perform Non-Maximum Suppression on a list of bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bboxes
|
List[Bbox]
|
List of bounding boxes. |
required |
scores
|
List[float]
|
List of confidence scores for each bounding box. |
required |
iou_threshold
|
float
|
IoU threshold for suppression. Defaults to 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
List[Tuple[Bbox, float]]
|
List[Tuple[Bbox, float]]: List of selected bounding boxes and their scores. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the length of bboxes and scores do not match. |
bbox_union
Calculate the union of a list of bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bboxes
|
Sequence[Bbox]
|
A sequence of bounding boxes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Bbox |
Bbox
|
The bounding box that represents the union of all input bounding boxes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list of bounding boxes is empty. |
bbox_intersection
Calculate the intersection of a list of bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bboxes
|
Sequence[Bbox]
|
A sequence of bounding boxes. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Bbox]
|
Optional[Bbox]: The bounding box that represents the intersection of all input bounding boxes. If the resulting bounding box is not valid, returns None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list of bounding boxes is empty. |