OpenCv error can't open camera through video capture

后端 未结 6 1970
太阳男子
太阳男子 2021-01-17 20:01

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


        
相关标签:
6条回答
  • 2021-01-17 20:35

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

    cap = cv2.VideoCapture(-1)
    

    This solved the issue.

    0 讨论(0)
  • 2021-01-17 20:41

    This issue is due to the interruption. Try to end the execution with the key 'q' for example, don't close the window suddenly.

    I solved the same issue by opening terminal again and execute the same script again.

    0 讨论(0)
  • 2021-01-17 20:45

    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
    
    0 讨论(0)
  • 2021-01-17 20:46

    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.

    0 讨论(0)
  • 2021-01-17 20:53

    I will not go to that part What you are trying to do, here is just a block of code that can open your camera every time you run it,

    python: 3.7.3

    OpenCV: 4.1.0

    import cv2
    cap = 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()
    
    0 讨论(0)
  • 2021-01-17 20:53

    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.

    0 讨论(0)
提交回复
热议问题