Skip to content

pixano.features.schemas.annotations.compressed_rle

CompressedRLE(created_at=None, updated_at=None, **data)

Bases: Annotation

Compressed RLE mask type.

Attributes:

Name Type Description
size list[int]

Mask size.

counts bytes

Mask RLE encoding.

Source code in pixano/features/schemas/base_schema.py
def __init__(self, /, created_at: datetime | None = None, updated_at: datetime | None = None, **data: Any):
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.

    Args:
        created_at: The creation date of the object.
        updated_at: The last modification date of the object.
        data: The data of the object validated by Pydantic.
    """
    created_at, updated_at = validate_and_init_create_at_and_update_at(created_at, updated_at)
    data.update({"created_at": created_at, "updated_at": updated_at})
    super().__init__(**data)

encode(mask, height, width, **kwargs) staticmethod

Create a compressed RLE mask from polygons / uncompressed RLE / compressed RLE.

Parameters:

Name Type Description Default
mask list[list] | dict[str, list[int]]

Mask as polygons / uncompressed RLE / compressed RLE.

required
height int

Image height.

required
width int

Image width.

required
kwargs Any

Additional arguments.

{}

Returns:

Type Description
CompressedRLE

The compressed RLE mask.

Source code in pixano/features/schemas/annotations/compressed_rle.py
@staticmethod
def encode(mask: list[list] | dict[str, list[int]], height: int, width: int, **kwargs: Any) -> "CompressedRLE":
    """Create a compressed RLE mask from polygons / uncompressed RLE / compressed RLE.

    Args:
        mask: Mask as polygons / uncompressed RLE / compressed RLE.
        height: Image height.
        width: Image width.
        kwargs: Additional arguments.

    Returns:
        The compressed RLE mask.
    """
    return CompressedRLE(**image_utils.encode_rle(mask, height, width), **kwargs)

from_mask(mask, **kwargs) staticmethod

Create a compressed RLE mask from a NumPy array.

Parameters:

Name Type Description Default
mask Image | ndarray

The mask as a NumPy array.

required
kwargs Any

Additional arguments.

{}

Returns:

Type Description
CompressedRLE

The compressed RLE mask.

Source code in pixano/features/schemas/annotations/compressed_rle.py
@staticmethod
def from_mask(mask: pil_image.Image | np.ndarray, **kwargs: Any) -> "CompressedRLE":
    """Create a compressed RLE mask from a NumPy array.

    Args:
        mask: The mask as a NumPy array.
        kwargs: Additional arguments.

    Returns:
        The compressed RLE mask.
    """
    rle = image_utils.mask_to_rle(mask)
    return CompressedRLE(size=rle["size"], counts=rle["counts"], **kwargs)

from_polygons(polygons, height, width, **kwargs) staticmethod

Create a compressed RLE mask from polygons.

Parameters:

Name Type Description Default
polygons list[list]

The mask as polygons.

required
height int

Image height.

required
width int

Image width.

required
kwargs Any

Additional arguments.

{}

Returns:

Type Description
CompressedRLE

The compressed RLE mask.

Source code in pixano/features/schemas/annotations/compressed_rle.py
@staticmethod
def from_polygons(
    polygons: list[list],
    height: int,
    width: int,
    **kwargs: Any,
) -> "CompressedRLE":
    """Create a compressed RLE mask from polygons.

    Args:
        polygons: The mask as polygons.
        height: Image height.
        width: Image width.
        kwargs: Additional arguments.

    Returns:
        The compressed RLE mask.
    """
    return CompressedRLE(**image_utils.polygons_to_rle(polygons, height, width), **kwargs)

from_urle(urle, **kwargs) staticmethod

Create a compressed RLE mask from an uncompressed RLE.

Parameters:

Name Type Description Default
urle dict[str, list[int]]

The mask as an uncompressed RLE.

required
kwargs Any

Additional arguments.

{}

Returns:

Type Description
CompressedRLE

The compressed RLE mask.

Source code in pixano/features/schemas/annotations/compressed_rle.py
@staticmethod
def from_urle(urle: dict[str, list[int]], **kwargs: Any) -> "CompressedRLE":
    """Create a compressed RLE mask from an uncompressed RLE.

    Args:
        urle: The mask as an uncompressed RLE.
        kwargs: Additional arguments.

    Returns:
        The compressed RLE mask.
    """
    return CompressedRLE(**image_utils.urle_to_rle(urle), **kwargs)  #  type: ignore[arg-type]

none() classmethod

Utility function to get a None equivalent. Should be removed as soon as Lance manages None value.

Returns:

Type Description
Self

"None" CompressedRLE.

Source code in pixano/features/schemas/annotations/compressed_rle.py
@classmethod
def none(cls) -> Self:
    """Utility function to get a `None` equivalent.
    Should be removed as soon as Lance manages `None` value.

    Returns:
        "None" `CompressedRLE`.
    """
    return cls(
        id="",
        item=ItemRef.none(),
        view=ViewRef.none(),
        entity=EntityRef.none(),
        size=[0, 0],
        counts=b"",
        created_at=datetime(1970, 1, 1),
        updated_at=datetime(1970, 1, 1),
    )

to_mask()

Convert the compressed RLE mask to a NumPy array.

Returns:

Type Description
ndarray

The mask as a NumPy array.

Source code in pixano/features/schemas/annotations/compressed_rle.py
def to_mask(self) -> np.ndarray:
    """Convert the compressed RLE mask to a NumPy array.

    Returns:
        The mask as a NumPy array.
    """
    return image_utils.rle_to_mask({"size": self.size, "counts": self.counts})

to_polygons()

Convert the compressed RLE mask to poylgons.

Returns:

Type Description
list[list]

The mask as polygons.

Source code in pixano/features/schemas/annotations/compressed_rle.py
def to_polygons(self) -> list[list]:
    """Convert the compressed RLE mask to poylgons.

    Returns:
        The mask as polygons.
    """
    return image_utils.rle_to_polygons(self.model_dump())

to_urle()

Convert compressed RLE mask to uncompressed RLE.

Returns:

Type Description
dict[str, list[int]]

The mask as an uncompressed RLE.

Source code in pixano/features/schemas/annotations/compressed_rle.py
def to_urle(self) -> dict[str, list[int]]:
    """Convert compressed RLE mask to uncompressed RLE.

    Returns:
        The mask as an uncompressed RLE.
    """
    return image_utils.rle_to_urle(self.model_dump())

create_compressed_rle(size, counts, id='', item_ref=ItemRef.none(), view_ref=ViewRef.none(), entity_ref=EntityRef.none(), source_ref=SourceRef.none())

Create a CompressedRLE instance.

Parameters:

Name Type Description Default
size list[int]

Mask size.

required
counts bytes

Mask RLE encoding.

required
id str

CompressedRLE ID.

''
item_ref ItemRef

Item reference.

none()
view_ref ViewRef

View reference.

none()
entity_ref EntityRef

Entity reference.

none()
source_ref SourceRef

Source reference.

none()

Returns:

Type Description
CompressedRLE

The compressed RLE instance.

Source code in pixano/features/schemas/annotations/compressed_rle.py
def create_compressed_rle(
    size: list[int],
    counts: bytes,
    id: str = "",
    item_ref: ItemRef = ItemRef.none(),
    view_ref: ViewRef = ViewRef.none(),
    entity_ref: EntityRef = EntityRef.none(),
    source_ref: SourceRef = SourceRef.none(),
) -> CompressedRLE:
    """Create a `CompressedRLE` instance.

    Args:
        size: Mask size.
        counts: Mask RLE encoding.
        id: `CompressedRLE` ID.
        item_ref: Item reference.
        view_ref: View reference.
        entity_ref: Entity reference.
        source_ref: Source reference.

    Returns:
        The compressed RLE instance.
    """
    return CompressedRLE(
        size=size,
        counts=counts,
        id=id,
        item_ref=item_ref,
        view_ref=view_ref,
        entity_ref=entity_ref,
        source_ref=source_ref,
    )

is_compressed_rle(cls, strict=False)

Check if the given class is a subclass of CompressedRLE.

Source code in pixano/features/schemas/annotations/compressed_rle.py
def is_compressed_rle(cls: type, strict: bool = False) -> bool:
    """Check if the given class is a subclass of `CompressedRLE`."""
    return issubclass_strict(cls, CompressedRLE, strict)