Picamera2 User Guide
Introduction
Picamera2
is the libcamera-based replacement for Picamera which was a Python interface to the Raspberry Pi's legacy camera stack. Picamera2
also presents an easy to use Python API.
Reference
Raspberry Pi Official Documentation about Picamera2 is available on Gtihub and in the Picamera2 Manual
At Arducam, we have added autofocus control to the original.
Picamera2
is only supported on Raspberry Pi OS Bullseye (or later) images, both 32 and 64-bit. As of September 2022, Picamera2
is pre-installed on images downloaded from Raspberry Pi. It works on all Raspberry Pi boards right down to the Pi Zero, although performance in some areas may be worse on less powerful devices.
Note
Picamera2
is not supported on:
-
Images based on Buster or earlier releases.
-
Raspberry Pi OS Legacy images.
-
Bullseye (or later) images where the legacy camera stack has been re-enabled.
Getting Started
For the Pivariety cameras, you need to install the following steps:
Our Picamera2 is basically the same as the official one. However, because we added extra autofocus control, it resulted in a different installation.
You can also refer to the Raspberry Pi office documentation and Github demo, but the 2.1 and 2.2 sections in there need to be replaced with the following steps:
Pivariety cameras camera Board list:
Resolution | Camera Module |
---|---|
2MP | IMX462 |
2MP | OG02B10 |
2MP | OV2311 |
2.3MP | AR0234 |
16MP | IMX298 |
18MP | AR1820 |
21MP | IMX230 |
PiCamera2 Focus Controller Instruction
-
Step 1. Install libcamera
Ensure that libcamera version 0.0.10 is installed:
dpkg -l | grep libcamera
If your version is lower than 0.0.10 please install the latest:
wget -O install_pivariety_pkgs.sh https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
chmod +x install_pivariety_pkgs.sh
./install_pivariety_pkgs.sh -p libcamera_dev
./install_pivariety_pkgs.sh -p libcamera_apps
-
Step 2. Installing
Picamera2
dependencies
sudo apt install -y python3-kms++
sudo apt install -y python3-pyqt5 python3-prctl libatlas-base-dev ffmpeg python3-pip
sudo pip3 install numpy --upgrade
sudo apt install picamera2 --upgrade
-
Step 3. Basic Usage Reference
Tip
In picamera2, the autofocus trigger is controlled by picam2.set_controls, {"AfMode": 0 ,"LensPosition": focus value} for manual focus and {"AfMode": 1 ,"AfTrigger": 0} for single autofocus and {"AfMode": 2 ,"AfTrigger": 0} for continuous autofocus.
#!/usr/bin/python3
import time
from picamera2 import Picamera2, Preview
picam2 = Picamera2()
picam2.start_preview(Preview.QTGL)
preview_config = picam2.create_preview_configuration()
picam2.configure(preview_config)
picam2.start()
time.sleep(1)
picam2.set_controls({"AfMode": 0, "LensPosition": 425})
# If your libcamera-dev version is 0.0.10, use the following code.
# AfMode Set the AF mode (manual, auto, continuous)
# For example, single focus: picam2.set_controls({"AfMode": 1 ,"AfTrigger": 0})
# continuous focus: picam2.set_controls({"AfMode": 2 ,"AfTrigger": 0})
time.sleep(5)
picam2.set_controls({"AfMode": 1 ,"AfTrigger": 0})
Quote
For detail, please refer to: https://github.com/ArduCAM/picamera2_examples/blob/main/AutofocusControl.py
Every time you set the focus, you need to set the focus mode
The range of LensPosition changed from 0~15:
Quote
For detail, please refer to: https://github.com/ArduCAM/picamera2_examples/blob/main/ManualfocusControl.py