-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathtime_stamp_matching.py
75 lines (61 loc) · 1.96 KB
/
time_stamp_matching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 5 15:09:02 2017
@author: ana-rpg
"""
import glob
import numpy as np
import re
import os
# Path to the data extracted from the Udacity dataset
folder = None #"training" or "testing"
assert folder, "You should provide the dataset folder"
experiments = glob.glob(folder + "/*")
def extractInfoFromFile(file_name):
steer_stamps = []
# Read file and extract time stamp
try:
steer_stamps = np.loadtxt(file_name, usecols=1, delimiter=',', skiprows=1, dtype=int)
except:
print(file_name)
return steer_stamps
def getMatching(array1, array2):
match_stamps = []
match_idx = []
for i in array1:
dist = abs(i - array2)
idx = np.where(dist == 0)[0]
match_stamps.append(array2[idx])
match_idx.append(idx)
return match_stamps, match_idx
def getSyncSteering(fname, idx):
mat = []
try:
mat = np.loadtxt(fname, usecols=(6,7,8,9,10,11), skiprows=1, delimiter=',')
mat = mat[idx,:]
except:
print(fname)
return mat
# For every bag...
for exp in experiments:
# Read images
images = [os.path.basename(x) for x in glob.glob(exp + "/images/*.png")]
im_stamps = []
for im in images:
stamp = int(re.sub(r'\.png$', '', im))
im_stamps.append(stamp)
im_stamps = np.array(sorted(im_stamps))
# Extract time stamps from steerings
file_name = exp + "/interpolated.csv"
steer_stamps = extractInfoFromFile(file_name)
# Time-stamp matching between images and steerings
match_stamp, match_idx = getMatching(im_stamps, steer_stamps)
match_idx = np.array(match_idx)
match_idx = match_idx[:,0]
# Get matched commands
original_fname = exp + "/interpolated.csv"
sync_steer = getSyncSteering(original_fname, match_idx)
new_fname = exp + "/sync_steering.txt"
np.savetxt(new_fname, sync_steer, delimiter=',',
header="angle,torque,speed,lat,long,alt")