Hi!

I'm trying to use the opencv bindings of Python.

I tried the following code:

from opencv.highgui import *
from opencv.cv import *
cam = 'cam'
cvNamedWindow(cam)
cap = cvCreateCameraCapture(0)
while cvWaitKey(1) != 27:
  img=cvQueryFrame(cap)
  cvShowImage(cam,img)

Although the window seems to open fine, I cannot see any image. I just get a window without any image -> as if there is no image stored in variable 'img'

On checking the 'img' variable (using cvSaveImage('test.jpg',img)), I found that the img file was ok (the stoed file was correct).

Any idea what is causing the problem?

Hi,

I'm new at opencv and python and I've got the same problem too.

If your opened window is a black screen just try to copy your captured frame to newly created image then show it.

If the screen is gray then I don't know the solution cause mine is gray :)

Ok I solved the problem, at least it worked well for me : )

The problem is in your loop condition where you said

while cvWaitKey(1) != 27:

change it to :

from opencv.highgui import *
from opencv.cv import *

cam = 'cam'
cvNamedWindow(cam)
cap = cvCreateCameraCapture(0)

while 1:
  img=cvQueryFrame(cap)
  cvShowImage(cam,img)

Well, I don't know why it works like that . cvWaitKey doesn't work it has problems :/

Edited 6 Years Ago by mohoho: n/a

Ok I solved the problem, at least it worked well for me : )

The problem is in your loop condition where you said

while cvWaitKey(1) != 27:

change it to :

from opencv.highgui import *
from opencv.cv import *

cam = 'cam'
cvNamedWindow(cam)
cap = cvCreateCameraCapture(0)

while 1:
  img=cvQueryFrame(cap)
  cvShowImage(cam,img)

Well, I don't know why it works like that . cvWaitKey doesn't work it has problems :/

I tried the code you suggested above. In my case, nothing shows up. Although the camera is on (I believe it is capturing images), I do not get any window (cvNamedWindow).

In case of cvWaitKey, the problem is that the whole process is terribly slow. When I waited for around five or six minutes, I got around three or four frames captured.

Anyone can help on this?

Thanks a lot!

opencv need this packages "build-essential, libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev" do you have them?

The problem is, as far as I can work out (I am having the same issue)
is that

while(a != 27):
    a = highgui.cvWaitKey(1000)
    print type(a)

is telling me that the type of a if no key is pressed is <type 'long'> and if the key is pressed is <type 'str'>

ideally it is meant to be long, a problem because we can't compare strings with longs.

One method is to use ord() -- this returns the ascii value of the string, but first ignore the -1 default value

thus:

while ord(str(cvWaitKey(1))[0]) != 27:
  img=cvQueryFrame(cap)
  cvShowImage(cam,img)

you must use the [0] following the str() command, when you str(-1) it returns a string '-1' of length 2. This causes ord() to fail.

A horribly cluncky work-around IMHO.

P.S. unless you exlude a = -1 when converting to string, you cannot use this to capture '-'...

This article has been dead for over six months. Start a new discussion instead.