从网络摄像头获取最新帧 [英] Get most recent frame from webcam

查看:68
本文介绍了从网络摄像头获取最新帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV2通过网络摄像头拍摄一些定时拍摄的照片.我想提取网络摄像头看到的最新视图.我试图做到这一点.

I am using OpenCV2 to take some timelapse photos with a webcam. I want to extract the most recent view seen by the webcam. I try to accomplish this like so.

import cv2
a = cv2.VideoCapture(1)
ret, frame = a.read()
#The following garbage just shows the image and waits for a key press
#Put something in front of the webcam and then press a key
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
#Since something was placed in front of the webcam we naively expect
#to see it when we read in the next image. We would be wrong.
ret, frame = a.read()
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]

除了未显示在网络摄像头前面的图像外.几乎好像有某种缓冲...

Except that the image placed in front of the webcam does not show. It's almost as if there's some kind of buffer...

所以我清除该缓冲区,就像这样:

So I purge that buffer, like so:

import cv2
a = cv2.VideoCapture(1)
ret, frame = a.read()
#Place something in front of the webcam and then press a key
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]

#Purge the buffer
for i in range(10): #Annoyingly arbitrary constant
  a.grab()

#Get the next frame. Joy!
ret, frame = a.read()
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]

现在这行得通,但是令人讨厌的不科学且缓慢.有没有一种方法专门针对缓冲区中的最新图像进行询问?或者,除此以外,清除缓冲区的更好方法?

Now this works, but it's annoyingly unscientific and slow. Is there a way to ask specifically for only the most recent image in the buffer? Or, baring that, a better way to purge the buffer?

推荐答案

我已经阅读到VideoCapture对象中有一个5帧缓冲区,并且有.grab方法采用该帧但不对其进行解码.

I have read that in the VideoCapture object there is a 5 frame buffer, and there is the .grab method that takes the frame but does not decode it.

所以你可以

cap = cv2.VideoCapture(0)
for i in xrange(4):
    cap.grab()
ret, frame = cap.read()
...

这篇关于从网络摄像头获取最新帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆