OpenCV VideoCapture - Index out of range - kworker process

Specifications
CUDA 11.6
OpenCV 4.x with contrib & CUDA support
Ubuntu 20.04
Zed2i UVC Camera

Problem
When I try to get an image from the camera, I get an index out of range error.
Running my script in the next 3-5 seconds after the error will correctly fetch the camera images with the same index.
I realized, that when I open my camera with cheese (ubuntu software to access webcams, …) and run my code afterwards, it runs correctly and I get the images.

I looked some more into this issue and found out, that a process called “kworker/uvcvideo” is created when I open cheese and when that process (kernel worker process) runs, I can run my code as well and the connectivity with UVC is given.
As soon as the process terminated (usually after ~10 minutes) my code doesn’t work again, unless I open cheese or run my code twice in quick succession (as stated above).

I have used different backends for VIdeoCapture, the default one I use is V4L2. None seem to fix the error.

I guess it is an error with my specific camera model, but when I used OpenCV without CUDA support, I was able to fetch images reliably with the use of camera indizes. Only after I upgraded to OpenCV with CUDA support did I encounter this error.

Further Info

OpenCV cmake command

cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D CUDA_ARCH_BIN=7.5 \
-D OPENCV_EXTRA_MODULES_PATH=../path/to/contrib/modules/ \
-D BUILD_EXAMPLES=OFF \
-D HAVE_opencv_python3=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
../opencv/path/

Program to fetch the images

#include <opencv2/opencv.hpp>

#include <iostream>
#include <chrono>
#include <thread>

#include <string.h>

int main(int argc, char* argv[])
{

    cv::VideoCapture cap = cv::VideoCapture(std::stoi(std::string(argv[1])));

    if (!cap.isOpened())
    {
        std::cout << "nope!" << std::endl;
        return -1;
    }
    std::cout << cap.getBackendName() << std::endl;
}

CMake File

cmake_minimum_required(VERSION 3.8)
project(test_project)

set(CMAKE_CXX_STANDARD 17)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(OpenCV REQUIRED)
find_package(Boost 1.45.0 COMPONENTS filesystem) # unnecessary

include_directories(${Boost_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(executable main4.cpp)

target_link_libraries(executable ${OpenCV_LIBS} ${Boost_LIBRARIES})

using guvcview or any other program, is the issue reproducible?

this doesn’t sound like an OpenCV problem. opencv merely uses V4L API.

your opencv versions with/without cuda, what were their exact version numbers? who compiled them?

who compiled them?

I am unsure what relevance this has, but in both cases, I compiled them. I do have administrator rights and I am not behind any filtering proxy.

For OpenCV versions, I used the following:

  • No CUDA Support: OpenCV 4.7.0
  • CUDA Support: OpenCV 4.7.0 and OpenCV_contrib 4.7.0

I will try the connectivity using guvcview sometime this day. For now, I can unfortunately not access the camera.

Here are some screenshots from guvcview. It detected, that a camera is present, but a videostream was not possible.

Interface for selecting camera
I can select the Zed2i Camera in “Device”. guvcview prompts me to restart the application, which leads to this:

Trying to update the camera from internal webcam to Zed2i
Screenshot from 2023-06-24 09-01-06

This leads to an error, namely “no video device found” (as visible in the picture). When running cheese on ubuntu, I can access the camera from guvcview , since the kernel worker process is created.

great. now it’s demonstrated to not be an OpenCV issue but a device/driver issue. that helps you because it narrows down where you need to look for the issue.

perhaps seek support from the manufacturer of that device.