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

时间:2017-05-03 13:08:02

标签: python opencv time frame-rate operations

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

我已尝试初始化时间

fps_time = time.time 
fps_time = float(time.time)
fps_time = np.float(time.time)
fps_time = time()

然后计算fps,

FPS = (counter / fps_time)
FPS = float(counter / fps_time)
FPS = float(counter (fps_time))

但是我得到的错误是对象不是/或' int'的可调用或不支持的操作数。和'建立功能'

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:14)

  • 这是在每个帧上打印程序的帧速率的一种非常简单的方法(不需要计数器):

    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
    
  • 如果你希望平均帧速率超过 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()
    

希望它有所帮助!

答案 1 :(得分:0)

你可能想要做这样的事情:

def program():
  start_time = time.time() #record start time of program
  frame_counter = 0

  # random logic 
  for i in range(0, 100):
    for j in range(0, 100):
      # do stuff that renders a new frame
      frame_counter += 1 # count frame

  end_time = time.time() #record end time of program
  fps = frame_counter / float(end_time - start_time)

当然,您不必等待程序结束时计算 end_time fps ,您可以不时地执行此操作以报告FPS为该程序运行。在报告当前FPS估计之后重新启动 start_time 也可以帮助报告更精确的FPS估计。

答案 2 :(得分:0)

像魅力一样工作

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())

确保每帧调用一次fps