From 0689a4227769f69819415f93845b4bde112d40a7 Mon Sep 17 00:00:00 2001 From: Pawel Kranzberg Date: Wed, 17 Feb 2021 12:22:24 +0100 Subject: [PATCH 1/3] Yet another attempt to allow fps() to dynamically get FPS value without having to call stop() --- imutils/video/fps.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/imutils/video/fps.py b/imutils/video/fps.py index cad5b89..3cf5e49 100644 --- a/imutils/video/fps.py +++ b/imutils/video/fps.py @@ -26,8 +26,11 @@ def update(self): def elapsed(self): # return the total number of seconds between the start and # end interval - return (self._end - self._start).total_seconds() + 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() \ No newline at end of file + return self._numFrames / self.elapsed() From d3ee3a96dd5dab3f7a51ae5f87f20a7493f79694 Mon Sep 17 00:00:00 2001 From: Pawel Kranzberg Date: Fri, 26 Feb 2021 12:24:25 +0100 Subject: [PATCH 2/3] Add `unpause()` function. --- imutils/video/fps.py | 68 ++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/imutils/video/fps.py b/imutils/video/fps.py index 3cf5e49..0a9bfb3 100644 --- a/imutils/video/fps.py +++ b/imutils/video/fps.py @@ -2,35 +2,47 @@ 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 __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 start(self): + # start the timer + self._start = datetime.datetime.now() + return self - def stop(self): - # stop the timer - self._end = datetime.datetime.now() + def stop(self): + # stop the timer + self._end = datetime.datetime.now() + self._pause = True - def update(self): - # increment the total number of frames examined during the - # start and end intervals - self._numFrames += 1 + def unpause(self, if_not_paused:str = None): + """ + Unpause the stopped timer. - 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() + 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.') From 9d49a320161afdbee384bf24764fdeb2eb9f0bcb Mon Sep 17 00:00:00 2001 From: Pawel Kranzberg Date: Fri, 26 Feb 2021 12:31:56 +0100 Subject: [PATCH 3/3] Update fps.py --- imutils/video/fps.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/imutils/video/fps.py b/imutils/video/fps.py index 0a9bfb3..8085bfd 100644 --- a/imutils/video/fps.py +++ b/imutils/video/fps.py @@ -3,20 +3,22 @@ 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 + """ + 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 + """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._pause = True @@ -46,3 +48,25 @@ def unpause(self, if_not_paused:str = None): 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()