-
Notifications
You must be signed in to change notification settings - Fork 67
Update CameraMimeType to allow for custom mimes #995
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,61 @@ | ||
| from array import array | ||
| from enum import Enum | ||
| from typing import List, Optional, Tuple | ||
| from typing import Any, List, Optional, Tuple | ||
|
|
||
| from typing_extensions import Self | ||
| from typing_extensions import ClassVar, Self | ||
|
|
||
| from viam.errors import NotSupportedError | ||
| from viam.proto.component.camera import Format | ||
|
|
||
| from .viam_rgba import RGBA_HEADER_LENGTH, RGBA_MAGIC_NUMBER | ||
|
|
||
|
|
||
| class CameraMimeType(str, Enum): | ||
| VIAM_RGBA = "image/vnd.viam.rgba" | ||
| VIAM_RAW_DEPTH = "image/vnd.viam.dep" | ||
| JPEG = "image/jpeg" | ||
| PNG = "image/png" | ||
| PCD = "pointcloud/pcd" | ||
| class _FrozenClassAttributesMeta(type): | ||
| """ | ||
| A metaclass that prevents the reassignment of existing class attributes. | ||
| """ | ||
|
|
||
| def __setattr__(cls, name: str, value: Any): | ||
| # Check if the attribute `name` already exists on the class | ||
| if name in cls.__dict__: | ||
| # If it exists, raise an error to prevent overwriting | ||
| raise AttributeError(f"Cannot reassign constant '{name}'") | ||
| # If it's a new attribute, allow it to be set | ||
| super().__setattr__(name, value) | ||
|
Comment on lines
+12
to
+23
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh this is clever |
||
|
|
||
|
|
||
| class CameraMimeType(str, metaclass=_FrozenClassAttributesMeta): | ||
| """ | ||
| The compatible mime-types for cameras and vision services. | ||
|
|
||
| You can use the `CameraMimeType.CUSTOM(...)` method to use an unlisted mime-type. | ||
| """ | ||
|
|
||
| VIAM_RGBA: ClassVar[Self] | ||
| VIAM_RAW_DEPTH: ClassVar[Self] | ||
| JPEG: ClassVar[Self] | ||
| PNG: ClassVar[Self] | ||
| PCD: ClassVar[Self] | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| for key, value in self.__class__.__dict__.items(): | ||
| if value == self: | ||
| return key | ||
| return "CUSTOM" | ||
|
|
||
| @property | ||
| def value(self) -> str: | ||
| return self | ||
|
|
||
| @classmethod | ||
| def CUSTOM(cls, mime_type: str) -> Self: | ||
| """ | ||
| Create a custom mime type. | ||
|
|
||
| Args: | ||
| mime_type (str): The mimetype as a string | ||
| """ | ||
| return cls.from_string(mime_type) | ||
|
|
||
| @classmethod | ||
| def from_string(cls, value: str) -> Self: | ||
|
|
@@ -28,13 +68,10 @@ def from_string(cls, value: str) -> Self: | |
| Self: The mimetype | ||
| """ | ||
| value_mime = value[:-5] if value.endswith("+lazy") else value # ViamImage lazy encodes by default | ||
| try: | ||
| return cls(value_mime) | ||
| except ValueError: | ||
| raise ValueError(f"Invalid mimetype: {value}") | ||
| return cls(value_mime) | ||
|
|
||
| @classmethod | ||
| def from_proto(cls, format: Format.ValueType) -> "CameraMimeType": | ||
| def from_proto(cls, format: Format.ValueType) -> Self: | ||
| """Returns the mimetype from a proto enum. | ||
|
|
||
| Args: | ||
|
|
@@ -44,14 +81,15 @@ def from_proto(cls, format: Format.ValueType) -> "CameraMimeType": | |
| Self: The mimetype. | ||
| """ | ||
| mimetypes = { | ||
| Format.FORMAT_RAW_RGBA: CameraMimeType.VIAM_RGBA, | ||
| Format.FORMAT_RAW_DEPTH: CameraMimeType.VIAM_RAW_DEPTH, | ||
| Format.FORMAT_JPEG: CameraMimeType.JPEG, | ||
| Format.FORMAT_PNG: CameraMimeType.PNG, | ||
| Format.FORMAT_RAW_RGBA: cls.VIAM_RGBA, | ||
| Format.FORMAT_RAW_DEPTH: cls.VIAM_RAW_DEPTH, | ||
| Format.FORMAT_JPEG: cls.JPEG, | ||
| Format.FORMAT_PNG: cls.PNG, | ||
| } | ||
| return mimetypes.get(format, CameraMimeType.JPEG) | ||
| return cls(mimetypes.get(format, cls.JPEG)) | ||
|
|
||
| def to_proto(self) -> Format.ValueType: | ||
| @property | ||
| def proto(self) -> Format.ValueType: | ||
| """Returns the mimetype in a proto enum. | ||
|
|
||
| Returns: | ||
|
|
@@ -65,6 +103,19 @@ def to_proto(self) -> Format.ValueType: | |
| } | ||
| return formats.get(self, Format.FORMAT_UNSPECIFIED) | ||
|
|
||
| def to_proto(self) -> Format.ValueType: | ||
| """ | ||
| DEPRECATED: Use `CameraMimeType.proto` | ||
| """ | ||
| return self.proto | ||
|
|
||
|
|
||
| CameraMimeType.VIAM_RGBA = CameraMimeType.from_string("image/vnd.viam.rgba") | ||
| CameraMimeType.VIAM_RAW_DEPTH = CameraMimeType.from_string("image/vnd.viam.dep") | ||
| CameraMimeType.JPEG = CameraMimeType.from_string("image/jpeg") | ||
| CameraMimeType.PNG = CameraMimeType.from_string("image/png") | ||
| CameraMimeType.PCD = CameraMimeType.from_string("pointcloud/pcd") | ||
|
|
||
|
|
||
| class ViamImage: | ||
| """A native implementation of an image. | ||
|
|
@@ -73,11 +124,11 @@ class ViamImage: | |
| """ | ||
|
|
||
| _data: bytes | ||
| _mime_type: str | ||
| _mime_type: CameraMimeType | ||
| _height: Optional[int] = None | ||
| _width: Optional[int] = None | ||
|
|
||
| def __init__(self, data: bytes, mime_type: str) -> None: | ||
| def __init__(self, data: bytes, mime_type: CameraMimeType) -> None: | ||
| self._data = data | ||
| self._mime_type = mime_type | ||
| self._width, self._height = _getDimensions(data, mime_type) | ||
|
|
@@ -88,7 +139,7 @@ def data(self) -> bytes: | |
| return self._data | ||
|
|
||
| @property | ||
| def mime_type(self) -> str: | ||
| def mime_type(self) -> CameraMimeType: | ||
| """The mime type of the image""" | ||
| return self._mime_type | ||
|
|
||
|
|
@@ -131,12 +182,12 @@ class NamedImage(ViamImage): | |
| """The name of the image | ||
| """ | ||
|
|
||
| def __init__(self, name: str, data: bytes, mime_type: str) -> None: | ||
| def __init__(self, name: str, data: bytes, mime_type: CameraMimeType) -> None: | ||
| self.name = name | ||
| super().__init__(data, mime_type) | ||
|
|
||
|
|
||
| def _getDimensions(image: bytes, mime_type: str) -> Tuple[Optional[int], Optional[int]]: | ||
| def _getDimensions(image: bytes, mime_type: CameraMimeType) -> Tuple[Optional[int], Optional[int]]: | ||
| try: | ||
| if mime_type == CameraMimeType.JPEG: | ||
| return _getDimensionsFromJPEG(image) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why these Sequence changes? Does it type-hint grpc repeated sets better?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea exactly -- a superset of
listthat allows us to save some casts