When switching from the legacy camera stack under Raspberry Pi OS with debian 10 to the new libcamera camera stack (in Raspberry Pi OS with debian 11 it is standard), I cannot use OpenCV any longer.
Even if I try a very simple example like this one:
import cv2 cap = cv2.VideoCapture(0) # Capture frame ret, frame = cap.read() if ret: cv2.imwrite('image.jpg', frame) cap.release() With the legacy camera stack it works without any issue and when I add the line
camera_auto_detect=1 in the file /boot/config.txt and do a reboot, I get this error message:
python3 cv2test.py [ WARN:0] global /tmp/pip-wheel-2c57qphc/opencv-python_86774b87799240fbaa4c11c089d08cc3/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index Often read recommendations like modify cv2.VideoCapture(0) into cv2.VideoCapture(cv2.CAP_V4L2) or cv2.VideoCapture(1) or cv2.VideoCapture(-1) did not help.
The camera works well. I can run this without trouble:
libcamera-vid -t 0 This is the output of v4l2-ctl:
$ v4l2-ctl --list-devices bcm2835-codec-decode (platform:bcm2835-codec): /dev/video10 /dev/video11 /dev/video12 /dev/video18 bcm2835-isp (platform:bcm2835-isp): /dev/video13 /dev/video14 /dev/video15 /dev/video16 unicam (platform:fe801000.csi): /dev/video0 /dev/video1 I also gave /dev/video* full permissions (777) just to ensure that it is not a permission issue. But this does not help.
My user (pi) is also a member of the group video:
$ whoami pi $ groups pi adm dialout cdrom sudo audio video plugdev games users input netdev lpadmin gpio i2c spi I installed OpenCV via pip:
$ pip3 list | grep cv opencv-python 4.5.4.60 $ uname -a Linux raspberrypi 5.10.63-v7l+ #1496 SMP Wed Dec 1 15:58:56 GMT 2021 armv7l GNU/Linux Has anyone here an idea of how to get /dev/video? with opencv working with the libcamera camera stack?