Created
May 23, 2017 12:33
-
-
Save mkassner/f97e118e66f153fb6066a65df7b0bc4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
logger = logging.getLogger() | |
import numpy as np | |
from scipy.interpolate import UnivariateSpline | |
import sys | |
def sanitize_timestamps(ts): | |
logger.debug("Checking %s timestamps for monotony in direction and smoothness"%ts.shape[0]) | |
avg_frame_time = (ts[-1] - ts[0])/ts.shape[0] | |
logger.debug('average_frame_time: %s'%(1./avg_frame_time)) | |
raw_ts = ts #only needed for visualization | |
runs = 0 | |
while True: | |
#forward check for non monotonic increasing behaviour | |
clean = np.ones((ts.shape[0]),dtype=np.bool) | |
damper = 0 | |
for idx in range(ts.shape[0]-1): | |
if ts[idx] >= ts[idx+1]: #not monotonically increasing timestamp | |
damper = 50 | |
clean[idx] = damper <= 0 | |
damper -=1 | |
#backward check to smooth timejumps forward | |
damper = 0 | |
for idx in range(ts.shape[0]-1)[::-1]: | |
if ts[idx+1]-ts[idx]>1: #more than one second forward jump | |
damper = 50 | |
clean[idx] &= damper <= 0 | |
damper -=1 | |
if clean.all() == True: | |
if runs >0: | |
logger.debug("Timestamps were bad but are ok now. Correction runs: %s"%runs) | |
# from matplotlib import pyplot as plt | |
# plt.plot(frames,raw_ts) | |
# plt.plot(frames,ts) | |
# # plt.scatter(frames[~clean],ts[~clean]) | |
# plt.show() | |
else: | |
logger.debug("Timestamps are clean.") | |
return ts | |
runs +=1 | |
if runs > 4: | |
logger.error("Timestamps could not be fixed!") | |
return ts | |
logger.warning("Timestamps are not sane. We detected non monotitc or jumpy timestamps. Fixing them now") | |
frames = np.arange(len(ts)) | |
s = UnivariateSpline(frames[clean],ts[clean],s=0) | |
ts = s(frames) | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.DEBUG) | |
ts_file_path = sys.argv[1] | |
ts_file = np.load(ts_file_path) | |
ts_file = sanitize_timestamps(ts_file) | |
np.save(ts_file_path,ts_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment