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
Resolution | Sensor |
---|---|
5MP | OV5647 |
8MP | IMX219 |
12MP | IMX477 |
12MP | 477M |
12MP | IMX708 |
12MP | IMX378 |
16MP | IMX519 |
64MP | 64MP Hawkeye |
64MP | 64MP OV64A40 |
0.3MP | OV7251 |
1.58MP | IMX296 |
1MP | OV9281 |
2MP | OV2311 |
2MP | IMX290/IMX462/IMX327 |
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