Can't read camera feed

asked 2017-01-10 10:17:06 -0600

greengiant83 gravatar image

I am new to Python, and I am trying to get OpenCV 3.2.0 running on my Windows 10 machine with a Logitech C920

The script will pop up a Video Source dialog where I select my Logitech C920. The little blue light on that indicates that its being accessed turns on, but the cap.read() call always returns false. If I run the same code using the vtest.avi video file, it runs fine

Whats going on? Why can't I read the frames from the live webcam? If I switch the camera to use the one in my HTC Vive it gets the frames and shows a window, but the image data is always black. Both cameras work fine if I check them in Google Hangouts

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32

OpenCV 3.2.0

import numpy as np
import cv2

#cap = cv2.VideoCapture("vtest.avi");
cap = cv2.VideoCapture(-1);

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    print cap.isOpened(), ret
    if frame is not None:
        # Display the resulting frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(22) & 0xFF == ord('q'):
            break
    else:
        print "Frame is None"

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
print "I am done"
edit retag flag offensive close merge delete

Comments

usiing -1 as cam id will force it into using VFW, which microsoft is trying to kill, atm.

can you try with 0 instead ?

berak gravatar imageberak ( 2017-01-10 10:19:38 -0600 )edit

What do you know, it works. I tried that yesterday on my office computer and it didnt change anything, thats why I switched to -1 in the first place. Today on my home computer that works like a charm.

greengiant83 gravatar imagegreengiant83 ( 2017-01-10 11:20:18 -0600 )edit

I used cap = cv2.VideoCapture(0+cv2.CAP_DSHOW) to access my laptops cam. The DSHOW here is directx show you can find it in the opencv python documentation online.

Santosh Meshram gravatar imageSantosh Meshram ( 2020-10-14 09:05:29 -0600 )edit