Timer
用法
效果
TimerFps
用法
效果
以下工具类代码来自开源项目pyslam
。
import cv2
class Colors(object):
'''
Colors class:reset all colors with colors.reset; two
sub classes fg for foreground
and bg for background; use as colors.subclass.colorname.
i.e. colors.fg.red or colors.bg.greenalso, the generic bold, disable,
underline, reverse, strike through,
and invisible work with the main class i.e. colors.bold
'''
reset='\033[0m'
bold='\033[01m'
disable='\033[02m'
underline='\033[04m'
reverse='\033[07m'
strikethrough='\033[09m'
invisible='\033[08m'
class fg:
black='\033[30m'
red='\033[31m'
green='\033[32m'
orange='\033[33m'
blue='\033[34m'
purple='\033[35m'
cyan='\033[36m'
lightgrey='\033[37m'
darkgrey='\033[90m'
lightred='\033[91m'
lightgreen='\033[92m'
yellow='\033[93m'
lightblue='\033[94m'
pink='\033[95m'
lightcyan='\033[96m'
class bg:
black='\033[40m'
red='\033[41m'
green='\033[42m'
orange='\033[43m'
blue='\033[44m'
purple='\033[45m'
cyan='\033[46m'
lightgrey='\033[47m'
class Printer(object):
@staticmethod
def red(*args, **kwargs):
print(Colors.fg.red, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def green(*args, **kwargs):
print(Colors.fg.green, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def blue(*args, **kwargs):
print(Colors.fg.blue, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def cyan(*args, **kwargs):
print(Colors.fg.cyan, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def orange(*args, **kwargs):
print(Colors.fg.orange, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def purple(*args, **kwargs):
print(Colors.fg.purple, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def yellow(*args, **kwargs):
print(Colors.fg.yellow, *args, **kwargs)
print(Colors.reset, end="")
@staticmethod
def error(*args, **kwargs):
print(Colors.fg.red, *args, **kwargs, file=sys.stderr)
print(Colors.reset, end="")
#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
用法
import Timer
timer = Timer(is_verbose=True)
timer.start()
# 此处是你的代码
timer.elapsed()
效果
TimerFps
在上面的基础上,再加上下面代码段:
import os
import numpy as np
import cv2
import math
class MovingAverage:
def __init__(self, average_width = 10, compute_sigma = False):
self._average_width = average_width
self._idx_ring = 0
self._average = 0
self._sigma2 = 0
self._is_init = False
self._is_compute_sigma = compute_sigma
self._one_over_average_width_min_one = 1./(average_width-1)
self._ring_buffer = np.zeros(average_width)
def init(self, initVal=None):
if initVal is None:
initVal = 0.
self._ring_buffer = np.full(self._average_width, initVal, dtype=np.float32)
self._average = initVal;
self._sigma2 = 0;
self._is_init = True;
def getAverage(self, new_val=None):
if not self._is_init:
self.init(new_val)
if new_val is None:
return self._average
averageOld = self._average
oldVal = self._ring_buffer[self._idx_ring]
self._average += (new_val - oldVal)/self._average_width
if self._is_compute_sigma:
self._sigma2 = self._sigma2 + self._one_over_average_width_min_one*(self._average_width*(averageOld*averageOld - self._average*self._average) - oldVal*oldVal + newVal*newVal)
self._ring_buffer[self._idx_ring] = new_val
self._idx_ring = (self._idx_ring + 1) % self._average_width
return self._average
def getSigma(self):
return math.sqrt(max(self._sigma2,0.))
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)
用法
import TimerFps
timer = TimerFps(name='')
timer.start()
# 这里是你的代码
timer.refresh()
效果
到此这篇关于Python Timer和TimerFPS计时工具类的文章就介绍到这了,更多相关Python 计时工具类内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!