欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?Timer和TimerFPS計(jì)時(shí)工具類

 更新時(shí)間:2022年03月17日 14:20:34   作者:小鋒學(xué)長生活大爆炸  
這篇文章主要介紹了Python?Timer和TimerFPS計(jì)時(shí)工具類,文章分享得代碼內(nèi)容詳細(xì),具有一的的參考價(jià)值,需要的小伙伴可以參考一下

以下工具類代碼來自開源項(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()

效果

到此這篇關(guān)于Python Timer和TimerFPS計(jì)時(shí)工具類的文章就介紹到這了,更多相關(guān)Python 計(jì)時(shí)工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python numpy查詢定位賦值數(shù)值所在行列

    python numpy查詢定位賦值數(shù)值所在行列

    這篇文章主要介紹了python numpy查詢定位賦值數(shù)值所在行列,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • python多線程并發(fā)實(shí)例及其優(yōu)化

    python多線程并發(fā)實(shí)例及其優(yōu)化

    這篇文章主要介紹了python多線程并發(fā)實(shí)例及其優(yōu)化,threading是擴(kuò)展模塊,在thread的基礎(chǔ)上進(jìn)行了封裝及改進(jìn)。所以只需要使用threading這個(gè)模塊就能完成并發(fā)的測試,需要的朋友可以參考下
    2019-06-06
  • Pytorch dataloader在加載最后一個(gè)batch時(shí)卡死的解決

    Pytorch dataloader在加載最后一個(gè)batch時(shí)卡死的解決

    這篇文章主要介紹了Pytorch dataloader在加載最后一個(gè)batch時(shí)卡死的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 淺析python 定時(shí)拆分備份 nginx 日志的方法

    淺析python 定時(shí)拆分備份 nginx 日志的方法

    本文給大家分享python 定時(shí)拆分備份 nginx 日志的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-04-04
  • Python+Selenium實(shí)現(xiàn)在Geoserver批量發(fā)布Mongo矢量數(shù)據(jù)

    Python+Selenium實(shí)現(xiàn)在Geoserver批量發(fā)布Mongo矢量數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了如何利用Python+Selenium實(shí)現(xiàn)在 Geoserver批量發(fā)布來自Mongo中的矢量數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-07-07
  • python3 實(shí)現(xiàn)函數(shù)寫文件路徑的正確方法

    python3 實(shí)現(xiàn)函數(shù)寫文件路徑的正確方法

    今天小編就為大家分享一篇python3 實(shí)現(xiàn)函數(shù)寫文件路徑的正確方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python線程的幾種創(chuàng)建方式詳解

    python線程的幾種創(chuàng)建方式詳解

    這篇文章主要介紹了python線程的幾種創(chuàng)建方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python的streamplot使用及說明

    Python的streamplot使用及說明

    這篇文章主要介紹了Python的streamplot使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python入門游戲之井字棋實(shí)例代碼

    python入門游戲之井字棋實(shí)例代碼

    python井字棋游戲雖然看上去非常簡陋,但是卻非常值得學(xué)習(xí),下面這篇文章主要給大家介紹了關(guān)于python入門游戲之井字棋的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Django執(zhí)行python?manage.py?makemigrations報(bào)錯的解決方案分享

    Django執(zhí)行python?manage.py?makemigrations報(bào)錯的解決方案分享

    相信用過很多Django makemigrations的人都會遇到過makemigrations時(shí)會發(fā)生報(bào)錯,下面這篇文章主要給大家介紹了關(guān)于Django執(zhí)行python?manage.py?makemigrations報(bào)錯的解決方案,需要的朋友可以參考下
    2022-09-09

最新評論