【发布时间】: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
#----------------------------------------------------------------------------
但是,为了更好地说明,我尝试使用 matplotlib 随时间绘制 fps,以便绘图如下所示:
所以谁能告诉我如何使用例如 matplotlib 绘制上面代码中显示的数据,因为它似乎并不容易,因为帧数会随着时间的推移而变化。
提前致谢
【问题讨论】:
-
您希望情节实时更新和显示,还是仅在您完成整个视频处理后显示?
-
感谢您的回复。因为我需要这个用于文档的目的,所以在视频处理完成后显示绘图就足够了。但是,两者都适用于我@rayryengspan>
标签: python numpy opencv matplotlib image-processing