Skip to content

pixano.core.bbox

BBox(coords, format, is_normalized=True, confidence=None)

Bases: PixanoType, BaseModel

Bounding box type using coordinates in xyxy or xywh format

Attributes:

Name Type Description
coords list[float]

List of coordinates in given format

format str

Coordinates format, 'xyxy' or 'xywh'

is_normalized bool

True if coordinates are normalized to image size

confidence float

Bounding box confidence if predicted

Parameters:

Name Type Description Default
coords list[float]

List of coordinates in given format

required
format str

Coordinates format, 'xyxy' or 'xywh'

required
is_normalized bool

True if coordinates are normalized to image size. Defaults to True.

True
confidence float

Bounding box confidence if predicted. Defaults to None.

None
Source code in pixano/core/bbox.py
def __init__(
    self,
    coords: list[float],
    format: str,
    is_normalized: bool = True,
    confidence: float = None,
):
    """Initialize Bounding box

    Args:
        coords (list[float]): List of coordinates in given format
        format (str): Coordinates format, 'xyxy' or 'xywh'
        is_normalized (bool, optional): True if coordinates are normalized to image size. Defaults to True.
        confidence (float, optional): Bounding box confidence if predicted. Defaults to None.
    """

    # Define public attributes through Pydantic BaseModel
    super().__init__()

    # Define private attributes manually
    self._coords = coords
    self._format = format
    self._is_normalized = is_normalized
    self._confidence = confidence

confidence: float property

Return bounding box confidence

Returns:

Type Description
float

Bounding box confidence if predicted, else None

coords: list[float] property

Return bounding box coordinates

Returns:

Type Description
list[float]

Coordinates

format: str property

Return bounding box coordinates format

Returns:

Type Description
str

Coordinates format, 'xyxy' or 'xywh'

is_normalized: bool property

Return bounding box normalization information

Returns:

Type Description
bool

True if coordinates are normalized to image size

is_predicted: bool property

Return True if bounding box is predicted and has a confidence value

Returns:

Type Description
bool

True if bounding box is predicted and has a confidence value

xywh_coords: list[float] property

Return bounding box xywh coordinates

Returns:

Type Description
list[float]

Coordinates in xywh format

xyxy_coords: list[float] property

Return bounding box xyxy coordinates

Returns:

Type Description
list[float]

Coordinates in xyxy format

denormalize(height, width)

Return bounding box with coordinates denormalized from image size

Parameters:

Name Type Description Default
height int

Image height

required
width int

Image width

required

Returns:

Type Description
BBox

Bounding box with coordinates denormalized from image size

Source code in pixano/core/bbox.py
def denormalize(self, height: int, width: int) -> "BBox":
    """Return bounding box with coordinates denormalized from image size

    Args:
        height (int): Image height
        width (int): Image width

    Returns:
        BBox: Bounding box with coordinates denormalized from image size
    """

    return BBox(
        denormalize_coords(self.coords, height, width),
        self.format,
        False,
        self.confidence,
    )

from_mask(mask) staticmethod

Create bounding box using a NumPy array mask

Parameters:

Name Type Description Default
mask ndarray

NumPy array mask

required

Returns:

Type Description
Bbox

Bounding box

Source code in pixano/core/bbox.py
@staticmethod
def from_mask(mask: np.ndarray) -> "BBox":
    """Create bounding box using a NumPy array mask

    Args:
        mask (np.ndarray): NumPy array mask

    Returns:
        Bbox: Bounding box
    """

    return BBox.from_xywh(mask_to_bbox(mask))

from_rle(rle) staticmethod

Create bounding box using a RLE mask

Parameters:

Name Type Description Default
rle CompressedRLE

RLE mask

required

Returns:

Type Description
Bbox

Bounding box

Source code in pixano/core/bbox.py
@staticmethod
def from_rle(rle: CompressedRLE) -> "BBox":
    """Create bounding box using a RLE mask

    Args:
        rle (CompressedRLE): RLE mask

    Returns:
        Bbox: Bounding box
    """

    return BBox.from_mask(rle.to_mask())

from_xywh(xywh, confidence=None) staticmethod

Create bounding box using normalized xywh coordinates

Parameters:

Name Type Description Default
xywh list[float]

List of coordinates in xywh format

required
confidence float

Bounding box confidence if predicted. Defaults to None.

None

Returns:

Type Description
Bbox

Bounding box

Source code in pixano/core/bbox.py
@staticmethod
def from_xywh(xywh: list[float], confidence: float = None) -> "BBox":
    """Create bounding box using normalized xywh coordinates

    Args:
        xywh (list[float]): List of coordinates in xywh format
        confidence (float, optional): Bounding box confidence if predicted. Defaults to None.

    Returns:
        Bbox: Bounding box
    """

    return BBox(xywh, "xywh", confidence=confidence)

from_xyxy(xyxy, confidence=None) staticmethod

Create bounding box using normalized xyxy coordinates

Parameters:

Name Type Description Default
xyxy list[float]

List of coordinates in xyxy format

required
confidence float

Bounding box confidence if predicted. Defaults to None.

None

Returns:

Type Description
Bbox

Bounding box

Source code in pixano/core/bbox.py
@staticmethod
def from_xyxy(xyxy: list[float], confidence: float = None) -> "BBox":
    """Create bounding box using normalized xyxy coordinates

    Args:
        xyxy (list[float]): List of coordinates in xyxy format
        confidence (float, optional): Bounding box confidence if predicted. Defaults to None.

    Returns:
        Bbox: Bounding box
    """

    return BBox(xyxy, "xyxy", confidence=confidence)

normalize(height, width)

Return bounding box with coordinates normalized to image size

Parameters:

Name Type Description Default
height int

Image height

required
width int

Image width

required

Returns:

Type Description
BBox

Bounding box with coordinates normalized to image size

Source code in pixano/core/bbox.py
def normalize(self, height: int, width: int) -> "BBox":
    """Return bounding box with coordinates normalized to image size

    Args:
        height (int): Image height
        width (int): Image width

    Returns:
        BBox: Bounding box with coordinates normalized to image size
    """

    return BBox(
        normalize_coords(self.coords, height, width),
        self.format,
        True,
        self.confidence,
    )

to_struct() staticmethod

Return BBox type as PyArrow Struct

Returns:

Type Description
StructType

Custom type corresponding PyArrow Struct

Source code in pixano/core/bbox.py
@staticmethod
def to_struct() -> pa.StructType:
    """Return BBox type as PyArrow Struct

    Returns:
        pa.StructType: Custom type corresponding PyArrow Struct
    """

    return pa.struct(
        [
            pa.field("coords", pa.list_(pa.float32(), list_size=4)),
            pa.field("is_normalized", pa.bool_()),
            pa.field("format", pa.string()),
            pa.field("confidence", pa.float32()),
        ]
    )

to_xywh()

Return bounding box in xywh format

Returns:

Type Description
BBox

Bounding box in xyxy format

Source code in pixano/core/bbox.py
def to_xywh(self) -> "BBox":
    """Return bounding box in xywh format

    Returns:
        BBox: Bounding box in xyxy format
    """

    return BBox(self.xywh_coords, "xywh", self.is_normalized, self.confidence)

to_xyxy()

Return bounding box in xyxy format

Returns:

Type Description
BBox

Bounding box in xyxy format

Source code in pixano/core/bbox.py
def to_xyxy(self) -> "BBox":
    """Return bounding box in xyxy format

    Returns:
        BBox: Bounding box in xyxy format
    """

    return BBox(self.xyxy_coords, "xyxy", self.is_normalized, self.confidence)