OpenCv error can't open camera through video capture

67,545

Solution 1

I got the same error. Try changing 0 to -1

cap = cv2.VideoCapture(-1)

This solved the issue.

Solution 2

I got the same problem when I created more than one instance of the cv2.VideoCapture(0). So check if your code contains multiple initializations or sections which call cv2.VideoCapture(0) more than once. I was facing this problem while running the flask server in debug mode because it called cv2.VideoCapture(0) twice.

import cv2
cap = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(0)
while True:

    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Error:

python3 debugCamera.py 
[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

Solution 3

Most likely a permission issue on /dev/video0.

Check if you are part of "video" group.
id -a

if you don't see video in your group list add sudo usermod -a -G video

for Ubuntu users:(20.04)
sudo usermod -a -G video $LOGNAME

Logout and log back in and try it.

Solution 4

I've encountered the same issue and attempted several methods like cv2.VideoCapture(-1) or cv2.VideoCapture(1) but without much success.

I succeeded after reading this article and disabling debug-mode

Solution 5

I had the same problem, Just change 0 to 1,then to -1 and back again to 0. Don't know why this worked for me.

Share:
67,545

Related videos on Youtube

Nabeel Ayub
Author by

Nabeel Ayub

Updated on August 02, 2022

Comments

  • Nabeel Ayub
    Nabeel Ayub almost 2 years

    I was using my cam through opencv and suddenly after restarting I ran my code it shows below error:

    [ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (802) open VIDEOIO ERROR: V4L: can't open camera by index 0
    Traceback (most recent call last):
      File "test.py", line 20, in <module>
        retval, buffer_img = cv2.imencode('.jpg', frame)
    cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:877: error: (-215:Assertion failed) !image.empty() in function 'imencode'
    
    cap = cv2.VideoCapture(0)  # here it throws an error
    
    
    import json
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
    
        retval, buffer_img = cv2.imencode('.jpg', frame)
    
        resdata = base64.b64encode(buffer_img)
    
        resdata = "data:image/png;base64,"+ str(resdata.decode("utf-8"))
        PARAMS = {'image': resdata}
    
        # Our operations on the frame come here
        #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
        # Display the resulting frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    

    I also tried with cap = cv2.VideoCapture(1) but then it shows can't find camera

    How can I fix this issue?

  • Nabeel Ayub
    Nabeel Ayub over 4 years
    cap = cv2.VideoCapture(0), its not working,its giving me the error I mentioned in the beginning
  • Sohel Reza
    Sohel Reza over 4 years
    what kind of camera are you using? Laptop default camera(0), external camera(1-....), just change the value 0-... and see what happen.
  • Nabeel Ayub
    Nabeel Ayub over 4 years
    I have used (-1) it gives can't find camera device
  • Sohel Reza
    Sohel Reza over 4 years
    just change it 0 to 1 or 2 or 3 , if it is not work then problem maybe you python or openCV version. you can reinstall it and see what happen, just a quick suggestion
  • Nabeel Ayub
    Nabeel Ayub over 4 years
    did all sort of things still can't fix,I think there is ubuntu compatibility issue otherwise it should run.
  • siful islam
    siful islam over 3 years
    You try to open a camera two times. When a hardware ingress a work then it don't allow outer command at same time .
  • siful islam
    siful islam over 3 years
    your got error cause -1 index. -1 index meaning last element of list. i recommend you. you may use 0 if you have webcam.
  • Diego Medeiros
    Diego Medeiros almost 3 years
    It worked for me to run a X application inside docker container. I have also needed to install v4l-utils. Just like this tutorial.
  • Kabir Lovero
    Kabir Lovero over 2 years
    it makes no sense whatsoever , but it worked for me too
  • Chris
    Chris over 2 years
    Welcome to Stack Overflow. Are you sure this answers the question at the top of the page? It doesn't appear to be related. Please read How to Answer.
  • Yhozen
    Yhozen about 2 years
    I don't know why but it works for me, no other solution worked