samedi 9 août 2014

Affichage webcam nourrir en utilisant opencv et python - Stack Overflow


I have been trying to create a simple program with python which utilises 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 doesnt seem to show anything in the window. hopefully someone can explain what im 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 doesnt come on unless I change the index). Is there a way to get python to determine the correct index?


thanks


Mitch




Try adding the line c = cv.WaitKey(10) at the bottom of your repeat() 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:


for i in range(3):
capture = cv.CaptureFromCAM(i)
if capture: break

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:


import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
global capture #declare as globals since we are assigning to them now
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = cv.WaitKey(10)
if(c=="n"): #in "n" key is pressed while the popup window is in focus
camera_index += 1 #try the next camera index
capture = cv.CaptureFromCAM(camera_index)
if not capture: #if the next camera index didn't work, reset to 0.
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

while True:
repeat()

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.




An update to show how to do it in the recent versions of OpenCV:


import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")

It works in OpenCV-2.4.2 for me.




If you only have one camera, or you don't care which camera is the correct one, then use "-1" as the index. Ie for your example capture = cv.CaptureFromCAM(-1).




This didn't work for me, but the following Gist was "copy/paste play" https://gist.github.com/2130079




change import cv to import cv2.cv as cv See also the post here.




Try this, simple, but haven't figured out a graceful way to exit yet.


import cv2.cv as cv import time


cv.NamedWindow("camera", 0)

capture = cv.CaptureFromCAM(0)

while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyAllWindows()


I have been trying to create a simple program with python which utilises 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 doesnt seem to show anything in the window. hopefully someone can explain what im 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 doesnt come on unless I change the index). Is there a way to get python to determine the correct index?


thanks


Mitch



Try adding the line c = cv.WaitKey(10) at the bottom of your repeat() 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:


for i in range(3):
capture = cv.CaptureFromCAM(i)
if capture: break

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:


import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
global capture #declare as globals since we are assigning to them now
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = cv.WaitKey(10)
if(c=="n"): #in "n" key is pressed while the popup window is in focus
camera_index += 1 #try the next camera index
capture = cv.CaptureFromCAM(camera_index)
if not capture: #if the next camera index didn't work, reset to 0.
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

while True:
repeat()

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.



An update to show how to do it in the recent versions of OpenCV:


import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")

It works in OpenCV-2.4.2 for me.



If you only have one camera, or you don't care which camera is the correct one, then use "-1" as the index. Ie for your example capture = cv.CaptureFromCAM(-1).



This didn't work for me, but the following Gist was "copy/paste play" https://gist.github.com/2130079



change import cv to import cv2.cv as cv See also the post here.



Try this, simple, but haven't figured out a graceful way to exit yet.


import cv2.cv as cv import time


cv.NamedWindow("camera", 0)

capture = cv.CaptureFromCAM(0)

while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyAllWindows()

0 commentaires:

Enregistrer un commentaire