Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type checking for img detection messages #3

Merged
merged 5 commits into from
May 30, 2024
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
7 changes: 6 additions & 1 deletion ml/messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
from .img_detections import ImgDetectionsWithKeypoints
from .img_detections import ImgDetectionWithKeypoints, ImgDetectionsWithKeypoints

__all__ = [
"ImgDetectionWithKeypoints",
"ImgDetectionsWithKeypoints",
]
47 changes: 42 additions & 5 deletions ml/messages/img_detections.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import depthai as dai
from typing import List
from typing import List, Tuple, Union


class ImgDetectionWithKeypoints(dai.ImgDetection):
def __init__(self):
dai.ImgDetection.__init__(self)
self.keypoints = [] # TODO: how to enforce type checking for keypoints? e.g. this currently accept strings
dai.ImgDetection.__init__(self) # TODO: change to super().__init__()?
self._keypoints: List[Tuple[float, float]] = []

@property
def keypoints(self) -> List[Tuple[float, float]]:
return self._keypoints

@keypoints.setter
def keypoints(self, value: List[Tuple[Union[int, float], Union[int, float]]]):
if not isinstance(value, list):
raise TypeError("Keypoints must be a list")
for item in value:
if (
not isinstance(item, tuple)
or len(item) != 2
or not all(isinstance(i, (int, float)) for i in item)
):
raise TypeError(
"Each keypoint must be a tuple of two floats or integers"
)
self._keypoints = [(float(x), float(y)) for x, y in value]


class ImgDetectionsWithKeypoints(dai.Buffer):

def __init__(self):
dai.Buffer.__init__(self)
self.detections: List[ImgDetectionWithKeypoints] = [] # TODO: how to enforce type checking for ImgDetectionWithKeypoints? e.g. this currently accept ImgDetection
dai.Buffer.__init__(self) # TODO: change to super().__init__()?
self._detections: List[ImgDetectionWithKeypoints] = []

@property
def detections(self) -> List[ImgDetectionWithKeypoints]:
return self._detections

@detections.setter
def detections(self, value: List[ImgDetectionWithKeypoints]):
if not isinstance(value, list):
raise TypeError("Detections must be a list")
for item in value:
if not isinstance(item, ImgDetectionWithKeypoints):
raise TypeError(
"Each detection must be an instance of ImgDetectionWithKeypoints"
)
self._detections = value