Search code examples
pythonopencvtimeframe-rateoperations

fps - how to divide count by time function to determine fps


I have a counter working that counts every frame. what I want to do is divide this by time to determine the FPS of my program. But I'm not sure how to perform operations on timing functions within python.

I've tried initializing time as

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

Then for calculating the fps,

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

But errors I'm getting are object is not callable or unsupported operand for /: 'int' and 'buildin functions'

thanks in advance for the help!


Solution

    • Here is a very simple way to print your program's frame rate at each frame (no counter needed) :

      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
      
    • If you want the average frame rate over x seconds, you can do like so (counter needed) :

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

    Hope it helps!