Skip to content

Commit 28b6df8

Browse files
committed
Add pause/resume functionality to FPS
* Does not break existing API. * Add `pause` and `resume` methods to FPS in order to pause and resume the timer in an accurate way. * Calls to `update` will not increment frame count while the timer is paused. * Also, convert old comments to proper docstrings.
1 parent 9f740a5 commit 28b6df8

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

imutils/video/fps.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
# import the necessary packages
22
import datetime
33

4+
45
class FPS:
56
def __init__(self):
6-
# store the start time, end time, and total number of frames
7-
# that were examined between the start and end intervals
7+
'''Create a new FPS timer.'''
8+
# Store the total elapsed time and the total number of frames
9+
# that were examined during that elapsed time.
10+
# Start time and end time apply only to a given segment.
11+
self._elapsed = datetime.timedelta()
812
self._start = None
913
self._end = None
1014
self._numFrames = 0
15+
self._is_paused = False
1116

1217
def start(self):
13-
# start the timer
18+
'''Start the timer.'''
1419
self._start = datetime.datetime.now()
1520
return self
1621

1722
def stop(self):
18-
# stop the timer
23+
'''Stop the timer.'''
1924
self._end = datetime.datetime.now()
25+
self._elapsed += (self._end - self._start)
26+
27+
def pause(self):
28+
'''Pause the timer.'''
29+
self.stop()
30+
self._is_paused = True
31+
32+
def resume(self):
33+
'''Resume the timer from a pause.'''
34+
self.start()
35+
self._is_paused = False
2036

2137
def update(self):
22-
# increment the total number of frames examined during the
23-
# start and end intervals
24-
self._numFrames += 1
38+
'''Increment the total number of frames examined during the
39+
timing intervals.'''
40+
# ignore this call while we're paused
41+
if not self._is_paused:
42+
self._numFrames += 1
2543

2644
def elapsed(self):
27-
# return the total number of seconds between the start and
28-
# end interval
29-
return (self._end - self._start).total_seconds()
45+
'''Return the total number of seconds during the
46+
timing intervals.'''
47+
return self._elapsed.total_seconds()
3048

3149
def fps(self):
32-
# compute the (approximate) frames per second
33-
return self._numFrames / self.elapsed()
50+
'''Return the (approximate) frames per second.'''
51+
return self._numFrames / self.elapsed()

0 commit comments

Comments
 (0)