How to use code to access libcamera(C++/Python)

We have wrapped the simple libcamera python library.

It allows you to access libcamera through python code, and realize tasks such as turning on the camera, controlling the camera, and turning off the camera.

In addition, we have also written a simple libcamera C++ example.

Through this example, you can quickly run the camera, and you can view the example source code to understand the specific process of libcamera operation.

Python

Download libcamera python library URL

Note

Our wrapped libcamera python library is about to retire. Full support for Picamera2.

We are about to release the libcamera package that supports Picamera2.

Picamera2 requires Raspberry Pi system version: bullseye, kernel version: >=5.10.92. We recommend users to use the latest Raspberry Pi system.

C++

libcamera_cpp_demo-master.zip (1.7 MB)

When running python capture_demo.py I get the following error:

[email protected]:~/libcamera-python $ python capture_demo.py
[1:35:27.317544971] [7481]  WARN IPAManager ipa_manager.cpp:149 No IPA found in '/usr/local/lib/arm-linux-gnueabihf/libcamera'
[1:35:27.317847593] [7481]  INFO Camera camera_manager.cpp:293 libcamera v0.0.0
Segmentation fault

Any ideas?

hi, aaatrsh

Have you installed our precompiled libcamera library.

You can install them by referring to the documentation at the link below and try running the capture_demo.py script again.

Install the Driver

Install libcamera

For users using libcamera on kernel version 5.10.92 (2022/01/28 SD Card Image), please use libcamera python wrap in this package: libcamera_python-1.0.2.zip - Google Drive

Hi, I’ve got the auto focus camera following the setup steps for that I can take photos with libcamera-still.

However what I’m trying to do it take a series of photos and programatically adjust the focus inbetween shots. The plan is that I’ll then use the resulting pics for focus stacking. I’d like to take the photos with as short a delay between them as possible.

When I try to use this wrapper, I get nowhere. If I install the other driver with shared in your instructions above, I can’t take photos at all, not even with the command line. Are the 2 compatible? Any suggestions on what I’m doing wrong?

Thanks

Hi baralog

What version of raspberry pi are you using? If the system version is 5.10.92, please refer to the instructions below to download the libcamera python library.

OK, I’ll give it another try this evening. I’m running on a Raspberry Pi Zero 2W running the latest 32bit lite version of Bullseye 5.10.103

BTW is the library compatible with 64 bit raspbian? (note: I’m not running 64bit, I’m just curious)

Hi, baralong

Currently this library is not compatible with 64-bit Raspberry Pi systems. We will make a library compatible with 64-bit systems. Thank you for your reminder.

Do I need to uninstall the other driver? If so, how do I do that? The install_pivariety_pkgs.sh script doesn’t seem to have a “remove” option. Do I need to do a fresh install of the OS?

thanks

Hi, baralong

If you upgrade from kernel version 5.10.92 to 5.10.103, you do not need to reinstall libcamera. Install libcamera python library version 1.0.2 directly.

If you upgrade to 5.10.103 from a kernel version prior to 5.10.92, you will need to reinstall libcamera. Then install libcamera python library version 1.0.1.

To reinstall libcamera, just execute the install_pivariety_pkgs.sh script again, the higher version will overwrite the lower version.

Alternatively, you can uninstall using the “sudo apt remove” command. Then install libcamera again.

Thanks again. I’ve had no luck with opencv, but I’m getting data :smile:

I needed to folow instructions here: Troubleshooting ImportError — NumPy v1.23.dev0 Manual

I also got the following:

[1:15:41.023748100] [13881]  INFO Camera camera_manager.cpp:293 libcamera v0.0.0
[1:15:41.071529856] [13915]  WARN CameraSensorProperties camera_sensor_properties.cpp:141 No static properties available for 'imx519'
[1:15:41.071723084] [13915]  WARN CameraSensorProperties camera_sensor_properties.cpp:143 Please consider updating the camera sensor properties database
[1:15:41.071872875] [13915] ERROR CameraSensor camera_sensor.cpp:551 'imx519 10-001a': Camera sensor does not support test pattern modes.
[1:15:41.152602689] [13915]  WARN RPI raspberrypi.cpp:1233 Mismatch between Unicam and CamHelper for embedded data usage!
[1:15:41.153874663] [13915] ERROR DelayedControls delayed_controls.cpp:87 Delay request for control id 0x009a090a but control is not exposed by device /dev/v4l-subdev0
[1:15:41.154786223] [13915]  INFO RPI raspberrypi.cpp:1356 Registered camera /base/soc/i2c0mux/[email protected]/[email protected] to Unicam device /dev/media3 and ISP device /dev/media0
[1:15:41.159699955] [13881]  INFO Camera camera.cpp:1028 configuring streams: (0) 5344x4012-RGB888
[1:15:41.160920055] [13915]  INFO RPI raspberrypi.cpp:751 Sensor: /base/soc/i2c0mux/[email protected]/[email protected] - Selected sensor format: 4656x3496-SRGGB10_1X10 - Selected unicam format: 4656x3496-pRAA

But I’ll have a look at it tomorrow.

It looks like data.imageData gets the image, how do I save that to a file? what format is it? JPG? RAW?

Hello @yang and the support team. Thank you for this thread. I was able to finally get libcamera and the python wrapper working with your demo python files (full_resolution_demo.py, capture_demo.py, and controls_demo.py). I’ve been making a few alterations, but what I don’t see is any command to tell the imx519 to autofocus. Is there a set command I can send to it that tells it to autofocus, or is there code somewhere I can see how to make it autofocus?

Hi, MichRX7

You can set autofocus like the code below.

cam.set(libcamera.AfTrigger, 1)

Screenshot from 2021-11-05 09-39-38

Also, all control options can be found in the document linked below.
libcamera Controls

Hello @yang and support team. Thank you for the autofocus code, that will be very helpful once this camera is up and running. And I really want to get this working because I am very interested in upgrading to Hawk-eye if possible (I have already pre-ordered one).

I believe I have followed all the installation instructions to the letter, but still I am unable to get the demo apps to run. I can run libcamera-hello and other libcamera options, but this is what I am currently getting when I try and run your full_resolution_demo.py and other demos from inside libcamera_python:

python3 full_resolution_demo.py
[0:04:05.503064666] [1219] WARN IPAManager ipa_manager.cpp:149 No IPA found in ‘/usr/local/lib/arm-linux-gnueabihf/libcamera’
[0:04:05.503308774] [1219] INFO Camera camera_manager.cpp:293 libcamera v0.0.0
[0:04:05.531612151] [1223] WARN CameraSensorProperties camera_sensor_properties.cpp:141 No static properties available for ‘imx519’
[0:04:05.531704299] [1223] WARN CameraSensorProperties camera_sensor_properties.cpp:143 Please consider updating the camera sensor properties database
[0:04:05.531967018] [1223] ERROR RPI raspberrypi.cpp:1030 Failed to load a suitable IPA library
Segmentation fault

hi, MichRX7

What version of the Raspberry Pi system are you using? Different versions of the system need to install different versions of the libcamera python library.

Below are the detailed instructions.

Hello @yang I am currently in version 5.10.103-v7l+

If I uninstall and re-install, do I have to go through everything on this page again?

Also my python app is run with sudo since it accesses the gpio pins. Do I need to install the pivariety packages with sudo?

hi, MichRX7

Follow the tutorial to reinstall the deb package.