Camera API for micropython as user module, starting with the esp32 port.
MIT License
This project aims to support cameras in different ports in micropython, starting with the ESP32-Port and omnivision (OV2640 & OV5640) cameras. The project implements a general API for cameras in micropython (such as circuitpython have done). At the moment, this is a micropython user module, but it might get in the micropython repo in the future. The API is stable, but it might change without previous anounce.
If you are not familiar with building a custom firmware, you can go to the releases page and download one of the generic FWs that suits your board.
from camera import Camera, GrabMode, PixelFormat, FrameSize, GainCeiling
# Camera construction and initialization
# These pins are just examples and if you use them just like that will get a watchdog error. Adapt them to your board!
camera = Camera(
data_pins=[1,2,3,4,5,6,7,8],
vsync_pin=9,
href_pin=10,
sda_pin=11,
scl_pin=12,
pclk_pin=13,
xclk_pin=14,
xclk_freq=20000000,
powerdown_pin=-1,
reset_pin=-1,
pixel_format=PixelFormat.RGB565,
frame_size=FrameSize.QVGA,
jpeg_quality=15,
fb_count=1,
grab_mode=GrabMode.WHEN_EMPTY
)
#Camera construction using defaults (if you specified them in mpconfigboard.h)
camera = Camera()
# Capture image
img = camera.capture()
# Camera reconfiguration
camera.reconfigure(pixel_format=PixelFormat.JPEG,frame_size=FrameSize.QVGA,grab_mode=GrabMode.LATEST, fb_count=2)
camera.set_quality(10)
You can get and set sensor properties by the respective methods (e.g. camera.get_brightness() or camera.set_vflip(True). See autocompletitions in Thonny in order to see the list of methods. If you want more insides in the methods and what they actually do, you can find a very good documentation here. Notice that for the methods in here you need to prefix a get/set, depending on what you want to do.
To build the project, follow the following instructions:
espressif/esp32-camera:
git: https://github.com/espressif/esp32-camera
Alternatively, you can clone the https://github.com/espressif/esp32-camera repository inside the esp-idf/components folder instead of altering the idf_component.yml file.
To make things easier, add the following lines to your board config-file "mpconfigboard.h" with the respective pins and camera parameters. Otherwise, you will need to pass all parameters during construction. Don't forget the empty line at the bottom. Example for Xiao sense:
#define MICROPY_CAMERA_PIN_D0 (15)
#define MICROPY_CAMERA_PIN_D1 (17)
#define MICROPY_CAMERA_PIN_D2 (18)
#define MICROPY_CAMERA_PIN_D3 (16)
#define MICROPY_CAMERA_PIN_D4 (14)
#define MICROPY_CAMERA_PIN_D5 (12)
#define MICROPY_CAMERA_PIN_D6 (11)
#define MICROPY_CAMERA_PIN_D7 (48)
#define MICROPY_CAMERA_PIN_PCLK (13)
#define MICROPY_CAMERA_PIN_VSYNC (38)
#define MICROPY_CAMERA_PIN_HREF (47)
#define MICROPY_CAMERA_PIN_XCLK (10)
#define MICROPY_CAMERA_PIN_PWDN (-1)
#define MICROPY_CAMERA_PIN_RESET (-1)
#define MICROPY_CAMERA_PIN_SIOD (40) // SDA
#define MICROPY_CAMERA_PIN_SIOC (39) // SCL
#define MICROPY_CAMERA_XCLK_FREQ (20000000) // Frequencies are normally either 10 MHz or 20 MHz
#define MICROPY_CAMERA_FB_COUNT (2) // Usually the value is between 1 (slow) and 2 (fast, but more load on CPU)
#define MICROPY_CAMERA_JPEG_QUALITY (10) // Quality of JPEG output. 0-63 lower means higher quality. Definition will change in the future
#define MICROPY_CAMERA_GRAB_MODE (1) // 0=WHEN_EMPTY (might have old data, but less resources), 1=LATEST (best, but more resources)
To build the project, you could do it the following way:
. <path2esp-idf>/esp-idf/export.sh
cd MyESPCam/micropython/ports/esp32
make USER_C_MODULES=../../../../micropython-camera-API/src/micropython.cmake BOARD=<Your-Board> clean
make USER_C_MODULES=../../../../micropython-camera-API/src/micropython.cmake BOARD=<Your-Board> submodules
make USER_C_MODULES=../../../../micropython-camera-API/src/micropython.cmake BOARD=<Your-Board> all
If you experience problems, visit MicroPython external C modules.