|
1 | 1 | import abc
|
2 |
| -from typing import Any, Final, List, Mapping, Optional |
| 2 | +import sys |
| 3 | +from typing import Any, Final, List, Mapping, Optional, Union |
3 | 4 |
|
4 | 5 | from viam.media.video import ViamImage
|
5 | 6 | from viam.proto.common import PointCloudObject
|
6 |
| -from viam.proto.service.vision import Classification, Detection |
| 7 | +from viam.proto.service.vision import Classification, Detection, GetPropertiesResponse |
7 | 8 | from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_SERVICE, Subtype
|
8 | 9 |
|
| 10 | +if sys.version_info >= (3, 10): |
| 11 | + from typing import TypeAlias |
| 12 | +else: |
| 13 | + from typing_extensions import TypeAlias |
| 14 | + |
9 | 15 | from ..service_base import ServiceBase
|
10 | 16 |
|
11 | 17 |
|
| 18 | +class CaptureAllResult: |
| 19 | + """ |
| 20 | + CaptureAllResult represents the collection of things that you have requested from the |
| 21 | + CaptureAllFromCamera method. This is used most often for visualization purposes, since normally, |
| 22 | + returning the image on every call to a classifier/detector/etc would be costly and unnecessary. |
| 23 | + The default result for each field is None rather than the empty list to distinguish between |
| 24 | + "there was no request for the classifier/detector to return a result" vs. |
| 25 | + "the classifier/detector was requested, but there were no results". |
| 26 | + """ |
| 27 | + def __init__(self, image=None, classifications=None, detections=None, objects=None, extra={}): |
| 28 | + """ |
| 29 | + Args: |
| 30 | + image (ViamImage|None): The image from the GetImage request of the camera, if it was requested. |
| 31 | + classifications (List[Classification]|None): The classifications from GetClassifications, if it was requested. |
| 32 | + detections (List[Detection]|None): The detections from GetDetections, if it was requested. |
| 33 | + objects (List[PointCloudObject]|None): the object point clouds from GetObjectPointClouds, if it was requested. |
| 34 | + extra (dict): A catch all structure, usually for metadata, that a module writer might want to return. Default empty. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + None |
| 38 | + """ |
| 39 | + self.image: Union[ViamImage, None] = image |
| 40 | + self.detections: Union[List[Detection], None] = detections |
| 41 | + self.classifications: Union[List[Classification], None] = classifications |
| 42 | + self.objects: Union[List[PointCloudObject], None] = objects |
| 43 | + self.extra: dict = extra |
| 44 | + |
| 45 | + |
12 | 46 | class Vision(ServiceBase):
|
13 | 47 | """
|
14 | 48 | Vision represents a Vision service.
|
15 | 49 |
|
16 | 50 | This acts as an abstract base class for any drivers representing specific
|
17 |
| - arm implementations. This cannot be used on its own. If the ``__init__()`` function is |
| 51 | + vision implementations. This cannot be used on its own. If the ``__init__()`` function is |
18 | 52 | overridden, it must call the ``super().__init__()`` function.
|
19 | 53 | """
|
20 |
| - |
21 | 54 | SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
|
22 | 55 | RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_SERVICE, "vision"
|
23 | 56 | )
|
24 | 57 |
|
| 58 | + Properties: "TypeAlias" = GetPropertiesResponse |
| 59 | + """ |
| 60 | + Properties is a class that states what features are supported on the associated vision service. |
| 61 | + Currently, these are the following properties: |
| 62 | + - classifications_supported (bool): GetClassifications and GetClassificationsFromCamera are implemented. |
| 63 | + - detections_supported (bool): GetDetections and GetDetectionsFromCamera are implemented. |
| 64 | + - object_point_clouds_supported (bool): GetObjectPointClouds is implemented. |
| 65 | + """ |
| 66 | + |
| 67 | + @abc.abstractmethod |
| 68 | + async def capture_all_from_camera( |
| 69 | + self, |
| 70 | + camera_name: str, |
| 71 | + return_image: bool = False, |
| 72 | + return_classifications: bool = False, |
| 73 | + return_detections: bool = False, |
| 74 | + return_object_point_clouds: bool = False, |
| 75 | + *, |
| 76 | + extra: Optional[Mapping[str, Any]] = None, |
| 77 | + timeout: Optional[float] = None, |
| 78 | + ) -> CaptureAllResult: |
| 79 | + """Get the next image, detections, classifications, and objects all together, |
| 80 | + given a camera name. Used for visualization. |
| 81 | +
|
| 82 | + :: |
| 83 | +
|
| 84 | + camera_name = "cam1" |
| 85 | +
|
| 86 | + # Grab the detector you configured on your machine |
| 87 | + my_detector = VisionClient.from_robot(robot, "my_detector") |
| 88 | +
|
| 89 | + # capture all from the next image from the camera |
| 90 | + result = await my_detector.capture_all_from_camera( |
| 91 | + camera_name, |
| 92 | + return_image=True, |
| 93 | + return_detections=True, |
| 94 | + ) |
| 95 | +
|
| 96 | + Args: |
| 97 | + camera_name (str): The name of the camera to use for detection |
| 98 | + return_image (bool): Ask the vision service to return the camera's latest image |
| 99 | + return_classifications (bool): Ask the vision service to return its latest classifications |
| 100 | + return_detections (bool): Ask the vision service to return its latest detections |
| 101 | + return_object_point_clouds (bool): Ask the vision service to return its latest 3D segmentations |
| 102 | +
|
| 103 | + Returns: |
| 104 | + vision.CaptureAllResult: A class that stores all potential returns from the vision service. |
| 105 | + It can return the image from the camera along with its associated detections, classifications, |
| 106 | + and objects, as well as any extra info the model may provide. |
| 107 | + """ |
| 108 | + ... |
| 109 | + |
25 | 110 | @abc.abstractmethod
|
26 | 111 | async def get_detections_from_camera(
|
27 | 112 | self,
|
@@ -195,3 +280,26 @@ async def get_object_point_clouds(
|
195 | 280 | List[viam.proto.common.PointCloudObject]: The pointcloud objects with metadata
|
196 | 281 | """
|
197 | 282 | ...
|
| 283 | + |
| 284 | + @abc.abstractmethod |
| 285 | + async def get_properties( |
| 286 | + self, |
| 287 | + *, |
| 288 | + extra: Optional[Mapping[str, Any]] = None, |
| 289 | + timeout: Optional[float] = None, |
| 290 | + ) -> Properties: |
| 291 | + """ |
| 292 | + Get info about what vision methods the vision service provides. Currently returns boolean values that |
| 293 | + state whether the service implements the classification, detection, and/or 3D object segmentation methods. |
| 294 | +
|
| 295 | + :: |
| 296 | + # Grab the detector you configured on your machine |
| 297 | + my_detector = VisionClient.from_robot(robot, "my_detector") |
| 298 | + properties = await my_detector.get_properties() |
| 299 | + properties.detections_supported # returns True |
| 300 | + properties.classifications_supported # returns False |
| 301 | +
|
| 302 | + Returns: |
| 303 | + Properties: The properties of the vision service |
| 304 | + """ |
| 305 | + ... |
0 commit comments