opencv: Camera Index out of range issue - Have tried everything!!!

System information (version)
  • OpenCV => 4.7.0
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio Code 1.76.2
Detailed description

My camera just won’t open with the code below. I have tried searching everywhere I cannot figure it out. My camera is a usb camera and works just fine. Everytime I try to run “cap = cv2.VideoCapture(1)” it outputs the error: [ERROR:0@3.185] global obsensor_uvc_stream_channel.cpp:156 cv::obsensor::getStreamChannelGroup Camera index out of range

Steps to reproduce
Issue submission checklist
  • I report the issue, it’s not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 25 (4 by maintainers)

Most upvoted comments

JUST GIVE ACCESS TO CAMERA Screenshot 2023-05-08 011659

The following code is working on my PC. where I used Logitech external Webcam. The code is below

cap=cv2.VideoCapture(0,cv2.CAP_DSHOW) #// if you have second camera you can set first parameter as 1
if not (cap.isOpened()):
    print("Could not open video device")
while True: 
    ret,frame= cap.read()
    cv2.imshow("Live",frame)
    cv2.waitKey(1)
cv2.destroyAllWindows()

image I met this problem too.But what could I do?Here are my test code: image

Change value inside VideoCapture to 0 or 1, and try it will work.

I get this as well, but only if I try to access it in a script on a RockPi:

_video_input = cv2.VideoCapture(0) ->

[ WARN:0@4.004] global cap_v4l.cpp:982 open VIDEOIO(V4L2:/dev/video0): can't open camera by index
[ERROR:0@4.006] global obsensor_uvc_stream_channel.cpp:156 getStreamChannelGroup Camera index out of range

If I do it from the interactive interpreter it works just fine.

Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> _video_input = cv2.VideoCapture(0)
>>> _video_input.read()
(True, array([[[ 43,  38,  46],
        [ 47,  43,  50],
        [ 43,  45,  51],
        ...,

The same script works fine on my local machine.