8

I want to get the video stream from my webcam using python and OpenCV, for that task i've implemented this simple code:

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()

when i try to execute it, i get the following error:

andfoy@ubuntu:~/Python$ python camera.py
VIDIOC_QUERYMENU: Argumento inválido   
VIDIOC_QUERYMENU: Argumento inválido
VIDIOC_QUERYMENU: Argumento inválido

I changed the following line as suggested by other posts:

capture = cv.CaptureFromCAM(0)

to:

capture = cv.CaptureFromCAM(-1) 

but the error persists.

1
  • WaitKey is an important part of OpenCV. many people feel they don't need to wait for a keystroke and omit it, but the GUI won't run without it and your window will never show up. Commented Jul 15, 2014 at 1:32

2 Answers 2

22

You need to add waitkey function at end.

Below piece of code works fine for me.

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()
    if cv.WaitKey(33)==27:
        break

cv.DestroyAllWindows()

And if you are not aware, Present OpenCV uses new python api cv2 and it has lots of features. In that, same code is written as :

import cv2
import numpy as np
c = cv2.VideoCapture(0)

while(1):
    _,f = c.read()
    cv2.imshow('e2',f)
    if cv2.waitKey(5)==27:
        break
cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

3 Comments

This code does not work for me. I am using python 2.7. I still get the error with the Invalid argument.
This doesn't appear to work for me either, maybe opencv has changed, maybe python. dir(cv) shows the method is namedWindow() not NamedWindow(), and VideoCapture doesn't seem to be a method anywhere.
Which version do you use?
1

Below code works for python 2.7 and opencv that has build for python 2.7

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

if not(cap.isOpened()):
    cap.open()

while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.