diff --git a/docs/utils/fps.md b/docs/utils/fps.md new file mode 100644 index 00000000..4732ce62 --- /dev/null +++ b/docs/utils/fps.md @@ -0,0 +1,3 @@ +## list_files_with_extensions + +:::supervision.utils.fps.FpsMonitor diff --git a/mkdocs.yml b/mkdocs.yml index dd41adc9..94a36abb 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -48,6 +48,7 @@ nav: - Image: utils/image.md - Notebook: utils/notebook.md - File: utils/file.md + - FPS: utils/fps.md - Changelog: changelog.md theme: diff --git a/supervision/utils/fps.py b/supervision/utils/fps.py new file mode 100644 index 00000000..648367bb --- /dev/null +++ b/supervision/utils/fps.py @@ -0,0 +1,34 @@ +import time +from collections import deque + + +class FpsMonitor: + def __init__(self, sample_size: int = 30): + """ + Initialize class to measure Frame per second for latency benchmark. + Args: + sample_size (int): Maximum observation to measure latency benchmark + Examples: + ```python + >>> import supervision as sv + + >>> fps_monitor = sv.FpsMonitor() + >>> while True: + ... (...) + ... fps_monitor.tick() + ... fps = fps_monitor() + ``` + """ + self.all_timestamps = deque(maxlen=sample_size) + + def __call__(self) -> float: + if not self.all_timestamps: + return 0.0 + taken_time = self.all_timestamps[-1] - self.all_timestamps[0] + return (len(self.all_timestamps)) / taken_time if taken_time != 0 else 0.0 + + def tick(self) -> None: + self.all_timestamps.append(time.monotonic()) + + def reset(self) -> None: + self.all_timestamps.clear() \ No newline at end of file