【问题标题】:How to plot frame per seconds (fps) over time using matplotlib when processing a video?处理视频时如何使用matplotlib随时间绘制每秒帧数(fps)?
【发布时间】:2020-12-01 11:28:54
【问题描述】:

以下代码计算视频每秒处理的帧数,并使用 opencv 将其显示在输出处理帧上:

import cv2 as cv 
import numpy as np 
from collections import  deque

from imutils.video import FPS
# matplotlib.pyplot as plt
import datetime
from class22 import pre_processing

pts1 = deque(maxlen=40)
pts2 = deque(maxlen=40)

#ceate a capture object-------------------------------------------------------------------

cap=cv.VideoCapture(r'C:/Users/kjbaili/Documents/Masterarbeit/Praxis/Erste
_Videoeinsatz/Basler_acA 1920-40uc__22823716__20200724_145423423.mp4')

fps_start_time = datetime.datetime.now() #start timer
FPs= 0
total_frames = 0
#-------------------------------------------------------------------
ob1=pre_processing()
ob2=pre_processing()

fps = FPS().start() 

#start reading frames
while cap.isOpened:
 ret,frame=cap.read(())
 
 #----------------------------------------------------------------------------
 total_frames = total_frames + 1  #collect the total number of frames
 fps_end_time = datetime.datetime.now() #stop timer
 time_diff = fps_end_time - fps_start_time
 if time_diff.seconds == 0: 
     FBs = 0.0
 else:
  FBs = (total_frames / time_diff.seconds) #estimate the frame per second
 fps_text = "FPS: {:.2f}".format(FBs)
 cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
 cv.putText(frame, fps_text, (15, 15),cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))# display the fps 
 on the output image
 #----------------------------------------------------------------------------

但是,为了更好地说明,我尝试使用 ma​​tplotlib 随时间绘制 fps,以便绘图如下所示:

所以谁能告诉我如何使用例如 matplotlib 绘制上面代码中显示的数据,因为它似乎并不容易,因为帧数会随着时间的推移而变化。

提前致谢

【问题讨论】:

  • 您希望情节实时更新和显示,还是仅在您完成整个视频处理后显示?
  • 感谢您的回复。因为我需要这个用于文档的目的,所以在视频处理完成后显示绘图就足够了。但是,两者都适用于我@rayryeng​​span>

标签: python numpy opencv matplotlib image-processing


【解决方案1】:

由于您可以在处理结束后显示绘图,因此完成此操作的最佳方法是定义两个列表,其中一个计算经过的时间,另一个存储每个时间点的 FPS。这些会在你的帧循环中更新,然后我们可以使用 matplotlib 来显示信息:

## New - lists for storing our information
time_list = []
fps_list = []


#start reading frames
while cap.isOpened:
    ret,frame=cap.read(())

    #----------------------------------------------------------------------------
    total_frames = total_frames + 1  #collect the total number of frames
    fps_end_time = datetime.datetime.now() #stop timer
    time_diff = fps_end_time - fps_start_time
    if time_diff.seconds == 0: 
       FBs = 0.0
    else:
       FBs = (total_frames / time_diff.seconds) #estimate the frame per second
    fps_text = "FPS: {:.2f}".format(FBs)
    cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
    cv.putText(frame, fps_text, (15, 15),cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))# display the fps 
 on the output image
    #----------------------------------------------------------------------------

    ## New - Update our lists
    time_list.append(time_diff.seconds)
    fps_list.append(FBs)

import matplotlib.pyplot as plt
plt.figure()
plt.plot(time_list, fps_list)
plt.xlabel('Time (s)')
plt.ylabel('FPS')
plt.show()

我使用了您在最后提供的代码来进行实际的帧处理。但是,我也宣布了上述列表。一旦我们计算出经过的时间,我们将其附加到列表和相应的估计 FPS 中。处理循环完成后,我们使用 matplotlib 显示一个绘图,横轴为时间,纵轴为 FPS。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2020-04-05
    • 2020-01-05
    • 2019-01-11
    相关资源
    最近更新 更多