io

I/O functions for SD card and external files.

class mio.io.BufferedCSVWriter(file_path: str | Path, buffer_size: int = 100)

Write data to a CSV file in buffered mode.

Parameters:
  • file_path (Union[str, Path]) – The file path for the CSV file.

  • buffer_size (int, optional) – The number of rows to buffer before writing to the file (default is 100).

file_path

The file path for the CSV file.

Type:

Path

buffer_size

The number of rows to buffer before writing to the file.

Type:

int

buffer

The buffer for storing rows before writing.

Type:

list

append(data: List[Any]) None

Append data (as a list) to the buffer.

Parameters:

data (List[Any]) – The data to be appended.

close() None

Close the CSV file and flush any remaining data.

flush_buffer() None

Write all buffered rows to the CSV file.

class mio.io.SDCard(drive: str | Path, layout: SDLayout | Path | PathLike[str] | Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])] = 'wirefree-sd-layout')

I/O for data on an SDCard

an instance of sdcard.SDLayout (typically in formats ) configures how the data is laid out on the SD card. This class makes the i/o operations abstract over multiple layouts

Parameters:
check_valid() bool

Checks that the header sector has the appropriate write keys in it

Returns:

bool - True if valid, False if not

property config: SDConfig

Read configuration from SD Card

property frame: int | None

When reading, the number of the frame that would be read if we were to call read()

property frame_count: int

Total number of frames in recording.

Inferred from n_buffers_recorded and reading a single frame to get the number of buffers per frame.

property position: int | None

When entered as context manager, the current position of the internal file descriptor

positions

A mapping between frame number and byte position in the video that makes for faster seeking :)

As we read, we store the locations of each frame before reading it. Later, we can assign to frame to seek back to those positions. Assigning to frame works without caching position, but has to manually iterate through each frame.

read(return_header: Literal[True] = True) Frame
read(return_header: Literal[False] = False) ndarray

Read a single frame

Parameters:

return_header (bool) – If True, return headers from individual buffers (default False)

Returns:

numpy.ndarray , or a tuple(ndarray, List[SDBufferHeader]) if return_header is True

skip() None

Skip a frame

Read the buffer headers to determine buffer sizes and just seek ahead

to_img(path: Path | str | None, frame: int | None = None, force: bool = False, chunk_size: int = 1000000.0, progress: bool = True) None

Create a new disk image that is truncated to the actual size of the video data

Typically, making disk images using dd or other tools will create an image file that is the full size of the media it’s stored on. Rather than sending a bunch of 30GB image files around, we can instead create an image that is truncated to just the size of the data that has actually been recorded

Parameters:
  • path (pathlib.Path) – Path to write .img file to

  • frame (int) – Optional, if present only write the first n frames. If None , write all frames

  • force (bool) – If True, overwrite an existing file. If False , (default) don’t.

  • chunk_size (int) – Number of bytes to read/write at once (default 1e6 )

  • progress (bool) – If True (default), show progress bar

to_video(path: Path | str, fourcc: Literal['GREY', 'mp4v', 'XVID'] = 'GREY', isColor: bool = False, force: bool = False, progress: bool = True) None

Save contents of SD card to video with opencv

Parameters:
  • path (pathlib.Path) – Output video path, with video extension .avi or .mp4

  • fourcc (str) –

    FourCC code used with opencv. Other codecs may be available depending on your opencv installation, but by default opencv supports one of:

    • GREY (default)

    • mp4v

    • XVID

  • isColor (bool) – Indicates whether output video is in color (default: False)

  • force (bool) – If True, overwrite output video if one already exists (default: False)

  • progress (bool) – If True (default) show progress bar.