首页 > 解决方案 > 处理视频时如何使用 matplotlib 随时间绘制每秒帧数 (fps)?

问题描述

下面的代码计算视频每秒处理的帧数,并使用 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
 #----------------------------------------------------------------------------

但是,为了更好地说明,我尝试使用matplotlib随着时间的推移绘制 fps ,以便绘图如下所示:

plot_example_fps

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

提前致谢

标签: pythonnumpyopencvmatplotlibimage-processing

解决方案


由于您可以在处理结束后显示绘图,因此完成此操作的最佳方法是定义两个列表,其中一个计算经过的时间,另一个存储每个时间点的 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。


推荐阅读