I have been trying to create a simple program with Python which uses OpenCV to get a video feed from my webcam and display it on the screen.
I know I am partly there because the window is created and the light on my webcam flicks on, but it just doesn’t seem to show anything in the window. Hopefully someone can explain what I’m doing wrong.
import cv
cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CaptureFromCAM(0)
def repeat():
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
while True:
repeat()
On an unrelated note, I have noticed that my webcam sometimes changes its index number in cv.CaptureFromCAM, and sometimes I need to put in 0, 1 or 2 even though I only have one camera connected and I haven’t unplugged it (I know because the light doesn’t come on unless I change the index). Is there a way to get Python to determine the correct index?
Try adding the line
c = cv.WaitKey(10)at the bottom of yourrepeat()method.This waits for 10 ms for the user to enter a key. Even if you’re not using the key at all, put this in. I think there just needed to be some delay, so
time.sleep(10)may also work.In regards to the camera index, you could do something like this:
This will find the index of the first “working” capture device, at least for indices from 0-2. It’s possible there are multiple devices in your computer recognized as a proper capture device. The only way I know of to confirm you have the right one is manually looking at your light. Maybe get an image and check its properties?
To add a user prompt to the process, you could bind a key to switching cameras in your repeat loop:
disclaimer: I haven’t tested this so it may have bugs or just not work, but might give you at least an idea of a workaround.