bobo作业8

作业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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值