最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python Timer和TimerFPS计时工具类代码示例
时间:2022-06-25 01:20:18 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python Timer和TimerFPS计时工具类代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
以下工具类代码来自开源项目pyslam
Timer
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='33[0m' bold='33[01m' disable='33[02m' underline='33[04m' reverse='33[07m' strikethrough='33[09m' invisible='33[08m' class fg: black='33[30m' red='33[31m' green='33[32m' orange='33[33m' blue='33[34m' purple='33[35m' cyan='33[36m' lightgrey='33[37m' darkgrey='33[90m' lightred='33[91m' lightgreen='33[92m' yellow='33[93m' lightblue='33[94m' pink='33[95m' lightcyan='33[96m' class bg: black='33[40m' red='33[41m' green='33[42m' orange='33[43m' blue='33[44m' purple='33[45m' cyan='33[46m' lightgrey='33[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_, 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_, 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()
效果
相关文章
- 《潜行者2:切尔诺贝利之心》游戏车辆使用推荐 11-22
- 《潜行者2:切尔诺贝利之心》挡路的特异点处理方法介绍 11-22
- 《潜行者2:切尔诺贝利之心》开局获得满强AK方法介绍 11-22
- 《潜行者2:切尔诺贝利之心》军用防弹背心获得方法介绍 11-22
- 《潜行者2:切尔诺贝利之心》防毒面具获得方法介绍 11-22
- 《潜行者2:切尔诺贝利之心》开局捡尸体摸尸注意事项分享 11-22