0

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?

asked Dec 25, 2021 at 16:14

2 Answers 2

1

I got the same issue and after some research I found that in Debian 11 Bullseye, you can only capture live video with a streaming framework, like gstreamer or ffmpeg.

So you will have to compile OpenCV and OpenCV-Python with the gstreamer support then you have to tell to OpenCV to capture the video from gstreamer.

This is the gstreamer command using libcamera as source, try and see if it works out of OpenCV.

$ gst-launch-1.0 libcamerasrc ! video/x-raw, width=1280, height=720, framerate=30/1 ! videoconvert ! videoscale ! clockoverlay time-format="%D %H:%M:%S" ! video/x-raw, width=640, height=360 ! autovideosink

Then in your Python code, you do this:

import cv2
pipeline = "libcamerasrc ! video/x-raw, width=640, height=480, framerate=15/1 ! videoconvert ! videoscale ! video/x-raw, width=640, height=480 ! appsink"
cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)

I hope it helps, let me know if it works for you.

answered Jan 16, 2022 at 15:40
0

This issue is due to the interruption. Try to end the execution with the key 'q' for example, don't close the window suddenly.

I solved the same issue by opening terminal again and execute the same script again.

answered Mar 30, 2022 at 9:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.