python3.5 opencv3显示视频fps

本文介绍了如何使用Python和OpenCV库来监测视频处理过程中的帧率(FPS),提供了三种不同的方法来获取和显示FPS,包括实时打印FPS值、每秒更新一次FPS值以及将FPS直接显示在视频画面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于要进行多路视频的处理,所以fps就很重要

  fps介绍

模板:

  1.获取某一时刻的fps

 

import time

while True:
    start_time = time.time() # start time of the loop

    ########################
    # your fancy code here #
    ########################

    print("FPS: ", 1.0 / (time.time() - start_time)) # FPS = 1 / time to process loop

 

   2.每一秒获取一次

 

import time

start_time = time.time()
x = 1 # displays the frame rate every 1 second
counter = 0
while True:

    ########################
    # your fancy code here #
    ########################

    counter+=1
    if (time.time() - start_time) > x :
        print("FPS: ", counter / (time.time() - start_time))
        counter = 0
        start_time = time.time()

 3.实例

  (1)读取目录中的某一个视频

 

import time
import cv2
cap = cv2.VideoCapture("../video/basketball1.mp4")
start_time = time.time()
x = 1 # displays the frame rate every 1 second
counter = 0
while True:

    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    counter += 1
    if (time.time() - start_time) > x:
        print("FPS: ", counter / (time.time() - start_time))
        counter = 0
        start_time = time.time()
cap.release()
cv2.destroyAllWindows()

 

   运行效果

 

  其实把fps显示到窗口上更人性化一点,于是我把原来的putTest()方法和这个结合了一下,中间format里面转化了一下类型

 

import time
import cv2
cap = cv2.VideoCapture("../video/basketball1.mp4")
start_time = time.time()
x = 1 # displays the frame rate every 1 second
counter = 0
while True:

    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    counter += 1#记录经过多少秒
    if (time.time() - start_time) > x:
        cv2.putText(gray, "FPS {0}" .format(str(counter / (time.time() - start_time))), (10, 230), 6, 2, (255, 0, 255), 3)
        #cv2.putText(gray, "FPS %s" % str(counter / (time.time() - start_time)), (10, 130), 6, 5, (255, 0, 255), 5)
        #cv2.putText(gray, "Hello World!", (400, 50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
        cv2.imshow('frame', gray)
        print("FPS: ", counter / (time.time() - start_time))
        #print(type(str(counter / (time.time() - start_time))))
        #print(type(counter / (time.time() - start_time)))
        counter = 0
        start_time = time.time()
cap.release()
cv2.destroyAllWindows()

 

 运行结果,fps数据每一秒都会刷新一下

 

转载于:https://www.cnblogs.com/MC-Curry/p/8681042.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值