# OpenCV VideoCapture - Index out of range - kworker process

**URL:** <https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652>\
**Category:** C++\
**Tags:** videoio\
**Created:** [June 23, 2023, 8:37pm UTC](https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652 "2023-06-23T20:37:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Edinger](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alexander_edinger/32/8152_2.png) [@Alexander\_Edinger](https://forum.opencv.org/u/Alexander_Edinger)\
**Post date:** [June 23, 2023, 8:37pm UTC](https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652/1 "2023-06-23T20:37:02Z")

</div>

**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

```shell
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

```auto
#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

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

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 23, 2023, 9:06pm UTC](https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652/2 "2023-06-23T21:06:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Alexander\_Edinger](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alexander_edinger/32/8152_2.png) [@Alexander\_Edinger](https://forum.opencv.org/u/Alexander_Edinger)\
**Post date:** [June 24, 2023, 6:38am UTC](https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652/3 "2023-06-24T06:38:50Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Alexander\_Edinger](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alexander_edinger/32/8152_2.png) [@Alexander\_Edinger](https://forum.opencv.org/u/Alexander_Edinger)\
**Post date:** [June 24, 2023, 7:12am UTC](https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652/4 "2023-06-24T07:12:15Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/91bdf411854aa7fc3425de2fe8fd87fc2f1634f9.png)

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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 24, 2023, 9:26am UTC](https://forum.opencv.org/t/opencv-videocapture-index-out-of-range-kworker-process/13652/5 "2023-06-24T09:26:26Z")

</div>

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.
