Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using webcam in python + openCV

I am using the following code to access my webcam using openCV + python...

import cv

cv.NamedWindow('webcam_feed', cv.CV_WINDOW_AUTOSIZE)

cam = cv.CaptureFromCAM(-1)

I am then getting the following error in the console...

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument

I was originally using,

cv.CaptureFromCAM(0)

to access the same and had the same issue and used -1 because it is suppose to pick up any webcam.

I also tested to see if Ubuntu recognizes the webcam and it does. I am using v4l2 for the webcam.

{EDIT}

I am using the following code to display a video feed, it seems to only be showing just one image the web cam captured instead of a continuous video feed...

import cv
cv.NamedWindow('webcam_feed', cv.CV_WINDOW_AUTOSIZE)
cam = cv.CaptureFromCAM(-1)
feed = cv.QueryFrame(cam)
cv.ShowImage("webcam_feed", feed)
cv.WaitKey(-1)
like image 306
Collin McGuire Avatar asked Nov 13 '22 02:11

Collin McGuire


1 Answers

WOW, answered my own question in 15 after me posting this. I did some research and the reason for the web cam only grabbing one image is because of the...

cv.WaitKey(-1)

This doesn't allow the contents of the window to refresh. I set the number to 10...

cv.WaitKey(10)

and it worked beautifully. I also tried 100, but saw no difference. I only saw a difference when the number was 1000. I use 1 because seems that it runs the smoothest.

Here is the full code to display a web cam feed

import cv

cv.NamedWindow("webcam", 1)

cam = cv.CaptureFromCAM(-1)

While True:
feed = cv.QueryFrame(cam)
cv.ShowImage("webcam", feed)
cv.WaitKey(1)
like image 196
Collin McGuire Avatar answered Dec 23 '22 17:12

Collin McGuire