2015年4月21日 星期二

Python 計時程式

目前的拍照程式中,幀與幀之間只是用 time.sleep(15) 來做計時。這樣做並不準確。因為拍攝出來的畫面內容的不同,會有不同的壓縮需時,也會有不同的檔案體積,更會有不同的儲存時間。上一幀由拍攝到儲存同了五秒,下一幀可能會花上六秒。為了精準處理拍攝間距,等候的部份需要特別的處理:
#!/usr/bin/python
##------------------------------------------------------------
##  Time Delay Test Version 1.00
##  Copyright Pacess Studio, 2015.  All rights reserved.
##------------------------------------------------------------

import time

##------------------------------------------------------------
##  Global variables
delayInMillis = 2000

##------------------------------------------------------------
##  Function
def getTimeMillis():
    millis = int(round(time.time()*1000))
    return millis

##============================================================
##  Program start
##============================================================
print "Time Delay Test Version 1.00"

millisBegin = getTimeMillis()
print "Begin: "+str(millisBegin)

##  Do your own task here!
time.sleep(1)

##------------------------------------------------------------
##  Calculate how long should be delayed
millisDelay = millisBegin+delayInMillis-getTimeMillis()
print "Delay for: "+str(millisDelay)

##  Wait until delay matches delayInMillis
time.sleep(millisDelay/1000.0)

##  Calculate how long was delayed
millisDifferent = getTimeMillis()-millisBegin
print "Different: "+str(millisDifferent)

沒有留言: