Skip to content

Boxes2D

SUMMARY

A batch of axis-aligned bounding boxes in 2D space.

python
from telekinesis import datatypes
boxes2d = datatypes.Boxes2D([[1, 2.5, 3, 3], [4, 5, 2, 1]])
API Reference
Complete API documentation for Boxes2D, including parameters, attributes, and methods.
View Reference →

Parameters

ParameterTypeDefaultDescription
datanp.ndarray | list | tupleRequiredBatch of box coordinates, each row [cx, cy, width, height] (center point + size, the native CXCYWH format); shape (N, 4).

Raises

ExceptionCondition
TypeErrordata can't be converted into a uniform float32 array (e.g. ragged nested lists)
ValueErrordata is not rank-2 (e.g. a flat (4,) single box — wrap it as [[...]], or use Box2D)
ValueErrorThe last axis is not exactly length 4
ValueErrorAny element is non-finite (NaN/Inf)
ValueErrorAny box's width or height (data[:, 2], data[:, 3]) is negative

A zero-sized width/height on any box is allowed but logs a warning (that box's area will be 0).

Attributes

AttributeTypeDescription
datanp.ndarrayDefensive copy of the underlying (N, 4) array. Reading it returns a copy; writing re-validates the same way as construction.
shapetuple[int, ...](N, 4).
ndimintAlways 2.
dtypenp.dtypeAlways float32.
sizeintN * 4.
shape_spectuple[int | None, ...]Class-level shape spec (None, 4)None means variable batch size.
centersnp.ndarray, shape (N, 2)Per-box [cx, cy]data[:, :2] directly.
dimensionsnp.ndarray, shape (N, 2)Per-box [width, height] (data[:, 2:4]), non-negative.
areasnp.ndarray, shape (N,)Per-box width * height.

Methods

MethodTypeDescription
Boxes2D.coerce(value)Boxes2DConverts array-like data into a Boxes2D. If value is already a Boxes2D, it is returned unchanged; otherwise it goes through the same checks as constructing one directly.
Boxes2D.from_xywh(data)Boxes2DBuilds a batch from rows of [x_min, y_min, width, height], converting each one to the native center-based format.
Boxes2D.from_xyxy(data)Boxes2DBuilds a batch from rows of [x_min, y_min, x_max, y_max], converting each one to the native center-based format.
as_xywh()np.ndarrayReturns these boxes as rows of [x_min, y_min, width, height] instead of the native center-based format.
as_xyxy()np.ndarrayReturns these boxes as rows of [x_min, y_min, x_max, y_max] instead of the native center-based format.
to_numpy(copy=True)np.ndarrayReturns the boxes as a plain array. Pass copy=False for a zero-copy view instead — mutating it mutates the Boxes2D.
copy()Boxes2DReturns a new, independent Boxes2D with the same data.

Operators

OperationBehavior
boxes == otherTrue only if other is also a Boxes2D with element-equal data (same N, same values). False for anything else.
len(boxes)Number of boxes, N.
boxes[i] (int)Returns a Box2D for row i. Negative indices count from the end; out-of-range raises IndexError.
boxes[i:j] (slice)Returns a new Boxes2D with the selected rows.
boxes[mask] (boolean np.ndarray)Returns a new Boxes2D with the rows where mask is True. Raises ValueError if the mask isn't a 1-D boolean array of length N.
for box in boxesIterates via indexed access (Boxes2D defines __getitem__ but not __iter__); yields one Box2D per row, stopping at the IndexError from an out-of-range index.
np.asarray(boxes)Returns a copy of data as an np.ndarray; NumPy functions accept a Boxes2D directly.

Visualization

python
import rerun as rr

# Your code block
# ....

rr.init("boxes2d_example", spawn=True)
datatypes.visualize(boxes2d, entity_path="/boxes2d", label="Boxes2D")

Example

python
"""Demonstrates the Telekinesis Boxes2D datatype."""

import time

import numpy as np
import rerun as rr
from loguru import logger

from telekinesis import datatypes

def boxes2d_example():
    """Demonstrate creation, inspection, operations, visualization, and serialization."""

    # ======================= Create ============================================
    # Boxes2D format is CXCYWH = [[cx, cy, width, height], ...]
    box2d_1 = [[1, 2.5, 3, 3]]
    box2d_2 = [[4, 5, 2, 1]]
    coords = np.concatenate([box2d_1, box2d_2], axis=0)
    boxes2d = datatypes.Boxes2D(coords)
    logger.info(f"Original Boxes2D: {boxes2d}")

    xyxy_coords = [[1.0, 1.5, 4.0, 4.5], [3.0, 4.5, 5.0, 5.5]]
    boxes2d_from_xyxy = datatypes.Boxes2D.from_xyxy(xyxy_coords)
    logger.info(f"Boxes2D created from xyxy format: {boxes2d_from_xyxy}")

    xywh_coords = [[1.0, 1.5, 3.0, 3.0], [3.0, 4.5, 2.0, 1.0]]
    boxes2d_from_xywh = datatypes.Boxes2D.from_xywh(xywh_coords)
    logger.info(f"Boxes2D created from xywh format: {boxes2d_from_xywh}")

    # ======================= Inspect ===========================================
    logger.info(f"data={boxes2d.data}")
    logger.info(f"dtype={boxes2d.dtype}")
    logger.info(f"ndim={boxes2d.ndim}")
    logger.info(f"shape={boxes2d.shape}")
    logger.info(f"size={boxes2d.size}")
    logger.info(f"dimensions={boxes2d.dimensions}")
    logger.info(f"areas={boxes2d.areas}")
    logger.info(f"centers={boxes2d.centers}")

    # ======================= Operations =========================================
    updated_box = [3, 4, 3, 5]
    data = boxes2d.data
    data[1] = updated_box
    boxes2d.data = data
    logger.info(f"Updated Boxes2D: {boxes2d}")

    xyxy_view = boxes2d.as_xyxy()
    logger.info(f"Boxes2D converted to xyxy format: {xyxy_view}")

    xywh_view = boxes2d.as_xywh()
    logger.info(f"Boxes2D converted to xywh format: {xywh_view}")

    boxes2d_copy = boxes2d.copy()
    logger.info(f"Copied Boxes2D: {boxes2d_copy}")

    # Returns the internal data as a NumPy array. If copy=True, returns a copy; otherwise, returns a view.
    boxes2d_numpy = boxes2d.to_numpy(copy=False)
    logger.info(f"NumPy Boxes2D:\n{boxes2d_numpy}")

    numpy_boxes2d = np.asarray(boxes2d)
    logger.info(f"Boxes2D via __array__:\n{numpy_boxes2d}")

    logger.info(f"Number of boxes: {len(boxes2d)}")
    first_box2d = boxes2d[0]
    sub_batch = boxes2d[1:]
    logger.info(f"First box: {first_box2d}")
    logger.info(f"Sub-batch [1:]: {sub_batch}")

    # Translate and scale by operating on the underlying NumPy array directly.
    translation = [2, 3]
    translated_data = boxes2d.data.copy()
    translated_data[:, :2] += translation
    translated_boxes2d = datatypes.Boxes2D(translated_data)
    logger.info(f"Translated Boxes2D: {translated_boxes2d}")

    scale_factors = [0.5, 0.5]
    scaled_data = boxes2d.data.copy()
    scaled_data[:, 2:] *= np.asarray(scale_factors, dtype=np.float32)
    scaled_boxes2d = datatypes.Boxes2D(scaled_data)
    logger.info(f"Scaled Boxes2D: {scaled_boxes2d}")

    # ======================= Visualize =========================================
    rr.init("boxes2d_example", spawn=True)
    datatypes.visualize(
        boxes2d, entity_path="/boxes2d/updated", label=["Updated Box2D 1", "Updated Box2D 2"]
    )
    datatypes.visualize(
        translated_boxes2d,
        entity_path="/boxes2d/translated",
        label=["Translated Box2D 1", "Translated Box2D 2"],
    )
    datatypes.visualize(
        scaled_boxes2d,
        entity_path="/boxes2d/scaled",
        label=["Scaled Box2D 1", "Scaled Box2D 2"],
    )

    # ======================= Serialize / Deserialize ===========================
    start = time.perf_counter()
    serialized = datatypes.serialize(boxes2d)
    serialization_ms = (time.perf_counter() - start) * 1000

    start = time.perf_counter()
    deserialized = datatypes.deserialize(serialized)["param_0"]
    deserialization_ms = (time.perf_counter() - start) * 1000

    logger.info(f"Deserialized Boxes2D: {deserialized}")
    logger.info(f"Round-trip successful: {boxes2d == deserialized}")
    logger.info(f"Serialization time: {serialization_ms:.3f} ms")
    logger.info(f"Deserialization time: {deserialization_ms:.3f} ms")


if __name__ == "__main__":
    boxes2d_example()