Skip to content

Allow fps() to dynamically get FPS value without having to call stop() #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 68 additions & 29 deletions imutils/video/fps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,71 @@
import datetime

class FPS:
def __init__(self):
# store the start time, end time, and total number of frames
# that were examined between the start and end intervals
self._start = None
self._end = None
self._numFrames = 0

def start(self):
# start the timer
self._start = datetime.datetime.now()
return self

def stop(self):
# stop the timer
self._end = datetime.datetime.now()

def update(self):
# increment the total number of frames examined during the
# start and end intervals
self._numFrames += 1

def elapsed(self):
# return the total number of seconds between the start and
# end interval
return (self._end - self._start).total_seconds()

def fps(self):
# compute the (approximate) frames per second
return self._numFrames / self.elapsed()
def __init__(self):
"""
store the start time, end time, and total number of frames
that were examined between the start and end intervals
"""
self._start = None
self._end = None
self._numFrames = 0
self._pause = False

def start(self):
"""start the timer"""
self._start = datetime.datetime.now()
return self

def stop(self):
"""stop the timer"""
self._end = datetime.datetime.now()
self._pause = True

def unpause(self, if_not_paused:str = None):
"""
Unpause the stopped timer.

Parameters
----------
if_not_paused : str, {None, 'restart', 'raise'} default = None
This parameter controls behaviour when the timer has not been
paused:
``None``:
Do nothing.
``'restart'``:
Restart both the timer and the frame counter from zero.
``'raise'``:
Raise an error.
"""
if self._pause:
self._start = datetime.datetime.now() - (self._end - self._start)
self._end = None
self._pause = False
elif if_not_paused == 'restart':
self._end = None
self._numFrames = 0
return self.start()
elif if_not_paused == 'raise':
raise ValueError('Timer has not been paused.')

def update(self):
"""
increment the total number of frames examined during the
start and end intervals
"""
if not self._pause:
self._numFrames += 1

def elapsed(self):
"""
return the total number of seconds between the start and
end interval
"""
if self._end is None:
return (datetime.datetime.now() - self._start).total_seconds()
else:
return (self._end - self._start).total_seconds()

def fps(self):
"""compute the (approximate) frames per second"""
return self._numFrames / self.elapsed()