Skip to content

Commit 2b52c16

Browse files
committed
Change from_string usage
1 parent bdf14e4 commit 2b52c16

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

src/viam/components/camera/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ 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, CameraMimeType.from_string(response.mime_type))
44+
return ViamImage(response.image, response.mime_type)
4545

4646
async def get_images(
4747
self,
@@ -57,10 +57,10 @@ async def get_images(
5757
imgs = []
5858
for img_data in response.images:
5959
if img_data.mime_type:
60-
mime_type = CameraMimeType.from_string(img_data.mime_type)
60+
mime_type = img_data.mime_type
6161
else:
6262
# TODO(RSDK-11728): remove this once we deleted the format field
63-
mime_type = CameraMimeType.from_proto(img_data.format)
63+
mime_type = str(CameraMimeType.from_proto(img_data.format))
6464
img = NamedImage(img_data.source_name, img_data.image, mime_type)
6565
imgs.append(img)
6666
resp_metadata: ResponseMetadata = response.response_metadata

src/viam/components/camera/service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ async def GetImages(self, stream: Stream[GetImagesRequest, GetImagesResponse]) -
5858
)
5959
img_bytes_lst = []
6060
for img in images:
61-
mime_type = CameraMimeType.from_string(img.mime_type)
62-
if isinstance(mime_type, CameraMimeType):
61+
try:
62+
mime_type = CameraMimeType.from_string(img.mime_type) # this can ValueError if the mime_type is not a CameraMimeType
6363
fmt = mime_type.to_proto()
64-
else:
64+
except ValueError:
65+
# TODO(RSDK-11728): remove this once we deleted the format field
6566
fmt = Format.FORMAT_UNSPECIFIED
6667

6768
img_bytes = img.data

src/viam/media/video.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from array import array
22
from enum import Enum
3-
from typing import List, Optional, Tuple, Union
3+
from typing import List, Optional, Self, Tuple
44

55
from viam.errors import NotSupportedError
66
from viam.proto.component.camera import Format
@@ -16,7 +16,7 @@ class CameraMimeType(str, Enum):
1616
PCD = "pointcloud/pcd"
1717

1818
@classmethod
19-
def from_string(cls, value: str) -> Union["CameraMimeType", str]:
19+
def from_string(cls, value: str) -> Self:
2020
"""Return the mimetype from a string.
2121
2222
Args:
@@ -29,7 +29,7 @@ def from_string(cls, value: str) -> Union["CameraMimeType", str]:
2929
try:
3030
return cls(value_mime)
3131
except ValueError:
32-
return value_mime
32+
raise ValueError(f"Invalid mimetype: {value}")
3333

3434
@classmethod
3535
def from_proto(cls, format: Format.ValueType) -> "CameraMimeType":

src/viam/services/vision/service.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ 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-
mime_type = CameraMimeType.from_string(request.mime_type)
83-
image = ViamImage(request.image, mime_type)
82+
image = ViamImage(request.image, request.mime_type)
8483

8584
result = await vision.get_detections(image, extra=extra, timeout=timeout)
8685
response = GetDetectionsResponse(detections=result)
@@ -105,8 +104,7 @@ async def GetClassifications(self, stream: Stream[GetClassificationsRequest, Get
105104
extra = struct_to_dict(request.extra)
106105
timeout = stream.deadline.time_remaining() if stream.deadline else None
107106

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

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

0 commit comments

Comments
 (0)