python - 处理视频时如何使用 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 ,以便绘图如下所示:
因此,谁能告诉我如何使用例如 matplotlib 绘制上述代码中显示的数据,因为它似乎并不容易,因为帧数会随着时间的推移而变化。
提前致谢
解决方案
由于您可以在处理结束后显示绘图,因此完成此操作的最佳方法是定义两个列表,其中一个计算经过的时间,另一个存储每个时间点的 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。
推荐阅读
- google-bigquery - 尝试从 BigQuery 读取表并使用 Airflow 将其保存为数据框时出现 _pickle.PicklingError
- javascript - 尝试导航顶级窗口的框架使用“用户激活允许顶部导航”进行沙盒处理
- android - 使用expo在android设备中打开反应本机应用程序时出错
- python - 根据给定条件将新列添加到现有数据框列
- django - django设置默认时区
- reactjs - 已解决:我试图在反应中将更新数据从父级传递给子级,但它没有在子组件的功能中更新
- database - Mongoose:TypeError:模式路径`guildId.type`的值无效,得到值“未定义”
- javascript - 仅用于数据结构和数据库的前端(React)解决方案
- python - 如何转换宽数据框以绘制特定数据
- regex - 正则表达式在文本文件中突出显示高于 85% 红色的百分位数