mbodied.types.sense package∞
Submodules∞
mbodied.types.sense.sensor_reading module∞
- class mbodied.types.sense.sensor_reading.SensorReading(datum=None, **data)[source]∞
Bases:
Sample
- model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}∞
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ConfigDict = {'arbitrary_types_allowed': True, 'extra': 'allow', 'from_attributes': True, 'use_enum_values': False, 'validate_assignment': False}∞
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[dict[str, FieldInfo]] = {}∞
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].
This replaces Model.__fields__ from Pydantic V1.
mbodied.types.sense.vision module∞
Wrap any common image representation in an Image class to convert to any other common format.
The following image representations are supported: - NumPy array - PIL Image - Base64 encoded string - File path - URL - Bytes object
The image can be resized to and from any size, compressed, and converted to and from any supported format:
```python image = Image(“path/to/image.png”, size=new_size_tuple).save(“path/to/new/image.jpg”) image.save(“path/to/new/image.jpg”, quality=5)
TODO: Implement Lazy attribute loading for the image data.
- class mbodied.types.sense.vision.Image(arg: ndarray | Image | str | Url | Path = None, url: str | None = None, path: str | None = None, base64: str | None = None, array: ndarray | None = None, pil: Image | None = None, encoding: str | None = 'jpeg', size: Tuple | None = None, bytes_obj: bytes | None = None, **kwargs)[source]∞
Bases:
Sample
An image sample that can be represented in various formats.
The image can be represented as a NumPy array, a base64 encoded string, a file path, a PIL Image object, or a URL. The image can be resized to and from any size and converted to and from any supported format.
- array∞
The image represented as a NumPy array.
- Type:
Optional[np.ndarray]
- base64∞
The base64 encoded string of the image.
- Type:
Optional[Base64Str]
- path∞
The file path of the image.
- Type:
Optional[FilePath]
- pil∞
The image represented as a PIL Image object.
- Type:
Optional[PILImage]
- url∞
The URL of the image.
- Type:
Optional[AnyUrl]
- size∞
The size of the image as a (width, height) tuple.
- Type:
Optional[tuple[int, int]]
- encoding∞
The encoding of the image.
- Type:
Optional[Literal[“png”, “jpeg”, “jpg”, “bmp”, “gif”]]
Example
>>> image = Image("https://example.com/image.jpg") >>> image = Image("/path/to/image.jpg") >>> image = Image("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4Q3zaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA")
>>> jpeg_from_png = Image("path/to/image.png", encoding="jpeg") >>> resized_image = Image(image, size=(224, 224)) >>> pil_image = Image(image).pil >>> array = Image(image).array >>> base64 = Image(image).base64
- array: ndarray[Any, dtype[Any]] | Path | MultiArrayNumpyFile∞
- base64: str | None∞
- static bytes_to_data(bytes_data: bytes, encoding: str = 'jpeg', size=None) dict [source]∞
Creates an Image instance from a bytes object.
- Parameters:
bytes_data (bytes) – The bytes object to convert to an image.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- dump(*args, as_field: str | None = None, **kwargs) dict | Any [source]∞
Return a dict or a field of the image.
- encoding: Literal['png', 'jpeg', 'jpg', 'bmp', 'gif']∞
- static from_base64(base64_str: str, encoding: str, size=None) Image [source]∞
Decodes a base64 string to create an Image instance.
- Parameters:
base64_str (str) – The base64 string to decode.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- classmethod from_bytes(bytes_data: bytes, encoding: str = 'jpeg', size=None) Image [source]∞
Creates an Image instance from a bytes object.
- Parameters:
bytes_data (bytes) – The bytes object to convert to an image.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- static load_url(url: str, download=False) Image | None [source]∞
Downloads an image from a URL or decodes it from a base64 data URI.
- Parameters:
url (str) – The URL of the image to download, or a base64 data URI.
- Returns:
The downloaded and decoded image as a PIL Image object.
- Return type:
PIL.Image.Image
- model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}∞
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ConfigDict = {'arbitrary_types_allowed': True, 'extra': 'allow', 'extras': 'forbid', 'from_attributes': True, 'use_enum_values': False, 'validate_assignment': False}∞
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[dict[str, FieldInfo]] = {'array': FieldInfo(annotation=Union[ndarray[Any, dtype[Any]], Annotated[Path, PathType], MultiArrayNumpyFile], required=True, metadata=[<class 'pydantic_numpy.helper.annotation.NpNDArrayPydanticAnnotation'>]), 'base64': FieldInfo(annotation=Union[Annotated[str, EncodedStr, InstanceOf], NoneType], required=False, default=None), 'encoding': FieldInfo(annotation=Literal['png', 'jpeg', 'jpg', 'bmp', 'gif'], required=True), 'path': FieldInfo(annotation=Union[Annotated[Path, PathType], NoneType], required=False, default=None), 'pil': FieldInfo(annotation=Union[Annotated[Image, InstanceOf], NoneType], required=False, default=None, description='The image represented as a PIL Image object.', exclude=True, repr=False), 'size': FieldInfo(annotation=tuple[int, int], required=True), 'url': FieldInfo(annotation=Union[Annotated[Url, InstanceOf], str, NoneType], required=False, default=None)}∞
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].
This replaces Model.__fields__ from Pydantic V1.
- static open(path: str, encoding: str = 'jpeg', size=None) Image [source]∞
Opens an image from a file path.
- Parameters:
path (str) – The path to the image file.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- path: Path | None∞
- pil: Image | None∞
- static pil_to_data(image: Image, encoding: str, size=None) dict [source]∞
Creates an Image instance from a PIL image.
- Parameters:
image (PIL.Image.Image) – The source PIL image from which to create the Image instance.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- save(path: str, encoding: str | None = None, quality: int = 10) None [source]∞
Save the image to the specified path.
If the image is a JPEG, the quality parameter can be used to set the quality of the saved image. The path attribute of the image is updated to the new file path.
- Parameters:
path (str) – The path to save the image to.
encoding (Optional[str]) – The encoding to use for saving the image.
quality (int) – The quality to use for saving the image.
- size: tuple[int, int]∞
- url: Url | str | None∞
Module contents∞
- class mbodied.types.sense.Image(arg: ndarray | Image | str | Url | Path = None, url: str | None = None, path: str | None = None, base64: str | None = None, array: ndarray | None = None, pil: Image | None = None, encoding: str | None = 'jpeg', size: Tuple | None = None, bytes_obj: bytes | None = None, **kwargs)[source]∞
Bases:
Sample
An image sample that can be represented in various formats.
The image can be represented as a NumPy array, a base64 encoded string, a file path, a PIL Image object, or a URL. The image can be resized to and from any size and converted to and from any supported format.
- array∞
The image represented as a NumPy array.
- Type:
Optional[np.ndarray]
- base64∞
The base64 encoded string of the image.
- Type:
Optional[Base64Str]
- path∞
The file path of the image.
- Type:
Optional[FilePath]
- pil∞
The image represented as a PIL Image object.
- Type:
Optional[PILImage]
- url∞
The URL of the image.
- Type:
Optional[AnyUrl]
- size∞
The size of the image as a (width, height) tuple.
- Type:
Optional[tuple[int, int]]
- encoding∞
The encoding of the image.
- Type:
Optional[Literal[“png”, “jpeg”, “jpg”, “bmp”, “gif”]]
Example
>>> image = Image("https://example.com/image.jpg") >>> image = Image("/path/to/image.jpg") >>> image = Image("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4Q3zaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA")
>>> jpeg_from_png = Image("path/to/image.png", encoding="jpeg") >>> resized_image = Image(image, size=(224, 224)) >>> pil_image = Image(image).pil >>> array = Image(image).array >>> base64 = Image(image).base64
- array: ndarray[Any, dtype[Any]] | Path | MultiArrayNumpyFile∞
- base64: str | None∞
- static bytes_to_data(bytes_data: bytes, encoding: str = 'jpeg', size=None) dict [source]∞
Creates an Image instance from a bytes object.
- Parameters:
bytes_data (bytes) – The bytes object to convert to an image.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- dump(*args, as_field: str | None = None, **kwargs) dict | Any [source]∞
Return a dict or a field of the image.
- encoding: Literal['png', 'jpeg', 'jpg', 'bmp', 'gif']∞
- static from_base64(base64_str: str, encoding: str, size=None) Image [source]∞
Decodes a base64 string to create an Image instance.
- Parameters:
base64_str (str) – The base64 string to decode.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- classmethod from_bytes(bytes_data: bytes, encoding: str = 'jpeg', size=None) Image [source]∞
Creates an Image instance from a bytes object.
- Parameters:
bytes_data (bytes) – The bytes object to convert to an image.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- static load_url(url: str, download=False) Image | None [source]∞
Downloads an image from a URL or decodes it from a base64 data URI.
- Parameters:
url (str) – The URL of the image to download, or a base64 data URI.
- Returns:
The downloaded and decoded image as a PIL Image object.
- Return type:
PIL.Image.Image
- model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}∞
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ConfigDict = {'arbitrary_types_allowed': True, 'extra': 'allow', 'extras': 'forbid', 'from_attributes': True, 'use_enum_values': False, 'validate_assignment': False}∞
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[dict[str, FieldInfo]] = {'array': FieldInfo(annotation=Union[ndarray[Any, dtype[Any]], Annotated[Path, PathType], MultiArrayNumpyFile], required=True, metadata=[<class 'pydantic_numpy.helper.annotation.NpNDArrayPydanticAnnotation'>]), 'base64': FieldInfo(annotation=Union[Annotated[str, EncodedStr, InstanceOf], NoneType], required=False, default=None), 'encoding': FieldInfo(annotation=Literal['png', 'jpeg', 'jpg', 'bmp', 'gif'], required=True), 'path': FieldInfo(annotation=Union[Annotated[Path, PathType], NoneType], required=False, default=None), 'pil': FieldInfo(annotation=Union[Annotated[Image, InstanceOf], NoneType], required=False, default=None, description='The image represented as a PIL Image object.', exclude=True, repr=False), 'size': FieldInfo(annotation=tuple[int, int], required=True), 'url': FieldInfo(annotation=Union[Annotated[Url, InstanceOf], str, NoneType], required=False, default=None)}∞
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].
This replaces Model.__fields__ from Pydantic V1.
- static open(path: str, encoding: str = 'jpeg', size=None) Image [source]∞
Opens an image from a file path.
- Parameters:
path (str) – The path to the image file.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- path: Path | None∞
- pil: Image | None∞
- static pil_to_data(image: Image, encoding: str, size=None) dict [source]∞
Creates an Image instance from a PIL image.
- Parameters:
image (PIL.Image.Image) – The source PIL image from which to create the Image instance.
encoding (str) – The format used for encoding the image when converting to base64.
size (Optional[Tuple[int, int]]) – The size of the image as a (width, height) tuple.
- Returns:
An instance of the Image class with populated fields.
- Return type:
- save(path: str, encoding: str | None = None, quality: int = 10) None [source]∞
Save the image to the specified path.
If the image is a JPEG, the quality parameter can be used to set the quality of the saved image. The path attribute of the image is updated to the new file path.
- Parameters:
path (str) – The path to save the image to.
encoding (Optional[str]) – The encoding to use for saving the image.
quality (int) – The quality to use for saving the image.
- size: tuple[int, int]∞
- url: Url | str | None∞