-
Notifications
You must be signed in to change notification settings - Fork 9
/
FPS.py
44 lines (36 loc) · 1.26 KB
/
FPS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
@author: geaxx
"""
import time
import cv2
from collections import deque
def now():
return time.perf_counter()
class FPS: # To measure the number of frame per second
def __init__(self, average_of=30):
self.timestamps = deque(maxlen=average_of)
self.nbf = -1
def update(self):
self.timestamps.append(time.monotonic())
if len(self.timestamps) == 1:
self.start = self.timestamps[0]
self.fps = 0
else:
self.fps = (len(self.timestamps)-1)/(self.timestamps[-1]-self.timestamps[0])
self.nbf+=1
def get(self):
return self.fps
def get_global(self):
return self.nbf/(self.timestamps[-1] - self.start)
def nb_frames(self):
return self.nbf+1
def draw(self, win, orig=(10,30), font=cv2.FONT_HERSHEY_SIMPLEX, size=2, color=(0,255,0), thickness=2):
cv2.putText(win,f"FPS={self.get():.2f}",orig,font,size,color,thickness)
if __name__ == "__main__":
fps = FPS()
for i in range(50):
fps.update()
print(f"fps = {fps.get()}")
time.sleep(0.1)
global_fps, nb_frames = fps.get_global()
print(f"Global fps : {global_fps} ({nb_frames} frames)")