sensor – 摄像头

[English]

sensor 模块用于控制摄像头并采集图像帧。它的接口与 OpenMV 的 sensor 保持一致,因此大多数 OpenMV 脚本无需改动即可移植。

摄像头初始化与连续采集

典型流程是复位传感器、选择像素格式与分辨率、等待自动曝光和自动白平衡稳定,然后连续采集图像:

import sensor

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()

sensor.reset() 会应用开发板的默认摄像头配置。RGB565 适用于显示、绘图和大多数 AI 工作流,GRAYSCALE 则可降低许多检测算法的内存占用和处理开销。sensor.snapshot() 返回由可复用帧缓冲承载的 image.Image,因此需要在下一次采集后继续保持图像内容不变时,应先复制该图像。

图像方向与摄像头状态

摄像头传感器的实际安装方向可能导致画面翻转,可通过水平镜像和垂直翻转进行校正。status() 返回当前分辨率、像素格式、传感器 ID、图像方向和裁剪信息,可用于诊断:

import sensor

sensor.reset()
sensor.set_hmirror(True)
sensor.set_vflip(False)

info = sensor.status()
print("sensor id:", info["id"])
print("output:", info["width"], "x", info["height"])
print("ready:", info["ready"])

镜像和翻转设置会影响后续采集的图像。产品代码可在初始化后调用 status(),确认摄像头已经就绪,并验证协商得到的输出尺寸与处理链路一致。

暂时停止采集

不需要采集时可以停止摄像头流,并在下一次采集前重新启动:

sensor.shutdown(True)
# 执行不需要摄像头的任务。
sensor.shutdown(False)
sensor.skip_frames(n=3)
img = sensor.snapshot()

重新启动摄像头流后,如果曝光或输入帧队列需要重新稳定,应丢弃少量图像帧。

参见

摄像头流水线 说明了一帧图像如何从图像传感器到达 image.Image图像模型 介绍像素格式与 色彩空间。

可运行示例:example/01-Camera/00-Snapshot(采集并保存)与 example/01-Camera/03-MJPEG(Wi-Fi MJPEG 串流)。

Constants

sensor.GRAYSCALE

Pixel format constants accepted by set_pixformat().

sensor.RGB565
sensor.QQVGA

Frame size constants accepted by set_framesize().

sensor.QVGA

Functions

sensor.reset()

Reset and initialize the camera with the board default sensor configuration.

sensor.shutdown(enable=...)

Stop or restart the camera stream.

参数:

enable – True shuts the camera down; False starts it again.

sensor.set_pixformat(pixformat)

Select the output pixel format.

参数:

pixformat – sensor.GRAYSCALE or sensor.RGB565.

sensor.get_pixformat()

Return the current pixel format constant.

sensor.set_framesize(framesize)

Select the output frame size.

参数:

framesize – sensor.QQVGA or sensor.QVGA.

sensor.get_framesize()

Return the current frame size constant.

sensor.width()

Return the current output image width in pixels.

sensor.height()

Return the current output image height in pixels.

sensor.get_id()

Return the camera sensor ID.

sensor.set_hmirror(enable)

Mirror the camera image horizontally.

参数:

enable – True enables horizontal mirror.

sensor.get_hmirror()

Return whether horizontal mirror is enabled.

sensor.set_vflip(enable)

Flip the camera image vertically.

参数:

enable – True enables vertical flip.

sensor.get_vflip()

Return whether vertical flip is enabled.

sensor.skip_frames(time=..., n=...)

Drop frames while camera exposure and processing settle.

参数:

time – milliseconds to wait. n: number of frames to skip.

sensor.snapshot(buffer=...)

Capture one image from the camera.

参数:

buffer – reserved for compatibility; pass None in current ESP-VISION builds.

sensor.status()

Return camera readiness, size, format, mirror, flip, and crop status.

Classes

class sensor.SensorStatus

Dictionary returned by status().