PyBBoxes: Light Weight Toolkit for Bounding Boxes

0.2.0 · active · verified Thu Apr 16

PyBBoxes is a lightweight Python toolkit designed for handling bounding box operations. It provides abstractions for various bounding box formats (e.g., Pascal VOC, YOLO, COCO, Albumentations, FiftyOne) and allows for seamless conversions, common mathematical operations, scaling, clamping, and IoU computations. The current version is 0.2.0, and the library maintains an active release cadence with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `BoundingBox` object from Pascal VOC coordinates, convert it to YOLO format, calculate Intersection over Union (IoU) with another box, and apply scaling and clamping operations. It highlights the use of both the `BoundingBox` class and functions from `pybboxes.functional`.

from pybboxes import BoundingBox
from pybboxes.functional import bbox_iou

# Example coordinates: (x_min, y_min, x_max, y_max)
voc_bbox_values = (10, 20, 100, 120)
image_size = (640, 480) # (width, height)

# Create a BoundingBox object from Pascal VOC format
# 'strict=False' is the default since v0.1.5, allowing out-of-bounds boxes
bbox1 = BoundingBox(voc_bbox_values, image_size=image_size, from_type="voc")

print(f"Original VOC (bbox1): {bbox1.values}")
print(f"Normalized (bbox1): {bbox1.to_yolo().values}")

# Convert to another format, e.g., YOLO
yolo_bbox = bbox1.to_yolo()
print(f"Converted YOLO (bbox1): {yolo_bbox.values}")
print(f"Type: {yolo_bbox.box_type}")

# Perform a functional operation, e.g., Intersection over Union (IoU)
# Note: functional methods moved to pybboxes.functional in v0.1.0
bbox2_values = (15, 25, 105, 115)
bbox2 = BoundingBox(bbox2_values, image_size=image_size, from_type="voc")

iou = bbox_iou(bbox1, bbox2)
print(f"IoU between bbox1 and bbox2: {iou:.4f}")

# Example of scaling and clamping
scaled_bbox = bbox1.scale(scale=1.2)
print(f"Scaled bbox1 (1.2x): {scaled_bbox.values}")

clamped_bbox = scaled_bbox.clamp()
print(f"Clamped scaled bbox1 (to {image_size}): {clamped_bbox.values}")

view raw JSON →