Skip to content

pixano_inference.schemas.rle

Pydantic models for compressed and numeric RLE masks.

CompressedRLE(**data)

Bases: BaseModel

Compressed or numeric RLE mask type.

Attributes:

Name Type Description
size list[int]

Mask size.

counts bytes | list[int]

Mask RLE encoding as compressed bytes/string or a numeric array.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """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.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            stacklevel=2,
        )

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_inference/schemas/rle.py
@staticmethod
def from_mask(mask: 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 = mask_to_rle(mask)
    return CompressedRLE(size=rle["size"], counts=rle["counts"], **kwargs)

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_inference/schemas/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 rle_to_mask({"size": self.size, "counts": self.counts})

mask_to_rle(mask)

Encode mask from Pillow or NumPy array to RLE.

Parameters:

Name Type Description Default
mask Image | ndarray

Mask as Pillow or NumPy array.

required

Returns:

Type Description
dict

Mask as RLE.

Source code in pixano_inference/schemas/rle.py
def mask_to_rle(mask: Image | np.ndarray) -> dict:
    """Encode mask from Pillow or NumPy array to RLE.

    Args:
        mask: Mask as Pillow or NumPy array.

    Returns:
        Mask as RLE.
    """
    mask_array = np.asfortranarray(mask)
    return mask_api.encode(mask_array)

rle_to_mask(rle)

Decode mask from RLE to NumPy array.

Parameters:

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

Mask as RLE.

required

Returns:

Type Description
ndarray

Mask as NumPy array.

Source code in pixano_inference/schemas/rle.py
def rle_to_mask(rle: dict[str, list[int] | bytes]) -> np.ndarray:
    """Decode mask from RLE to NumPy array.

    Args:
        rle: Mask as RLE.

    Returns:
        Mask as NumPy array.
    """
    return mask_api.decode(rle)