Skip to content

msight_core.data.image

msight_core.data.image

Utilities for encoding/decoding images carried as Data messages.

This module provides :class:ImageData, a :class:SensorData subclass that wraps an encoded (e.g. JPEG) or raw numpy image together with the common sensor metadata (timestamps, frame id, device name). It includes helper constructors and conversion utilities to move between numpy.ndarray and wire-friendly byte representations.

Key functions / behavior: - from_ndarray: Create an ImageData from a numpy array, optionally encoding to JPEG with configurable quality. - to_array: Decode the stored bytes back into a numpy.ndarray. - from_json: Support JSON deserialization where the image bytes are base64-encoded.

ImageData dataclass

Bases: SensorData

Container for image sensor payloads.

The dataclass stores an image payload together with timestamps and other metadata inherited from :class:SensorData.

Attributes:

Name Type Description
image Optional[bytes]

Encoded image bytes (e.g. JPEG) or raw bytes depending on is_encoded. Hidden from repr to avoid large binary dumps in logs.

is_encoded bool

Whether image contains an encoded image format (True) or raw numpy bytes (False). When encoded, to_array will decode using OpenCV; when not encoded the consumer must know the original dtype/shape, which is provided in size.

detection_results Optional[DetectionResultsData]

Optional detection output associated with this image/frame.

size Optional[tuple[int, int, int]]

Shape of the original numpy array (height, width, channels). Required when is_encoded is False to reconstruct the array from raw bytes.

decoded_image property

Get the decoded image as a numpy array.

Returns:

Type Description
ndarray

np.ndarray: The decoded image.

Raises:

Type Description
ValueError

If the image cannot be decoded from encoded bytes.

from_ndarray(image, sensor_name, capture_timestamp=None, creation_timestamp=None, is_encoded=True, detection_results=None, jpeg_quality=50) classmethod

Create an ImageData instance from a numpy array.

Parameters:

Name Type Description Default
image ndarray

The image data as a numpy array.

required
sensor_name str

The name of the sensor.

required
capture_timestamp float

The capture timestamp. Defaults to None.

None
creation_timestamp float

The creation timestamp. Defaults to None.

None
is_encoded bool

Flag indicating if the image is encoded. Defaults to True. When flag is True, the function will encode the image to JPEG format.

True
detection_results Optional[DetectionResultsData]

Detection results data. Defaults to None.

None
jpeg_quality int

JPEG quality for encoding. Defaults to 50.

50

Returns:

Name Type Description
ImageData ImageData

An instance of ImageData containing the image and metadata.

to_ndarray()

Decode JPEG bytes back into a numpy array.

from_json(data) classmethod

Deserialize a JSON string into a concrete Data subclass.