This is a wrapper around libcamera which hides all its complexity and makes it as easy as possible to get a humble callback delivering openCV BGR frames.
The motivation behind this wrapper is that libcamera's raw callback interface forces the user to understand complex memory mapping, mmap and conventions buried deep in its example applications. This wrapper here is an attempt to abstract all this complexity away and instead provide a simple friendly callback delivering openCV BGR frames so that the coder can focus on their actual task.
Works with:
- Raspberry PI CSI cameras
- Webcams delivering MJPEG
apt install libopencv-dev libcamera-dev libturbojpeg0-dev
cmake .
make
sudo make install
-
Include
libcam2opencv.hand addtarget_link_libraries(yourproj cam2opencv)to yourCMakeLists.txt. -
Create a class containing a callback receiving the frames:
class MyAI {
public:
void onFrame(const cv::Mat &frame) {
ai->detect(frame);
}
};
- Create instances of the camera and your application:
Libcam2OpenCV camera;
MyAI myAI;
- Register the callback
camera.registerCallback([&](const cv::Mat &mat, const libcamera::ControlList &meta){ myAI.onFrame(mat); });
- Create an instance of the cameramanager (only one allowed for all cameras) and start it:
libcamera::CameraManager cm;
cm.start();
- Start the camera delivering frames via the callback
camera.start(cm);
-
sleep or run your GUI or wait for a keypress
-
Stop the camera and the cameramanager
camera.stop();
cm.stop();
The full class documentation is here: https://berndporr.github.io/libcamera2opencv/
In the subdirectory metadataprinter is a demo which just prints the sensor
metadata from the callback. This is useful to see what
info is available, for example the sensor timestamp to
check the framerate.
The subdirectory qtviewer contains a simple QT application which displays the camera on screen. This shows also how the callback triggers QT's update() without resorting to ugly message queues as QT deals with the heavy lifting itself -- while the callback returns as quickly as possible.
The Raspberry PI 5 allows 2 cameras to be connected. The subdirectory dualcamviewer contains a QT application which displays the images of two cameras on screen. Again, it shows how the image update is handled with QT's update() command keeping the callback as short as possible and leaving it to QT to do the actual displaying.
Based on https://github.com/kbingham/simple-cam, libcamera's qcam and then turned into this library by Bernd Porr. Additional features by Raphael Nekam.
