fps - 如何通过时间函数划分计数来确定fps

S p*_*ce 5 python time opencv operations frame-rate

我有一个计数器工作,计算每一帧.我想要做的是将时间除以确定我的程序的FPS.但我不确定如何在python中对定时函数执行操作.

我已经尝试将时间初始化为

fps_time = time.time 
fps_time = float(time.time)
fps_time = np.float(time.time)
fps_time = time()
Run Code Online (Sandbox Code Playgroud)

然后计算fps,

FPS = (counter / fps_time)
FPS = float(counter / fps_time)
FPS = float(counter (fps_time))
Run Code Online (Sandbox Code Playgroud)

但是我得到的错误是对象是不可调用的或不支持的操作数/:'int'和'buildin functions'

在此先感谢您的帮助!

Elo*_*ine 16

  • 这是一种在每帧打印程序帧速率的简单方法(无需计数器):

    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
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果你想要平均帧速率超过x秒,你可以这样做(需要计数器):

    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()
    
    Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


Men*_*nua 6

奇迹般有效

import time
import collections

class FPS:
    def __init__(self,avarageof=50):
        self.frametimestamps = collections.deque(maxlen=avarageof)
    def __call__(self):
        self.frametimestamps.append(time.time())
        if(len(self.frametimestamps) > 1):
            return len(self.frametimestamps)/(self.frametimestamps[-1]-self.frametimestamps[0])
        else:
            return 0.0
fps = FPS()
for i in range(100):
    time.sleep(0.1)
    print(fps())
Run Code Online (Sandbox Code Playgroud)

确保每帧调用一次 fps