sensor – Camera
The sensor module controls the camera and captures frames. It mirrors the OpenMV sensor API so existing OpenMV scripts port with little change.
Camera Initialization and Continuous Capture
A typical program resets the sensor, selects a pixel format and frame size, lets automatic exposure and white balance settle, and then captures frames continuously:
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() applies the board’s default camera configuration. RGB565 is suitable for display, drawing, and most AI workflows, while GRAYSCALE reduces memory and processing cost for many detection algorithms. sensor.snapshot() returns an image.Image backed by the reusable frame buffer, so copy the image when it must remain unchanged after the next capture.
Image Orientation and Camera Status
Use horizontal mirror and vertical flip to correct the image orientation imposed by the physical sensor installation. status() provides the active dimensions, pixel format, sensor ID, orientation, and crop information for diagnostics:
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"])
Mirror and flip settings affect subsequently captured frames. Product code can call status() after initialization to verify that the camera is ready and that the negotiated output size matches the processing pipeline.
Temporarily Stop Capture
The camera stream can be stopped when capture is not required and restarted before the next frame:
sensor.shutdown(True)
# Perform work that does not require the camera.
sensor.shutdown(False)
sensor.skip_frames(n=3)
img = sensor.snapshot()
After restarting the stream, discard a few frames if exposure or the incoming frame queue needs to stabilize.
See also
The Camera Pipeline explains how a frame travels from the image sensor to an image.Image, and The Image Model covers pixel formats and color spaces.
Runnable examples: example/01-Camera/00-Snapshot (capture and save) and example/01-Camera/03-MJPEG (Wi-Fi MJPEG stream).
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.
- Parameters:
enable – True shuts the camera down; False starts it again.
- sensor.set_pixformat(pixformat)
Select the output pixel format.
- Parameters:
pixformat – sensor.GRAYSCALE or sensor.RGB565.
- sensor.get_pixformat()
Return the current pixel format constant.
- sensor.set_framesize(framesize)
Select the output frame size.
- Parameters:
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.
- Parameters:
enable – True enables horizontal mirror.
- sensor.get_hmirror()
Return whether horizontal mirror is enabled.
- sensor.set_vflip(enable)
Flip the camera image vertically.
- Parameters:
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.
- Parameters:
time – milliseconds to wait. n: number of frames to skip.
- sensor.snapshot(buffer=...)
Capture one image from the camera.
- Parameters:
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().