DynAIkonTrap.imdecode#

Static functions providing access to decoding byte arrays into image formats, returned data are numpy ndarrays.

Classes

decoder()

A class containing static methods to decode image formats YUV and JPEG, depends on numpy and opencv (cv2) python packages.

class decoder#

A class containing static methods to decode image formats YUV and JPEG, depends on numpy and opencv (cv2) python packages. YUV formats are assumed to be YUV420, with 1.5 bytes per pixel, as described here: https://en.wikipedia.org/wiki/YUV#Y.E2.80.B2UV420p_.28and_Y.E2.80.B2V12_or_YV12.29_to_RGB888_conversion

Included methods convert a given byte array into numpy ndarrays of pixel values.

static bgr_array_to_yuv_buf(bgr: ndarray) bytes#

Used to convert bgr colour matrices into YUV420 format buffers compatible with the remainder of the LOW_POWERED pipeline

Parameters:

bgr (np.ndarray) – Input BGR matrix

Returns:

Output YUV420 format buffer

Return type:

bytes

static h264_to_jpeg_frames(h264_file: str) List[str]#

Create a list of jpeg files stored on disk for every frame in the h264 recording file. If no valid file is given, returns empty list

Parameters:

h264_file (str) – file path to a h264 encoded recording

Returns:

list of file paths to saved jpeg images, one per h264 encoded frame. All files saved within the /tmp/ directory for further moving around. If the h264 path given, an empty list is returned

Return type:

List[str]

static h264_to_mp4(h264_file: str, video_framerate: int, suffix: str = '.mp4') str#

This wraps H264 files in the mp4 container format, this is performed calling FFMPEG. Video framerate is also required.

MP4 files may also be passed in place of the h264 input.

Parameters:
  • h264_file (str) – The path to the h264 (or mp4) file to be converted

  • video_framerate (int) – The video framerate

  • suffix (str, optional) – An optional video suffix to produce other file formats such as .avi. Defaults to “.mp4”.

Returns:

Path to the generated mp4 file on disk. This file may be empty if the encoding process fails.

Return type:

str

static jpg_buf_to_bgr_array(buf: bytes) ndarray#

Wraps around the OpenCV imdecode method, to decode colour jpeg images produces a numpy ndarray in BGR format of uncompressed data

Parameters:

buf (bytes) – a bytes buffer containing image data compressed in jpeg format, data is assumed to be a 3-channel, colour image

Returns:

an ndarray with size (width, height, 3) where each element is a pixel, pixel format is BGR, one byte per colour

Return type:

np.ndarray

static yuv_buf_to_bgr_array(buf: bytes, dims: Tuple[int, int]) ndarray#

converts a given byte buffer in YUV420 format into an ndarray of pixel values in BGR format. Code inspired from Picamera example, availble: https://picamera.readthedocs.io/en/release-1.13/recipes2.html#unencoded-image-capture-yuv-format

Parameters:

buf (bytes) – a bytes object containing the raw pixel data in YUV format. Dimensions of the buffer are assumed to be square, the width and height are calculated from this from using the buffer length.

Returns:

an ndarray with size (width, height, 3) where each element is a pixel, pixel format is BGR, one byte per colour

Return type:

np.ndarray

static yuv_to_png_temp_file(buf: bytes, dims) str#

converts a given buffer in YUV420 format into a temporary png file stored on disk

Parameters:

buf (bytes) – a bytes object containing the raw pixel data in YUV format. Dimensions of the buffer are assumed to be square, the width and height are calculated from this from using the buffer length.

Returns:

path to png file on disk, returns empty string if no file could be created

Return type:

str