網(wǎng)站首頁 編程語言 正文
以下工具類代碼來自開源項(xiàng)目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='\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
在上面的基礎(chǔ)上,再加上下面代碼段:
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()
效果
原文鏈接:https://xfxuezhang.blog.csdn.net/article/details/122407412
相關(guān)推薦
- 2022-03-19 解決訪問不到Linux服務(wù)器中RabbitMQ管理頁面問題_Linux
- 2022-11-23 Python利用keyboard模塊實(shí)現(xiàn)鍵盤記錄操作_python
- 2023-01-07 anaconda?navigator打不開問題的解決方法_python
- 2024-03-18 bootstrap application 和 nacos 中配置文件的優(yōu)先級
- 2022-01-31 torch.save實(shí)現(xiàn)對網(wǎng)絡(luò)結(jié)構(gòu)和模型參數(shù)的保存 & pytorch模型文件.pt .pt
- 2022-07-11 搭建spring MVC框架,完成和servlet相似的操作
- 2022-03-28 如何創(chuàng)建VS?Code?擴(kuò)展插件_相關(guān)技巧
- 2023-02-09 go?sync?Once實(shí)現(xiàn)原理示例解析_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支