Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions luxonis_ml/data/datasets/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Any, ClassVar, Dict, List, Literal, Optional, Tuple, TypedDict, Union

import numpy as np
import numpy.typing as npt
import pycocotools.mask as mask_util
from PIL import Image, ImageDraw
from pydantic import (
Expand Down Expand Up @@ -194,7 +193,7 @@ class SegmentationAnnotation(Annotation):
@abstractmethod
def to_numpy(
self, class_mapping: Dict[str, int], width: int, height: int
) -> npt.NDArray[np.bool_]:
) -> np.ndarray:
"""Converts the annotation to a numpy array."""
pass

Expand Down Expand Up @@ -257,9 +256,7 @@ def get_value(self) -> Dict[str, Any]:
"counts": rle["counts"].decode("utf-8"),
}

def to_numpy(
self, _: Dict[str, int], width: int, height: int
) -> npt.NDArray[np.bool_]:
def to_numpy(self, _: Dict[str, int], width: int, height: int) -> np.ndarray:
assert isinstance(self.counts, bytes)
return mask_util.decode(
{"counts": self.counts, "size": [height, width]}
Expand Down Expand Up @@ -312,7 +309,7 @@ def _validate_shape(mask: np.ndarray) -> np.ndarray:

@field_validator("mask", mode="before")
@staticmethod
def _validate_mask(mask: Any) -> npt.NDArray[np.bool_]:
def _validate_mask(mask: Any) -> np.ndarray:
if not isinstance(mask, np.ndarray):
raise ValueError("Mask must be a numpy array")
return mask.astype(np.bool_)
Expand All @@ -327,7 +324,7 @@ def get_value(self) -> Dict[str, Any]:
"counts": rle["counts"].decode("utf-8"), # type: ignore
}

def to_numpy(self, *_) -> npt.NDArray[np.bool_]:
def to_numpy(self, *_) -> np.ndarray:
return self.mask


Expand All @@ -343,9 +340,7 @@ class PolylineSegmentationAnnotation(SegmentationAnnotation):

points: List[Tuple[NormalizedFloat, NormalizedFloat]] = Field(min_length=3)

def to_numpy(
self, _: Dict[str, int], width: int, height: int
) -> npt.NDArray[np.bool_]:
def to_numpy(self, _: Dict[str, int], width: int, height: int) -> np.ndarray:
polyline = [(round(x * width), round(y * height)) for x, y in self.points]
mask = Image.new("L", (width, height), 0)
draw = ImageDraw.Draw(mask)
Expand Down
5 changes: 2 additions & 3 deletions luxonis_ml/data/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Dict, Iterator, Tuple

import numpy as np
import numpy.typing as npt


def check_array(path: Path) -> None:
Expand Down Expand Up @@ -30,10 +29,10 @@ def _check_valid_array(path: Path) -> bool:


def rgb_to_bool_masks(
segmentation_mask: npt.NDArray[np.uint8],
segmentation_mask: np.ndarray,
class_colors: Dict[str, Tuple[int, int, int]],
add_background_class: bool = False,
) -> Iterator[Tuple[str, npt.NDArray[np.bool_]]]:
) -> Iterator[Tuple[str, np.ndarray]]:
"""Helper function to convert an RGB segmentation mask to boolean masks for each
class.

Expand Down