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¶
The file path for the CSV file.
- Type:
Path
- 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 informats) configures how the data is laid out on the SD card. This class makes the i/o operations abstract over multiple layouts- Parameters:
drive (str,
pathlib.Path) – Path to the SD card drivelayout (
sdcard.SDLayout) – A layout configuration for an SD card
- check_valid() bool¶
Checks that the header sector has the appropriate write keys in it
- Returns:
bool - True if valid, False if not
- 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_recordedand 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
- 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 toframe (int) – Optional, if present only write the first n frames. If
None, write all framesforce (bool) – If
True, overwrite an existing file. IfFalse, (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.avior.mp4fourcc (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)mp4vXVID
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.