Types¶
types ¶
Classes¶
BBox ¶
Bases: NamedTuple
Bounding box representation in XYXY format.
This class represents a rectangular bounding box using the XYXY coordinate format (top-left and bottom-right corners). It provides conversions to other common formats and utility methods for area calculation.
The internal representation uses XYXY format (x_min, y_min, x_max, y_max) which is the most common format for object detection tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
x_min | float | Minimum X coordinate of the bounding box. |
y_min | float | Minimum Y coordinate of the bounding box. |
x_max | float | Maximum X coordinate of the bounding box. |
y_max | float | Maximum Y coordinate of the bounding box. |
Example
Example
Example
Attributes¶
xyxy property ¶
Return bounding box in XYXY format.
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float] | Tuple of (x_min, y_min, x_max, y_max). |
xywh property ¶
cxcywh property ¶
area property ¶
Functions¶
from_xywh classmethod ¶
from_xywh(x: float, y: float, w: float, h: float) -> BBox
Create BBox from XYWH format (COCO format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | float | Left X coordinate. | required |
y | float | Top Y coordinate. | required |
w | float | Width of the box. | required |
h | float | Height of the box. | required |
Returns:
| Type | Description |
|---|---|
BBox | BBox instance. |
Example
Source code in boxlab/dataset/types.py
from_cxcywh classmethod ¶
from_cxcywh(cx: float, cy: float, w: float, h: float) -> BBox
Create BBox from center format (YOLO format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cx | float | Center X coordinate. | required |
cy | float | Center Y coordinate. | required |
w | float | Width of the box. | required |
h | float | Height of the box. | required |
Returns:
| Type | Description |
|---|---|
BBox | BBox instance. |
Example
Source code in boxlab/dataset/types.py
Annotation ¶
Bases: NamedTuple
Object annotation with bounding box and category information.
Represents a single object annotation in an image, including its spatial location (bounding box) and semantic information (category). Follows COCO annotation conventions.
Attributes:
| Name | Type | Description |
|---|---|---|
bbox | BBox | Bounding box of the annotated object. |
category_id | int | Integer category identifier. |
category_name | str | Human-readable category name. |
image_id | str | ID of the image this annotation belongs to. |
annotation_id | str | None | Optional unique identifier for this annotation. |
area | float | None | Optional pre-computed area. If None, calculated from bbox. |
iscrowd | int | Crowd annotation flag (0=single object, 1=crowd of objects). |
Example
from boxlab.dataset.types import Annotation, BBox
# Create an annotation
annotation = Annotation(
bbox=BBox(x_min=10, y_min=20, x_max=100, y_max=150),
category_id=1,
category_name="person",
image_id="img_001",
annotation_id="ann_001",
area=11700.0,
iscrowd=0,
)
print(f"Object: {annotation.category_name}")
print(f"Location: {annotation.bbox.xyxy}")
print(f"Area: {annotation.get_area()}")
Example
Attributes¶
annotation_id class-attribute instance-attribute ¶
Unique ID of the annotation.
area class-attribute instance-attribute ¶
Area of the annotation, if available.
Functions¶
get_area ¶
Get annotation area.
Returns pre-computed area if available, otherwise calculates from bbox.
Returns:
| Type | Description |
|---|---|
float | Area in square pixels. |
Example
# With pre-computed area
ann1 = Annotation(
bbox=BBox(0, 0, 10, 10),
category_id=1,
category_name="obj",
image_id="1",
area=100.0,
)
print(ann1.get_area()) # 100.0 (uses pre-computed)
# Without pre-computed area
ann2 = Annotation(
bbox=BBox(0, 0, 10, 10),
category_id=1,
category_name="obj",
image_id="1",
)
print(ann2.get_area()) # 100.0 (computed from bbox)
Source code in boxlab/dataset/types.py
ImageInfo ¶
Bases: NamedTuple
Image metadata container.
Stores essential information about an image in the dataset, including dimensions and file location.
Attributes:
| Name | Type | Description |
|---|---|---|
image_id | str | Unique identifier for the image. |
file_name | str | Filename of the image (e.g., "image001.jpg"). |
width | int | Image width in pixels. |
height | int | Image height in pixels. |
path | Path | None | Optional filesystem path to the image file. |
Example
from pathlib import Path
from boxlab.dataset.types import ImageInfo
# Create image metadata
img_info = ImageInfo(
image_id="img_001",
file_name="photo.jpg",
width=1920,
height=1080,
path=Path("/data/images/photo.jpg"),
)
print(f"Image: {img_info.file_name}")
print(f"Resolution: {img_info.width}x{img_info.height}")
print(
f"Exists: {img_info.path.exists() if img_info.path else 'N/A'}"
)
Example
DatasetStatistics ¶
Bases: TypedDict
Dataset statistics container.
TypedDict containing comprehensive statistical information about a dataset, including counts, distributions, and aggregate metrics.
This structure is returned by Dataset.get_statistics() and related methods.
Attributes:
| Name | Type | Description |
|---|---|---|
num_images | int | Total number of images in the dataset. |
num_annotations | int | Total number of annotations across all images. |
num_categories | int | Number of unique object categories. |
category_distribution | dict[str, int] | Mapping of category names to their annotation counts. |
avg_annotations_per_image | float | Mean number of annotations per image. |
std_annotations_per_image | float | Standard deviation of annotations per image. |
min_annotations_per_image | int | Minimum annotations found in any image. |
max_annotations_per_image | int | Maximum annotations found in any image. |
avg_bbox_area | float | Mean bounding box area in square pixels. |
median_bbox_area | float | Median bounding box area in square pixels. |
Example
from boxlab.dataset import Dataset
dataset = Dataset(name="my_dataset")
# ... populate dataset ...
# Get statistics
stats = dataset.get_statistics()
print(f"Images: {stats['num_images']}")
print(f"Annotations: {stats['num_annotations']}")
print(f"Categories: {stats['num_categories']}")
print(
f"Avg objects per image: {stats['avg_annotations_per_image']:.2f}"
)
# Category distribution
for category, count in stats[
"category_distribution"
].items():
print(f" {category}: {count}")
Example
# Analyze bounding box sizes
stats = dataset.get_statistics()
print(f"Average bbox area: {stats['avg_bbox_area']:.2f}")
print(f"Median bbox area: {stats['median_bbox_area']:.2f}")
# Check for annotation imbalance
min_anns = stats["min_annotations_per_image"]
max_anns = stats["max_annotations_per_image"]
if max_anns > min_anns * 10:
print(
"Warning: Large annotation count variance detected"
)
Attributes¶
category_distribution instance-attribute ¶
Distribution of annotations per category.
avg_annotations_per_image instance-attribute ¶
Average number of annotations per image.
std_annotations_per_image instance-attribute ¶
Standard deviation of annotations per image.
min_annotations_per_image instance-attribute ¶
Minimum number of annotations in any image.
max_annotations_per_image instance-attribute ¶
Maximum number of annotations in any image.
SplitRatio ¶
Bases: NamedTuple
Dataset split ratios for train/val/test division.
Defines the proportions for splitting a dataset into training, validation, and test sets. All ratios must sum to 1.0.
Attributes:
| Name | Type | Description |
|---|---|---|
train | float | Training set ratio (default: 0.8). |
val | float | Validation set ratio (default: 0.1). |
test | float | Test set ratio (default: 0.1). |
Raises:
| Type | Description |
|---|---|
ValidationError | If ratios don't sum to 1.0 (within tolerance of 1e-6). |
Example
Example
Example
Example
Attributes¶
Functions¶
validate ¶
Validate that split ratios sum to 1.0.
Checks that train + val + test equals 1.0 within a tolerance of 1e-6.
Raises:
| Type | Description |
|---|---|
ValidationError | If ratios don't sum to 1.0. |
Example
Source code in boxlab/dataset/types.py
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true
Overview¶
The types module defines core data structures used throughout BoxLab. These types provide type-safe, immutable representations of bounding boxes, annotations, images, and dataset metadata.
Bounding Box Formats¶
BoxLab supports three common bounding box coordinate formats:
XYXY Format (Internal)¶
Top-left and bottom-right corners:
- Most common in detection frameworks
- Used internally by BoxLab
- Direct representation of box corners
XYWH Format (COCO)¶
Top-left corner and dimensions:
- Used by COCO dataset
- Convenient for drawing operations
- Common in visualization tools
CXCYWH Format (YOLO)¶
Center point and dimensions:
- Used by YOLO formats
- Natural for certain augmentations
- Often used with normalized coordinates
Format Conversions¶
BBox Conversions¶
from boxlab.dataset.types import BBox
# Create in XYXY (internal format)
bbox = BBox(x_min=10, y_min=20, x_max=100, y_max=150)
# Convert to other formats
xyxy = bbox.xyxy # (10, 20, 100, 150)
xywh = bbox.xywh # (10, 20, 90, 130)
cxcywh = bbox.cxcywh # (55.0, 85.0, 90, 130)
# Create from other formats
bbox1 = BBox.from_xywh(x=10, y=20, w=90, h=130)
bbox2 = BBox.from_cxcywh(cx=55, cy=85, w=90, h=130)
# All represent the same box
assert bbox == bbox1 == bbox2
Usage Patterns¶
Creating Annotations¶
from boxlab.dataset.types import BBox, Annotation
# Complete annotation
annotation = Annotation(
bbox=BBox(10, 20, 100, 150),
category_id=1,
category_name="person",
image_id="img_001",
annotation_id="ann_001",
area=11700.0,
iscrowd=0
)
# Minimal annotation (area computed automatically)
annotation = Annotation(
bbox=BBox(10, 20, 100, 150),
category_id=1,
category_name="person",
image_id="img_001"
)
Image Metadata¶
from pathlib import Path
from boxlab.dataset.types import ImageInfo
# With file path
img_info = ImageInfo(
image_id="001",
file_name="photo.jpg",
width=1920,
height=1080,
path=Path("/data/images/photo.jpg")
)
# Without path (for annotations-only export)
img_info = ImageInfo(
image_id="001",
file_name="photo.jpg",
width=1920,
height=1080
)
Dataset Splitting¶
from boxlab.dataset.types import SplitRatio
# Standard split
split = SplitRatio(train=0.7, val=0.2, test=0.1)
split.validate()
# No test set
split = SplitRatio(train=0.8, val=0.2, test=0.0)
split.validate()
# Use with dataset
splits = dataset.split(split, seed=42)
Analyzing Statistics¶
from boxlab.dataset import Dataset
dataset = Dataset(name="my_dataset")
# ... populate dataset ...
stats = dataset.get_statistics()
# Access statistics
print(f"Total images: {stats['num_images']}")
print(f"Total annotations: {stats['num_annotations']}")
# Category analysis
dist = stats['category_distribution']
most_common = max(dist.items(), key=lambda x: x[1])
print(f"Most common: {most_common[0]} ({most_common[1]} instances)")
# Annotation density
avg = stats['avg_annotations_per_image']
std = stats['std_annotations_per_image']
print(f"Objects per image: {avg:.2f} ± {std:.2f}")
# Box size analysis
print(f"Average box area: {stats['avg_bbox_area']:.2f}")
print(f"Median box area: {stats['median_bbox_area']:.2f}")
Coordinate Systems¶
Pixel Coordinates¶
All bounding box coordinates in BoxLab are in absolute pixel coordinates:
# For a 1920x1080 image
bbox = BBox(x_min=100, y_min=200, x_max=300, y_max=400)
# Width and height in pixels
width = bbox.x_max - bbox.x_min # 200 pixels
height = bbox.y_max - bbox.y_min # 200 pixels
Normalized Coordinates¶
For normalized coordinates (e.g., YOLO format), convert manually:
# Image dimensions
img_width, img_height = 1920, 1080
# Absolute bbox
bbox = BBox(100, 200, 300, 400)
# Normalize
cx, cy, w, h = bbox.cxcywh
cx_norm = cx / img_width # 0.104
cy_norm = cy / img_height # 0.278
w_norm = w / img_width # 0.104
h_norm = h / img_height # 0.185
Coordinate Origin¶
BoxLab uses the top-left corner as origin (0, 0):
Immutability¶
All types are immutable (NamedTuple or TypedDict):
bbox = BBox(10, 20, 100, 150)
# This raises an error
# bbox.x_min = 15 # AttributeError
# Create a new bbox instead
new_bbox = BBox(15, 20, 100, 150)
Type Safety¶
Use type hints for better IDE support:
from boxlab.dataset.types import BBox, Annotation, ImageInfo
def process_bbox(bbox: BBox) -> float:
return bbox.area
def create_annotation(
bbox: BBox,
category_id: int,
category_name: str,
image_id: str
) -> Annotation:
return Annotation(
bbox=bbox,
category_id=category_id,
category_name=category_name,
image_id=image_id
)
Validation¶
SplitRatio Validation¶
from boxlab.dataset.types import SplitRatio
from boxlab.exceptions import ValidationError
# Valid split
split = SplitRatio(0.7, 0.2, 0.1)
split.validate() # OK
# Invalid split
try:
split = SplitRatio(0.5, 0.3, 0.1) # Sums to 0.9
split.validate()
except ValidationError as e:
print(f"Error: {e}")
BBox Validation¶
BBox doesn't validate coordinates by default. Add custom validation if needed:
def validate_bbox(bbox: BBox, img_width: int, img_height: int) -> bool:
"""Check if bbox is within image bounds."""
return (
0 <= bbox.x_min < bbox.x_max <= img_width and
0 <= bbox.y_min < bbox.y_max <= img_height
)
bbox = BBox(10, 20, 100, 150)
is_valid = validate_bbox(bbox, 1920, 1080) # True
See Also¶
- Dataset Core - Dataset management
- Plugin System: Extend dataset functionality
- I/O Operations - Loading and exporting datasets
- PyTorch Adapter - Training integration