generated from roboflow/template-python
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from hardikdava/feature/fps
Feature FPS meaure
- Loading branch information
Showing
4 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## list_files_with_extensions | ||
|
||
:::supervision.utils.fps.FpsMonitor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |