在 Python 中使用 OpenCV VideoCapture 获取当前帧 [英] Getting current frame with OpenCV VideoCapture in Python

查看:81
本文介绍了在 Python 中使用 OpenCV VideoCapture 获取当前帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cv2.VideoCapture 在 python 脚本中读取 RTSP 视频链接的帧..read() 函数处于 while 循环中,每秒运行一次,但是,我没有从流中获取最新的帧.我得到了较旧的帧,这样我的滞后就会增加.无论如何,我可以获得最新的帧而不是已通过管道传输到 VideoCapture 对象的旧帧吗?

I am using cv2.VideoCapture to read the frames of an RTSP video link in a python script. The .read() function is in a while loop which runs once every second, However, I do not get the most current frame from the stream. I get older frames and in this way my lag builds up. Is there anyway that I can get the most current frame and not older frames which have piped into the VideoCapture object?

推荐答案

我也遇到了同样的问题,找到了Azure 示例 用于他们的计算机视觉服务.相关部分是 Camera Capture 模块,特别是 视频流类.

I've encountered the same problem and found a git repository of Azure samples for their computer vision service. The relevant part is the Camera Capture module, specifically the Video Stream class.

您可以看到他们实现了一个正在更新的队列以仅保留最新的帧:

You can see they've implemented a Queue that is being updated to keep only the latest frame:

def update(self):
    try:
        while True:
            if self.stopped:
                return

            if not self.Q.full():
                (grabbed, frame) = self.stream.read()

                # if the `grabbed` boolean is `False`, then we have
                # reached the end of the video file
                if not grabbed:
                    self.stop()
                    return

                self.Q.put(frame)

                # Clean the queue to keep only the latest frame
                while self.Q.qsize() > 1:
                    self.Q.get()

这篇关于在 Python 中使用 OpenCV VideoCapture 获取当前帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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