Skip to content

Commit

Permalink
Merge pull request #280 from hardikdava/feature/fps
Browse files Browse the repository at this point in the history
Feature FPS meaure
  • Loading branch information
onuralpszr committed Oct 6, 2023
2 parents 89c1b63 + b482784 commit d5d5725
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/utils/fps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## list_files_with_extensions

:::supervision.utils.fps.FpsMonitor
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ nav:
- Image: utils/image.md
- Notebook: utils/notebook.md
- File: utils/file.md
- FPS: utils/fps.md
- Changelog: changelog.md

theme:
Expand Down
1 change: 1 addition & 0 deletions supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision
from supervision.tracker.byte_tracker.core import ByteTrack
from supervision.utils.file import list_files_with_extensions
from supervision.utils.fps import FpsMonitor
from supervision.utils.image import ImageSink, crop_image
from supervision.utils.notebook import plot_image, plot_images_grid
from supervision.utils.video import (
Expand Down
34 changes: 34 additions & 0 deletions supervision/utils/fps.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d5d5725

Please sign in to comment.