Videocapture read() method returns empty ret

I am using a PI 4 with 64 bit os bullseye.
python 3.7.7
opencv-python 4.5.5.62

I am able to access pi cam from libcamera, i am also able to read a mp4 video using opencv; however when running opencv videocapture function, read method returns empty ret value. see code below:

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(-1)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

ERROR:

(virtenv3.7) pi@pi:~ $ python opcvtest.py
Can't receive frame (stream end?). Exiting ...
(virtenv3.7) pi@pi:~ $

Opencv build info:

General configuration for OpenCV 4.5.5 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2022-02-05T09:15:14Z
    Host:                        Linux 5.10.92-v8+ aarch64
    CMake:                       3.22.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                /usr/bin/c++  (ver 10.2.1)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed
    Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          /usr/lib/aarch64-linux-gnu/libjpeg.so /usr/lib/aarch64-linux-gnu/libpng.so /usr/lib/aarch64-linux-gnu/libtiff.so /usr/lib/aarch64-linux-gnu/libz.so dl m pthread rt
    3rdparty dependencies:       libprotobuf ade ittnotify libwebp libopenjp2 IlmImf quirc tegra_hal

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           GTK3
    GTK+:                        YES (ver 3.24.24)
      GThread :                  YES (ver 2.66.8)
      GtkGlExt:                  NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
    JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.37)
    TIFF:                        /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      YES (2.2.6)
    FFMPEG:                      YES
      avcodec:                   YES (58.91.100)
      avformat:                  YES (58.45.100)
      avutil:                    YES (56.51.100)
      swscale:                   YES (5.7.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       YES (ver 3.3.9)
    Custom HAL:                  YES (carotene (ver 0.0.1))
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (no extra features)
    Include path:                /tmp/pip-install-m36v8df1/opencv-python/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /home/pi/virtenv3.7/bin/python3.7 (ver 3.7.6)
    Libraries:                   /usr/local/lib/libpython3.7m.a (ver 3.7.6)
    numpy:                       /tmp/pip-build-env-_z0k49zk/overlay/lib/python3.7/site-packages/numpy/core/include (ver 1.19.3)
    install path:                python/cv2/python-3

  Python (for build):            /home/pi/virtenv3.7/bin/python3.7

  Java:
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /tmp/pip-install-m36v8df1/opencv-python/_skbuild/linux-aarch64-3.7/cmake-install
-----------------------------------------------------------------

first I’d like to say, I’m pleased to see good error checking on the use of VideoCapture, and I’m pleased to see build info right away.

it is possible that the pi cam isn’t yet exposed as a V4L device. this, I hear, requires some system configuration and drivers. It’s not exactly plug-and-play.

libcamera can access the camera in any case because it accesses the hardware directly, like the V4L driver would do.

check if /dev/video0 exists.

a few links to this issue:

thanks for responding.
video0 is there, however vcgencmd get_camera is 0. so we know the camera is working as i can access using libcamera, does vcgencmd uses the driver which you mentioned might be missing? If yes how do i install it?

pi@pi:~ $ sudo ls -l /dev/video*
crw-rw----+ 1 root video 81, 12 Feb  9 23:17 /dev/video0
crw-rw----+ 1 root video 81,  4 Feb  9 23:17 /dev/video10
crw-rw----+ 1 root video 81,  7 Feb  9 23:17 /dev/video11
crw-rw----+ 1 root video 81, 10 Feb  9 23:17 /dev/video12
crw-rw----+ 1 root video 81,  0 Feb  9 23:17 /dev/video13
crw-rw----+ 1 root video 81,  1 Feb  9 23:17 /dev/video14
crw-rw----+ 1 root video 81,  2 Feb  9 23:17 /dev/video15
crw-rw----+ 1 root video 81,  3 Feb  9 23:17 /dev/video16
crw-rw----+ 1 root video 81, 11 Feb  9 23:17 /dev/video18
crw-rw----+ 1 root video 81,  5 Feb  9 23:17 /dev/video20
crw-rw----+ 1 root video 81,  6 Feb  9 23:17 /dev/video21
crw-rw----+ 1 root video 81,  8 Feb  9 23:17 /dev/video22
crw-rw----+ 1 root video 81,  9 Feb  9 23:17 /dev/video23
pi@pi:~ $ vcgencmd get_camera
supported=0 detected=0
pi@pi:~ $ sudo vcgencmd get_camera
supported=0 detected=0
pi@pi:~ $

Few more checks as recommended by the other post you shared:

pi@pi:~ $ sudo modprobe bcm2835-v4l2
pi@pi:~ $
pi@pi:~ $ dmesg | grep bcm2835
[    0.000000] Kernel command line: coherent_pool=1M 8250.nr_uarts=0 snd_bcm2835.enable_compat_alsa=0 snd_bcm2835.enable_hdmi=1  smsc95xx.macaddr=DC:A6:32:FE:27:E0 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000  console=ttyS0,115200 console=tty1 root=PARTUUID=418a3386-02 rootfstype=ext4 fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
[    0.088767] bcm2835-mbox fe00b880.mailbox: mailbox enabled
[    0.172001] bcm2835-dma fe007000.dma: DMA legacy API manager, dmachans=0x1
[    1.302842] gpiomem-bcm2835 fe200000.gpiomem: Initialised: Registers at 0xfe200000
[    1.488467] bcm2835-wdt bcm2835-wdt: Broadcom BCM2835 watchdog timer
[    1.488949] bcm2835-power bcm2835-power: Broadcom BCM2835 power domains driver
[    1.490118] mmc-bcm2835 fe300000.mmcnr: mmc_debug:0 mmc_debug2:0
[    1.490137] mmc-bcm2835 fe300000.mmcnr: DMA channel allocated
[    4.913001] bcm2835_vc_sm_cma_probe: Videocore shared memory driver
[    5.032253] snd_bcm2835: module is from the staging directory, the quality is unknown, you have been warned.
[    5.086416] bcm2835_audio bcm2835_audio: card created with 8 channels
[    5.254742] bcm2835_mmal_vchiq: module is from the staging directory, the quality is unknown, you have been warned.
[    5.254751] bcm2835_mmal_vchiq: module is from the staging directory, the quality is unknown, you have been warned.
[    5.289442] bcm2835_isp: module is from the staging directory, the quality is unknown, you have been warned.
[    5.304189] bcm2835-isp bcm2835-isp: Device node output[0] registered as /dev/video13
[    5.317198] bcm2835-isp bcm2835-isp: Device node capture[0] registered as /dev/video14
[    5.319460] bcm2835-isp bcm2835-isp: Device node capture[1] registered as /dev/video15
[    5.322180] bcm2835_v4l2: module is from the staging directory, the quality is unknown, you have been warned.
[    5.327411] bcm2835-isp bcm2835-isp: Device node stats[2] registered as /dev/video16
[    5.327459] bcm2835-isp bcm2835-isp: Register output node 0 with media controller
[    5.327481] bcm2835-isp bcm2835-isp: Register capture node 1 with media controller
[    5.327500] bcm2835-isp bcm2835-isp: Register capture node 2 with media controller
[    5.327518] bcm2835-isp bcm2835-isp: Register capture node 3 with media controller
[    5.328141] bcm2835_codec: module is from the staging directory, the quality is unknown, you have been warned.
[    5.353456] bcm2835-codec bcm2835-codec: Device registered as /dev/video10
[    5.353518] bcm2835-codec bcm2835-codec: Loaded V4L2 decode
[    5.355906] bcm2835-isp bcm2835-isp: Device node output[0] registered as /dev/video20
[    5.362328] bcm2835-isp bcm2835-isp: Device node capture[0] registered as /dev/video21
[    5.362524] bcm2835-codec bcm2835-codec: Device registered as /dev/video11
[    5.362581] bcm2835-codec bcm2835-codec: Loaded V4L2 encode
[    5.364371] bcm2835-isp bcm2835-isp: Device node capture[1] registered as /dev/video22
[    5.365889] bcm2835-isp bcm2835-isp: Device node stats[2] registered as /dev/video23
[    5.368037] bcm2835-isp bcm2835-isp: Register output node 0 with media controller
[    5.368161] bcm2835-isp bcm2835-isp: Register capture node 1 with media controller
[    5.368228] bcm2835-isp bcm2835-isp: Register capture node 2 with media controller
[    5.368247] bcm2835-isp bcm2835-isp: Register capture node 3 with media controller
[    5.370329] bcm2835-isp bcm2835-isp: Loaded V4L2 bcm2835-isp
[    5.379618] bcm2835-codec bcm2835-codec: Device registered as /dev/video12
[    5.379725] bcm2835-codec bcm2835-codec: Loaded V4L2 isp
[    5.385634] bcm2835-codec bcm2835-codec: Device registered as /dev/video18
[    5.385690] bcm2835-codec bcm2835-codec: Loaded V4L2 image_fx
pi@pi:~ $
pi@pi:~ $ sudo lsmod | grep bcm2835
bcm2835_unicam         53248  0
bcm2835_codec          49152  0
bcm2835_v4l2           45056  0
bcm2835_isp            32768  0
v4l2_dv_timings        40960  1 bcm2835_unicam
v4l2_fwnode            20480  2 ov5647,bcm2835_unicam
bcm2835_mmal_vchiq     36864  3 bcm2835_codec,bcm2835_v4l2,bcm2835_isp
v4l2_mem2mem           40960  1 bcm2835_codec
v4l2_async             24576  3 v4l2_fwnode,ov5647,bcm2835_unicam
videobuf2_vmalloc      20480  1 bcm2835_v4l2
videobuf2_dma_contig    24576  3 bcm2835_codec,bcm2835_unicam,bcm2835_isp
i2c_bcm2835            16384  0
videobuf2_v4l2         32768  5 bcm2835_codec,bcm2835_unicam,bcm2835_v4l2,v4l2_mem2mem,bcm2835_isp
videobuf2_common       65536  9 bcm2835_codec,videobuf2_vmalloc,videobuf2_dma_contig,videobuf2_v4l2,bcm2835_unicam,bcm2835_v4l2,v4l2_mem2mem,videobuf2_memops,bcm2835_isp
snd_bcm2835            24576  1
videodev              262144  9 v4l2_async,bcm2835_codec,ov5647,videobuf2_v4l2,bcm2835_unicam,bcm2835_v4l2,videobuf2_common,v4l2_mem2mem,bcm2835_isp
mc                     57344  8 videodev,bcm2835_codec,ov5647,videobuf2_v4l2,bcm2835_unicam,videobuf2_common,v4l2_mem2mem,bcm2835_isp
snd_pcm               135168  5 snd_bcm2835,snd_soc_hdmi_codec,snd_compress,snd_soc_core,snd_pcm_dmaengine
vc_sm_cma              36864  2 bcm2835_mmal_vchiq,bcm2835_isp
snd                   102400  8 snd_bcm2835,snd_soc_hdmi_codec,snd_timer,snd_compress,snd_soc_core,snd_pcm
pi@pi:~ $

Actually i just found the issue. on the raspi-config i had to enable legacy camera support ( not sure why, i thought this was deprecated and not required). now it works and opencv can access the camera.

(virtenv3.7) pi@pi:~/ml/detection $ vcgencmd get_camera
supported=1 detected=1
(virtenv3.7) pi@pi:~/ml/detection $

thank you again

1 Like

accessing these things through /dev/video* (V4L) is supposed to work, regardless of what they’re doing to the plumbing in the background. if it doesn’t, they messed up. that is independent of OpenCV, so you can check functionality using any suitable program, e.g. VLC or ffplay/ffmpeg.

this isn’t a raspberry pi forum, so you should look for raspberry-pi-specific assistance elsewhere. we may try but we aren’t necessarily experts on these devices.

they have this to say about the situation:

hi, i’d like to know how did you change the legacy camera with the bullseye version.
I have the same problem as you but i don’t find the way to change the “legacy camera”.
Thank you :slight_smile: