Skip to content

Picamera2

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.

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.

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 Native cameras, you can find documentation and demo which should help you to get started.

Native Camera Board list:

Resolution Camera Module
5MP OV5647
8MP imx219
12MP imx477
16MP imx298
0.3MP OV7251
2MP OV2311
1.58MP imx296

For the imx519/hawkeye 64mp,

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 office documentation and demo, but the 2.1 and 2.2 sections in there need to be replaced with the following steps:

imx519/hawkeye 64mp camera Board list:

Resolution Camera Module
16MP imx519
64MP hawkeye-64mp

PiCamera2 Focus Controller Instruction

  • Step 1. Ensure that libcamera version 0.0.10 is installed
dpkg -l | grep libcamera

libcamera-verison

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

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)

Modification

Update Date:

February 20th, 2023

picam2.set_controls({"AfMode": 1 ,"AfTrigger": 0})

picamera1

Every time you set the focus, you need to set the focus mode

picamera2

The range of LensPosition changed from 0~15:

picamera3