【问题标题】:Processing an image with Open CV every n seconds每 n 秒使用 Open CV 处理一张图像
【发布时间】:2018-10-31 12:42:43
【问题描述】:

我正在使用 OpenCV 3.4 和 Python 3 编写程序。我还使用 Raspberry pi 3 和 PiCamera。 我的问题是我必须每 2 或 3 秒处理一次从 picamera 实时录制的视频的最后一帧。

import cv2
from time import sleep
cam=cv2.VideoCapture(0)
while True:
    suc, img=cam.read()
    #operation on image, it's not important
    cv2.imshow(...)

我有一个类似的代码。 这段代码工作得很好,但是树莓派继续处理视频的最后一帧,持续几秒钟。 我希望每 2 或 3 秒只处理 1 次。 我已经尝试在 while 循环中使用 time.sleep(2) 但它不起作用,因为那时视频不是实时的。我在互联网上搜索了很多,我认为 picamera 继续记录,当 2 秒过去后,我的程序处理之后的帧,而不是相机记录的最后一帧。 在互联网上,我找到了一个名为 VideoCapture.Grab 的函数,但我不明白它是如何工作的,以及是否必须使用它。 我发现这个问题与我的非常相似 How to capture a picture after every 5 seconds of camera using opencv python and Raspbery Pi 3? 但是代码的第二部分被剪切或类似,我无法理解。 谢谢大家。

【问题讨论】:

  • suc,img=cam.read() 行放在time.sleep(2) 行之后。
  • 发布的答案是否解决了您的问题?
  • 我认为这里的问题是 OpenCV 最多缓冲 5 帧,所以如果你没有阅读 2 秒,那么你之后阅读的前 4-5 帧将长达 2 秒.解决方案是不断调用grab() 以保持帧刷新。

标签: python-3.x opencv image-processing raspberry-pi


【解决方案1】:

这里有一个快速简单的解决方案:

在 python 中,time.time() 获取自 1970 年 1 月 1 日以来的当前时间(以秒为单位)。在您的 while 循环中,如果您只是获取当前时间并将其与您上次获取帧的时间进行比较,那么您可以检查如果 2 秒过去了,然后决定像你提到的那样“保留”你的框架。你可以试试下面的代码:

import cv2
import time
cam=cv2.VideoCapture(0)
last_recorded_time = time.time() # this keeps track of the last time a frame was processed
while True:
    curr_time = time.time() # grab the current time

    # keep these three statements outside of the if statement, so we 
    #     can still display the camera/video feed in real time
    suc, img=cam.read()
    #operation on image, it's not important
    cv2.imshow(...)

    if curr_time - last_recorded_time >= 2.0: # it has been at least 2 seconds
        # NOTE: ADD SOME STATEMENTS HERE TO PROCESS YOUR IMAGE VARIABLE, img

        # IMPORTANT CODE BELOW
        last_recorded_time = curr_time

请注意,我添加了 4 行代码并更改了 1 行(from time import sleepimport time)。我希望这有帮助!它根本不会暂停您的程序或减慢它的速度。

【讨论】:

  • 谢谢你的回答,但它不起作用;实际上现在程序停止了 2 秒就可以了,但是随后处理了一个旧帧而不是 picamera 的最后一帧......相机的实时视频和图像处理有很多延迟/间隙......我' m 寻找在程序停止时的 2 或 3 秒内跳过所有帧的东西
  • 没有任何 sleep 或 time.time() ecc ecc 程序处理实时视频,当我使用一些类似的功能时,实时视频和图像处理之间存在很大差距......我想要停止 2 秒,然后处理实时帧而不是旧帧
  • 好吧,我更新了帖子中的代码。我认为您可能指的是cv2.imshow(...) 仅在选定的帧上运行,而不是在每一帧上运行,因此它“出现”就好像它停止了 2 秒一样。基本上,只需将 cv2.imshow(...)suc, img=cam.read() 移到 if 语句之前。
【解决方案2】:

您可以使用time.time() 手动计算时间。

from time import time
import cv2

# Create a new VideoCapture object
cam = cv2.VideoCapture(0)

# Initialise variables to store current time difference as well as previous time call value
previous = time()
delta = 0

# Keep looping
while True:
    # Get the current time, increase delta and update the previous variable
    current = time()
    delta += current - previous
    previous = current

    # Check if 3 (or some other value) seconds passed
    if delta > 3:
        # Operations on image
        # Reset the time counter
        delta = 0

    # Show the image and keep streaming
    _, img = cam.read()
    cv2.imshow("Frame", img)
    cv2.waitKey(1)

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多