Skip to content
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

Add pause/resume functionality to FPS (closes #227) #228

Open
wants to merge 1 commit 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
42 changes: 30 additions & 12 deletions imutils/video/fps.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
# import the necessary packages
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
'''Create a new FPS timer.'''
# Store the total elapsed time and the total number of frames
# that were examined during that elapsed time.
# Start time and end time apply only to a given segment.
self._elapsed = datetime.timedelta()
self._start = None
self._end = None
self._numFrames = 0
self._is_paused = False

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

def stop(self):
# stop the timer
'''Stop the timer.'''
self._end = datetime.datetime.now()
self._elapsed += (self._end - self._start)

def pause(self):
'''Pause the timer.'''
self.stop()
self._is_paused = True

def resume(self):
'''Resume the timer from a pause.'''
self.start()
self._is_paused = False

def update(self):
# increment the total number of frames examined during the
# start and end intervals
self._numFrames += 1
'''Increment the total number of frames examined during the
timing intervals.'''
# ignore this call while we're paused
if not self._is_paused:
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()
'''Return the total number of seconds during the
timing intervals.'''
return self._elapsed.total_seconds()

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