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})