Skip to content

Commit 28f67f4

Browse files
authored
Revert "[RSDK-11704, RSDK-11603] Update Python SDK with new Ge… (#991)
1 parent b16bf1c commit 28f67f4

File tree

6 files changed

+19
-46
lines changed

6 files changed

+19
-46
lines changed

src/viam/components/camera/camera.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,7 @@ async def get_image(
6363
...
6464

6565
@abc.abstractmethod
66-
async def get_images(
67-
self,
68-
*,
69-
filter_source_names: Optional[List[str]] = None,
70-
extra: Optional[Dict[str, Any]] = None,
71-
timeout: Optional[float] = None,
72-
**kwargs,
73-
) -> Tuple[List[NamedImage], ResponseMetadata]:
66+
async def get_images(self, *, timeout: Optional[float] = None, **kwargs) -> Tuple[List[NamedImage], ResponseMetadata]:
7467
"""Get simultaneous images from different imagers, along with associated metadata.
7568
This should not be used for getting a time series of images from the same imager.
7669

src/viam/components/camera/client.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,20 @@ async def get_image(
4141
md = kwargs.get("metadata", self.Metadata()).proto
4242
request = GetImageRequest(name=self.name, mime_type=mime_type, extra=dict_to_struct(extra))
4343
response: GetImageResponse = await self.client.GetImage(request, timeout=timeout, metadata=md)
44-
return ViamImage(response.image, response.mime_type)
44+
return ViamImage(response.image, CameraMimeType.from_string(response.mime_type))
4545

4646
async def get_images(
4747
self,
4848
*,
49-
filter_source_names: Optional[List[str]] = None,
50-
extra: Optional[Dict[str, Any]] = None,
5149
timeout: Optional[float] = None,
5250
**kwargs,
5351
) -> Tuple[List[NamedImage], ResponseMetadata]:
5452
md = kwargs.get("metadata", self.Metadata()).proto
55-
request = GetImagesRequest(name=self.name, extra=dict_to_struct(extra), filter_source_names=filter_source_names)
53+
request = GetImagesRequest(name=self.name)
5654
response: GetImagesResponse = await self.client.GetImages(request, timeout=timeout, metadata=md)
5755
imgs = []
5856
for img_data in response.images:
59-
if img_data.mime_type:
60-
mime_type = img_data.mime_type
61-
else:
62-
# TODO(RSDK-11728): remove this once we deleted the format field
63-
mime_type = str(CameraMimeType.from_proto(img_data.format))
57+
mime_type = CameraMimeType.from_proto(img_data.format)
6458
img = NamedImage(img_data.source_name, img_data.image, mime_type)
6559
imgs.append(img)
6660
resp_metadata: ResponseMetadata = response.response_metadata

src/viam/components/camera/service.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
from google.api.httpbody_pb2 import HttpBody # type: ignore
44
from grpclib.server import Stream
55

6-
from viam.media.video import CameraMimeType
76
from viam.proto.common import DoCommandRequest, DoCommandResponse, GetGeometriesRequest, GetGeometriesResponse
87
from viam.proto.component.camera import (
98
CameraServiceBase,
10-
Format,
119
GetImageRequest,
1210
GetImageResponse,
1311
GetImagesRequest,
@@ -50,23 +48,12 @@ async def GetImages(self, stream: Stream[GetImagesRequest, GetImagesResponse]) -
5048
camera = self.get_resource(name)
5149

5250
timeout = stream.deadline.time_remaining() if stream.deadline else None
53-
images, metadata = await camera.get_images(
54-
timeout=timeout,
55-
metadata=stream.metadata,
56-
extra=struct_to_dict(request.extra),
57-
filter_source_names=list(request.filter_source_names),
58-
)
51+
images, metadata = await camera.get_images(timeout=timeout, metadata=stream.metadata)
5952
img_bytes_lst = []
6053
for img in images:
61-
try:
62-
mime_type = CameraMimeType.from_string(img.mime_type) # this can ValueError if the mime_type is not a CameraMimeType
63-
fmt = mime_type.to_proto()
64-
except ValueError:
65-
# TODO(RSDK-11728): remove this once we deleted the format field
66-
fmt = Format.FORMAT_UNSPECIFIED
67-
54+
fmt = img.mime_type.to_proto()
6855
img_bytes = img.data
69-
img_bytes_lst.append(Image(source_name=name, mime_type=img.mime_type, format=fmt, image=img_bytes))
56+
img_bytes_lst.append(Image(source_name=name, format=fmt, image=img_bytes))
7057
response = GetImagesResponse(images=img_bytes_lst, response_metadata=metadata)
7158
await stream.send_message(response)
7259

src/viam/media/video.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ def from_string(cls, value: str) -> Self:
2828
Self: The mimetype
2929
"""
3030
value_mime = value[:-5] if value.endswith("+lazy") else value # ViamImage lazy encodes by default
31-
try:
32-
return cls(value_mime)
33-
except ValueError:
34-
raise ValueError(f"Invalid mimetype: {value}")
31+
return cls(value_mime)
3532

3633
@classmethod
3734
def from_proto(cls, format: Format.ValueType) -> "CameraMimeType":
@@ -73,11 +70,11 @@ class ViamImage:
7370
"""
7471

7572
_data: bytes
76-
_mime_type: str
73+
_mime_type: CameraMimeType
7774
_height: Optional[int] = None
7875
_width: Optional[int] = None
7976

80-
def __init__(self, data: bytes, mime_type: str) -> None:
77+
def __init__(self, data: bytes, mime_type: CameraMimeType) -> None:
8178
self._data = data
8279
self._mime_type = mime_type
8380
self._width, self._height = _getDimensions(data, mime_type)
@@ -88,7 +85,7 @@ def data(self) -> bytes:
8885
return self._data
8986

9087
@property
91-
def mime_type(self) -> str:
88+
def mime_type(self) -> CameraMimeType:
9289
"""The mime type of the image"""
9390
return self._mime_type
9491

@@ -131,12 +128,12 @@ class NamedImage(ViamImage):
131128
"""The name of the image
132129
"""
133130

134-
def __init__(self, name: str, data: bytes, mime_type: str) -> None:
131+
def __init__(self, name: str, data: bytes, mime_type: CameraMimeType) -> None:
135132
self.name = name
136133
super().__init__(data, mime_type)
137134

138135

139-
def _getDimensions(image: bytes, mime_type: str) -> Tuple[Optional[int], Optional[int]]:
136+
def _getDimensions(image: bytes, mime_type: CameraMimeType) -> Tuple[Optional[int], Optional[int]]:
140137
try:
141138
if mime_type == CameraMimeType.JPEG:
142139
return _getDimensionsFromJPEG(image)

src/viam/services/vision/service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ async def GetDetections(self, stream: Stream[GetDetectionsRequest, GetDetections
7979
extra = struct_to_dict(request.extra)
8080
timeout = stream.deadline.time_remaining() if stream.deadline else None
8181

82-
image = ViamImage(request.image, request.mime_type)
82+
mime_type = CameraMimeType.from_string(request.mime_type)
83+
image = ViamImage(request.image, mime_type)
8384

8485
result = await vision.get_detections(image, extra=extra, timeout=timeout)
8586
response = GetDetectionsResponse(detections=result)
@@ -104,7 +105,8 @@ async def GetClassifications(self, stream: Stream[GetClassificationsRequest, Get
104105
extra = struct_to_dict(request.extra)
105106
timeout = stream.deadline.time_remaining() if stream.deadline else None
106107

107-
image = ViamImage(request.image, request.mime_type)
108+
mime_type = CameraMimeType.from_string(request.mime_type)
109+
image = ViamImage(request.image, mime_type)
108110

109111
result = await vision.get_classifications(image, request.n, extra=extra, timeout=timeout)
110112
response = GetClassificationsResponse(classifications=result)

tests/test_camera.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from viam.proto.component.camera import (
1414
CameraServiceStub,
1515
DistortionParameters,
16+
Format,
1617
GetImageRequest,
1718
GetImageResponse,
1819
GetImagesRequest,
@@ -141,7 +142,6 @@ async def test_get_image(self, camera: MockCamera, service: CameraRPCService, im
141142
request = GetImageRequest(name="camera", mime_type=CameraMimeType.PNG)
142143
response: GetImageResponse = await client.GetImage(request, timeout=18.1)
143144
assert response.image == image.data
144-
assert response.mime_type == CameraMimeType.PNG
145145
assert camera.timeout == loose_approx(18.1)
146146

147147
# Test empty mime type. Empty mime type should default to response mime type
@@ -158,7 +158,7 @@ async def test_get_images(self, camera: MockCamera, service: CameraRPCService, m
158158
request = GetImagesRequest(name="camera")
159159
response: GetImagesResponse = await client.GetImages(request, timeout=18.1)
160160
raw_img = response.images[0]
161-
assert raw_img.mime_type == CameraMimeType.PNG
161+
assert raw_img.format == Format.FORMAT_PNG
162162
assert raw_img.source_name == camera.name
163163
assert response.response_metadata == metadata
164164
assert camera.timeout == loose_approx(18.1)

0 commit comments

Comments
 (0)