rpicam-apps and libcamera
Overview
Raspberry Pi developers created rpicam-apps with several sample applications (rpicam-[hello|jpeg|still|vid|raw|detect]) using the software library libcamera, which provides a C++ API for configuring a camera and receiving and processing image frames. Extensive documentation and links are available at Raspberry Pi Camera software, including common command-line options. There are apt libcamera-and-rpicam-apps-packages available also.
Hardware Installation Instructions
Follow the camera installation instructions here using the correct ribbon cable orientation (camera cable contacts face away from ethernet socket on Pi). Also, note that you need to boot or reboot the Pi after plugging in the camera.
Trying out the Baseline Software
The rpicam apps are included in recent Raspberry Pi OS versions. Try out the camera as per Getting Started/Using the camera for the first time and do troubleshooting if needed. Some of the apps will display images in a preview window. To see this preview window, use one of these two options:
- Easy and fast: Connect a monitor into the Pi’s HDMI output, or
- Harder and slow: Support X forwarding with the –qt-preview option. The software needs to be built differently (details below).
Software Installation Instructions
For speed and simplicity, we want to edit and rebuild only rpicam-apps, not libcamera.
- Follow the instructions at building-rpicam-apps-without-building-libcamera to get necessary packages with apt.
- Skip the section Building libcamera
- Follow the instructions at Building rpicam-apps
- Install additional necessary packages with apt.
- Clone the code for rpicam-apps.
- Change into rpicam-apps directory.
- After reading the Tip (“Use -Dneon…”), configure the build with meson setup build … adding arguments as needed. We don’t plan to use OpenCV or TensorFlow Lite, but you may enable them if you like.
- Build rpicam-apps with meson compile -C build -j 1 . The -j 1 argument limits ninja to using one job for the build. Without it, my builds freeze up partway through, forcing me to power-cycle the RPi3 or RPi4.
- Install rpicam-apps binary with sudo meson install -C build .
- Open new terminal window and then follow other Getting Started instructions as needed.
Software Development Guidance
Big Picture
You can specify post-processing activities for a rpicam-app using the command-line options --post-process-file path/assets/my_ppa.json
. The file my_ppa.json lists the stage(s) to run (with optional parameters) in order. The stage names are defined by code within the stage’s class, not by any file names.
C++ source code for post-processing stages is located in rpicam-apps/post_processing_stages. The source files are compiled and linked into a shared library object (rpicam-apps/build/libpost_processing_stages.so), which is used when building the specific rpicam-app (e.g. rpicam-hello).
Within your source code for the post-processing stage, start by deriving your class from the PostProcessingStage class, which is defined in post_processing_stages/post_processing_stage.[cpp|hpp]
. Define the required member functions. The functions Name()
and RegisterStage()
are used let the system know what your stage is called.
TBD: how to pass parameters from .json to stage.
Here is some of the original documentation:
- Understanding and Writing your own Apps
- Post-Processing Stages
- Writing your own Post-Processing Stages
Details
- After editing code for a post-processing stage, you just need to rebuild the rpicam-app which you are using (not all of the apps). For example:
~/rpicam-apps/build $ make rpicam-hello
- To add a new post-processing stage, you will need to
- Add the source code file(s) to rpicam-apps/post_processing_stages
- Let cmake know about that source code by adding the filename to rpicam-apps/post_processing_stages/CMakeLists.txt:
set(SRC post_processing_stage.cpp negate_stage.cpp my_new_stage.cpp ... )
- Rerun cmake to create new make files: TBD: add command with correct syntax
- Create a .json file in ~/rpicam-apps/assets/ to configure the post-processing pipeline
- To use that stage, use the command-line option
--post-process-file path/assets/my_ppa.json