Cameras for Raspberry Pi

  1. Home
  2. Docs
  3. Cameras for Raspberry Pi
  4. New: Pivariety Camera
  5. How to access and program the Pivariety Camera using Command Line, Python, OpenCV, and GStreamer

How to access and program the Pivariety Camera using Command Line, Python, OpenCV, and GStreamer

Table Of Contents

1. Libcamera and Libcamera App Installation

1.1. Use pre-compiled binaries

Note: If you don’t want to compile, you can choose to use our pre-compiled deb package.

1.1.1. Install libcamera

sudo apt install ./libcamera-dev.deb

1.1.2. Install libcamera-apps

sudo apt install ./libcamera-apps.deb

If you use the pre-compiled package please go to the chapter of Select the correct graphics driver.

1.2. Build from the source code

1.2.1. Install dependency

If you need to use the plugin GStreamer, please install the GStreamer dependency before compiling libcamera.

sudo apt update
sudo apt install -y libboost-dev libgnutls28-dev openssl \
	libtiff5-dev qtbase5-dev libqt5core5a libqt5gui5 \
	libqt5widgets5 meson libegl1-mesa-dev cmake \
	libboost-program-options-dev libdrm-dev \
	libexif-dev libegl1-mesa-dev
sudo pip3 install pyyaml ply
sudo pip3 install --upgrade meson

Optional dependencies (for GStreamer plugin).

sudo apt update
sudo apt install -y libgstreamer1.0-dev \

1.2.2. Download and compile libcamera

Please note that Pivariety can’t be supported by Raspistill, you need to download libcamera to make it work. You can follow the installation steps listed in the code library.

cd ~
git clone git://
cd libcamera
meson build
cd build
meson configure -Dpipelines=raspberrypi -Dtest=false
cd ..
ninja -C build

On some lower memory platforms (e.g. 1GB) there have been cases of ninja exhausting all the system memory and aborting (as it will attempt to use all the CPU cores). If this happens, please try replacing ninja -C build by ninja -C build -j 2 this will restrict Ninja to only 2 cores.

After downloading and compiling libcamera, you need to replace the code with.

cd ~
tar xvf ipa_rpi.tar.xz
cp libcamera/build/src/ipa/raspberrypi/
cd libcamera && sudo ninja -C build install

1.2.3. Download and compile libepoxy

libcamera-apps requires libepoxy to be installed.

git clone
cd libepoxy
mkdir _build
cd _build
sudo ninja install

1.2.4. Download and compile libcamera-apps

git clone
cd libcamera-apps
mkdir build
cd build
cmake ..
make -j4
sudo make install

To check everything is working correctly, type ./libcamera-hello – you should see a preview window displayed for about 5 seconds.

Note for Pi 3 devices

As we saw previously, 1GB devices may need make -j2 instead of make -j4.Also, Pi 3s do not by default use the correct GL driver, so please ensure you have dtoverlay=vc4-fkms-v3d in the [all] (not in the [pi4]) section of your /boot/config.txt file.

1.2.5. Select the correct graphics driver

There are 3 different graphics drivers available on the Raspberry Pi: firmware, FKMS, and KMS.

The firmware graphics driver cannot be used with libcamera-apps. The Raspberry Pi 4 and 400 use the newer FKMS graphics driver by default: this is compatible with libcamera-apps. 

For all other models of Raspberry Pi, you must select the FKMS driver by adding the following line to the /boot/config.txt file(Note: Please add it under the [all] tag ).


NOTE: If you want to modify the file of /boot/config.txt, you need to reboot your device after modifying.

Tips: How to add the /boot/config.txt under the [all] tag? 

a. open the file
sudo nano /boot/config.txt #Use the mouse to draw to the bottom #Add dtoverlay=vc4-fkms-v3d under the table of [all]
b. Press the ctrl+o after editing, and press enter to save your file
c. Press the Ctrl+x to exit the editor

You can refer to here to learn more.

2. Use libcamera to access the camera

2.1. Use libcamera-app to access the camera

2.1.1. Capture image

For example, preview for 5s and save the image named test.jpg.

libcamera-still -t 5000 -o test.jpg

Parameter explanation
-t 5000 means preview for 5 seconds
-o test.jpg means to take a picture after the preview is over and save it as test.jpg

2.1.2. Record video

For example, record a H.264 10s video with the frame size 1920W × 1080H.

libcamera-vid -t 10000 --width 1920 --height 1080 -o test.h264

Parameter explanation
-t 10000 means recording for 10 seconds.
–width 1920 means to set the width to 1920.
–height 1080 means to set the height to 1080.
-o means to output the encoded data stream to the test.h264 file.

Note: H.264 format only supports 1920×1080 and below resolution.

2.2. Create a virtual video node (Access by Python)

2.2.1. Install V4l2loopback

Download kernel source code
cd ~
sudo apt install git bc bison flex libssl-dev
sudo wget -O /usr/local/bin/rpi-source && sudo chmod +x /usr/local/bin/rpi-source && /usr/local/bin/rpi-source -q --tag-update
rpi-source -d $(uname -r)
Compile driver
cd ~
git clone
cd v4l2loopback
make clean && make
make && sudo make install
sudo depmod -a

2.2.2. Use v4l2loopback

sudo modprobe v4l2loopback video_nr=3
cd ~/libcamera
export GST_PLUGIN_PATH=$(pwd)/build/src/gstreamer
gst-launch-1.0 libcamerasrc ! 'video/x-raw,width=1920,height=1080' ! videoconvert ! tee ! v4l2sink device=/dev/video3

Now you can use the video 3 node to access the camera. (vlc/OpenCV etc. )

Python OpenCV:
import cv2
cap = cv2.VideoCapture(3)
while True:
	ret, frame =
	cv2.imshow("Arducam", frame)
	key = cv2.waitKey(1)
	if key == ord('q'):

2.3. Use GStreamer to access the camera

2.3.1 Plugin GStreamer installation

Install GStreamer
sudo apt update
sudo apt install -y gstreamer1.0-tools
cd ~/libcamera
export GST_PLUGIN_PATH=$(pwd)/build/src/gstreamer
gst-launch-1.0 libcamerasrc ! 'video/x-raw,width=1920,height=1080' ! videoconvert ! autovideosink

2.3.2. Streaming MJPEG

# server 
libcamera-vid -t 0 --width 1920 --height 1080 --codec mjpeg -n -l -o tcp://
# client
gst-launch-1.0 -v tcpclientsrc port=5001 ! decodebin ! fpsdisplaysink sync=false text-overlay=false

2.3.3. Streaming H.264

# server 
libcamera-vid -t 0 --width 1920 --height 1080 --codec h264 -n -l -o tcp://
# client
gst-launch-1.0 -v tcpclientsrc port=5001 ! decodebin ! fpsdisplaysink sync=false text-overlay=false

3. Control the Focusing Manually

Some Pivariety Camera is a program controllable motorized focus camera, its focal length can be adjusted via v4l2-ctl or script.

6 1

3.1. Control via v4l2-ctl

v4l2-ctl -c focus_absolute=300

3.1. Control via script

scrigit clone
cd Arducam-Pivariety-V4L2-Driver/focus

Now you can use the up and down on the keyboard to control the focus position:

7 1

Was this article helpful to you? Yes No