forked from luigifreda/pyslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
82 lines (71 loc) · 2.77 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
* This file is part of PYSLAM
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
"""
import cv2
from moving_average import MovingAverage
from utils_sys import Printer
#timer_print = print
timer_print = Printer.cyan
class Timer:
def __init__(self, name = '', is_verbose = False):
self._name = name
self._is_verbose = is_verbose
self._is_paused = False
self._start_time = None
self._accumulated = 0
self._elapsed = 0
self.start()
def start(self):
self._accumulated = 0
self._start_time = cv2.getTickCount()
def pause(self):
now_time = cv2.getTickCount()
self._accumulated += (now_time - self._start_time)/cv2.getTickFrequency()
self._is_paused = True
def resume(self):
if self._is_paused: # considered only if paused
self._start_time = cv2.getTickCount()
self._is_paused = False
def elapsed(self):
if self._is_paused:
self._elapsed = self._accumulated
else:
now = cv2.getTickCount()
self._elapsed = self._accumulated + (now - self._start_time)/cv2.getTickFrequency()
if self._is_verbose is True:
name = self._name
if self._is_paused:
name += ' [paused]'
message = 'Timer::' + name + ' - elapsed: ' + str(self._elapsed)
timer_print(message)
return self._elapsed
class TimerFps(Timer):
def __init__(self, name='', average_width = 10, is_verbose = True):
super().__init__(name, is_verbose)
self.moving_average = MovingAverage(average_width)
def refresh(self):
elapsed = self.elapsed()
self.moving_average.getAverage(elapsed)
self.start()
if self._is_verbose is True:
dT = self.moving_average.getAverage()
name = self._name
if self._is_paused:
name += ' [paused]'
message = 'Timer::' + name + ' - fps: ' + str(1./dT) + ', T: ' + str(dT)
timer_print(message)