开发者

How to capture a frame from a camera stream exactly when a key is hit using opencv in python?

I am trying to capture image from a webcam when a key is hit. Following code is successful

    import cv

    cv.NamedWindow("w1")
    camera = cv.CaptureFromCAM(-1)

    while True:

        key = cv.WaitKey(0);

        if key == 'q': 
            break;
        image = cv.QueryFrame(camera)
        cv.ShowImage("w1", image)


    cv开发者_开发技巧.DestroyWindow("w1")

It works fine for the first keypress. For the next keydown it shows a frame very close to the first one even if you moved. After several key presses it changes to the actual image. What i can infer is that, there is some kind of buffer where frames are stored I am wondering if someone could please help me in getting the precise frame when a key is being hit.

I am using opencv with interface to python. The operating system is ubuntu 11.04. The calls for capture frame are sent to v4l library. I have an integrated webcam with my dell laptop.

I am wondering if someone can help me with this issue.

Thanks a lot


I suggest you try it in a slightly different manner:

import cv

cv.NamedWindow("w1")
camera = cv.CaptureFromCAM(-1)

while True:
    image = cv.QueryFrame(camera)
    key = cv.WaitKey(33)

    if key == 'q':
        break
    elif key != -1:
        cv.ShowImage("w1", image)

cv.DestroyWindow("w1")

Notice the change in the call to cv.WaitKey(): instead of blocking on it, just wait for a reasonable time. Then check if a key was actually pressed (key != -1).

I should note that your code worked on my Mac OS X 10.6 with OpenCV 2.3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜