class documentation

Run-length encoded segmentation mask.

The encoded mask uses COCO-style run-length encoding.

This class support parsing segmentation masks from multiple input formats:

  • Run-length encoding (RLE) directly as a list of counts or as a byte string.
  • Binary mask arrays as numpy arrays or saved as .npy or .png files.
  • Polygons as lists of normalized points together with the image width and height.

Example

>>> rle = SegmentationAnnotation(
...     height=4,
...     width=4,
...     counts=b'11213ON0'
... )
>>> mask = SegmentationAnnotation(
...     mask=np.array(
...         [
...            [0, 1, 0, 0],
...            [1, 1, 0, 0],
...            [0, 0, 0, 0],
...            [0, 0, 1, 1],
...         ]
...     )
... )
>>> np.array_equal(rle.to_numpy(), mask.to_numpy())
True

Note

When providing the RLE as a list of counts instead of encoded bytes make sure the counts follow FORTRAN (column-major) order as expected by the COCO RLE format.

Static Method combine_to_numpy Combine segmentation annotations into class masks.
Method to_numpy Convert the segmentation annotation to a binary mask.
Instance Variable counts Run-length encoded mask data.
Instance Variable height The height of the segmentation mask.
Instance Variable width The width of the segmentation mask.
Class Method _validate_mask Undocumented
Class Method _validate_polyline Undocumented
Class Method _validate_rle Undocumented
Static Method _clip_points Undocumented
Static Method _numpy_to_rle Undocumented
Method _serialize_counts Undocumented
@staticmethod
@override
def combine_to_numpy(annotations: list[SegmentationAnnotation], classes: list[int], n_classes: int) -> np.ndarray:

Combine segmentation annotations into class masks.

Note

In case of overlapping annotations, the first mask in the list takes precedence.

Parameters
annotations:list[SegmentationAnnotation]Segmentation annotations to combine.
classes:list[int]Class IDs associated with the annotations.
n_classes:intTotal number of known classes.
Returns
np.ndarrayCombined semantic segmentation masks of shape (C, H, W).
def to_numpy(self) -> np.ndarray:

Convert the segmentation annotation to a binary mask.

Returns
np.ndarrayBinary mask of shape (H, W).
counts: bytes =

Run-length encoded mask data.

height: PositiveInt =

The height of the segmentation mask.

width: PositiveInt =

The width of the segmentation mask.

@model_validator(mode='before')
@classmethod
def _validate_mask(cls, values: dict[str, Any]) -> dict[str, Any]:

Undocumented

@model_validator(mode='before')
@classmethod
def _validate_polyline(cls, values: dict[str, Any]) -> dict[str, Any]:

Undocumented

@model_validator(mode='before')
@classmethod
def _validate_rle(cls, values: dict[str, Any]) -> dict[str, Any]:

Undocumented

@staticmethod
def _clip_points(points: list[tuple[float, float]]):

Undocumented

@staticmethod
def _numpy_to_rle(mask: np.ndarray) -> dict[str, Any]:

Undocumented

@field_serializer('counts', when_used='json')
def _serialize_counts(self, counts: bytes) -> str:

Undocumented