作业8:
显示视频的fps(求帧率)
参考链接:
http://www.it1352.com/467296.html
https://blog.csdn.net/w5688414/article/details/78426153(python:读取视频,处理后,实时计算帧数fps)
1、import time
2、时间太短会出现: float division by zero报错
3、把这些写在brake后:
end = time.time()
# Time elapsed
seconds = end - start
fps_video = 1 / seconds # 获取视频帧率
cv.putText(frame, '%f' % fps_video, (word_x, word_y), cv.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
cv.imshow('frame', frame) # 显示结果
videoWriter = cv.VideoWriter(result_video, fourcc, fps_video, (frame_width, frame_height))
答案:
import cv2 as cv
import numpy as np
#import random
import time
capture = cv.VideoCapture(0) # 创建一个VideoCapture对象
result_video = "result.mp4"
while(True):
# Start time
start = time.time()
ret, frame = capture.read() # 一帧一帧读取视频
#gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # 对每一帧做处理,设置为灰度图
# 设置写入视频的编码格式
fourcc = cv.VideoWriter_fourcc(*"mp4v")
# 获取视频宽度
frame_width = int(capture.get(cv.CAP_PROP_FRAME_WIDTH))
# 获取视频高度
frame_height = int(capture.get(cv.CAP_PROP_FRAME_HEIGHT))
#left_x_up = int(369)
#left_y_up = int(328)
#right_x_down = int(469)
#right_y_down = int(200)
# 文字坐标
word_x = 369
word_y = 328
#cv.rectangle(frame, (left_x_up, left_y_up), (right_x_down, right_y_down), (55, 255, 155), 5)
#x = random.randint(0, 9)
if cv.waitKey(1) &0xFF == ord('q'): # 按q停止
break
# End time
end = time.time()
# Time elapsed
seconds = end - start
fps_video = 1 / seconds # 获取视频帧率
cv.putText(frame, '%f' % fps_video, (word_x, word_y), cv.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
cv.imshow('frame', frame) # 显示结果
videoWriter = cv.VideoWriter(result_video, fourcc, fps_video, (frame_width, frame_height))
videoWriter.write(frame)