image.ImageIO – 图像流

[English]

image.ImageIO 类型用于录制和回放图像序列,并保留帧间时间间隔。文件流在存储上 读写容器文件;内存流则把帧保存在预分配的缓冲区中。它以 image.ImageIO 的形式 暴露在 image 模块上;类型存根位于 stubs/imageio.pyi

录制到存储

import sensor, image

stream = image.ImageIO("/sdcard/stream.bin", "w")
for _ in range(30):
    stream.write(sensor.snapshot())
stream.sync()
print("frames:", stream.count(), "bytes:", stream.size())
stream.close()

每次 write() 都会保存图像以及与上一帧之间的时间间隔。sync() 可在不关闭流的情况下将文件缓冲数据写入存储。文件流使用完毕后必须关闭,以完成容器元数据和存储缓冲区的写入。

回放已录制的图像流

import image

stream = image.ImageIO("/sdcard/stream.bin", "r")
try:
    while True:
        img = stream.read(loop=False, pause=True)
        if img is None:
            break
        img.flush()
finally:
    stream.close()

pause=True 会按照录制时的帧间隔进行回放,loop=False 则会在流结束时返回 None,而不是回到第一帧。需要以处理能力允许的最快速度读取时,可设置 pause=False

使用内存图像流

stream = image.ImageIO((320, 240, image.RGB565), 10)
for _ in range(10):
    stream.write(sensor.snapshot())

stream.seek(0)
first = stream.read(pause=False)
print("buffer bytes:", stream.buffer_size())
stream.close()

内存流不产生存储 I/O,适合保存短时间图像历史或实现帧差处理,但创建流时会在 RAM 或 PSRAM 中一次性分配全部容量。

参见

编解码与推流 一并介绍了 ImageIO、JPEG、H.264 以及 USB CDC 预览通路。

可运行示例:example/02-Image-Processing/03-Frame-Differencing/in_memory_frame_differencing.py 使用内存 ImageIO 流。

Constants

imageio.FILE_STREAM

Stream backed by a file on storage (e.g. /sdcard/stream.bin).

imageio.MEMORY_STREAM

Stream backed by a fixed-size buffer in PSRAM/RAM.

Classes

class imageio.ImageIO(stream, mode)
class imageio.ImageIO(stream, count)

Record and replay sequences of images, preserving inter-frame timing. Exposed as image.ImageIO. A file stream reads/writes the OpenMV “OMV IMG STR” container on storage; a memory stream keeps frames in a pre-allocated buffer for fast capture/playback.

Open a file stream for reading (“r”) or writing (“w”).

参数:
  • stream – path to the stream file.

  • mode – “r” to read, “w” to create/overwrite.

type()

Return the stream type: FILE_STREAM or MEMORY_STREAM.

is_closed()

Return True if the stream has been closed.

count()

Return the number of frames recorded in the stream.

offset()

Return the current frame index (read/write cursor).

version()

Return the container version for file streams, or None for memory streams.

buffer_size()

Return the per-frame buffer size for memory streams, or None for file streams.

size()

Return the total stream size in bytes.

write(image)

Append one image to the stream (records the elapsed time since the last write).

参数:

image – frame to store.

read(copy_to_fb=..., *, loop=..., pause=...)

Read the next frame from the stream.

参数:
  • copy_to_fb – True loads the frame into the frame buffer (and updates the preview).

  • loop – True rewinds to the first frame at end-of-stream instead of returning None.

  • pause – True sleeps to honor the frame’s recorded timestamp (real-time playback).

seek(offset)

Move the read/write cursor to the given frame index.

参数:

offset – target frame index.

sync()

Flush buffered file data to storage (no-op for memory streams).

close()

Close the stream and release its resources.