-
Notifications
You must be signed in to change notification settings - Fork 2
/
trajectoryAndImage2GroundTruth.py
executable file
·284 lines (209 loc) · 9 KB
/
trajectoryAndImage2GroundTruth.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# disabling contracts for speed
import contracts
import yaml
import geometry as geo
import numpy as np
from duckietown_world.world_duckietown.tile import get_lane_poses
import duckietown_world as dw
import geometry as g
import pandas as pd
contracts.disable_all()
# Load Duckietown map
m = dw.load_map('robotarium2')
# Folders that should be processed
folderNames = ['autobot04_r2']
# Calculates relative pose between two poses
def relative_pose(q0, q1):
return g.SE2.multiply(g.SE2.inverse(q0), q1)
# Interpolates position based on timestamp between two poses
def interpolate(q0, q1, alpha):
q1_from_q0 = relative_pose(q0, q1)
vel = g.SE2.algebra_from_group(q1_from_q0)
rel = g.SE2.group_from_algebra(vel * alpha)
q = g.SE2.multiply(q0, rel)
return q
# Calculates distance to middle lane and heading
def calculatePose(qPose):
success = False
transforms = dw.SE2Transform.from_SE2(qPose)
# Filters these tiles out. We don't want them in training set
notWantedTiles = ['3way_left', '4way', 'asphalt']
pose_object = transforms
lanePoses = list(get_lane_poses(m, pose_object.as_SE2()))
try:
tile = list(lanePoses[0].tile.children.keys())[0]
if tile in notWantedTiles:
return success, 0, 0, str(tile)
except Exception as e:
print(e)
if len(lanePoses) == 0:
return success, 0, 0, 'asphalt'
else:
distance_from_center = lanePoses[0].lane_pose.distance_from_center / 2
rel_heading = lanePoses[0].lane_pose.relative_heading
tile = list(lanePoses[0].tile.children.keys())[0]
success = True
return success, distance_from_center, rel_heading, str(tile)
# Open trajectory yaml files in each folder
for folderNamesSingle in folderNames:
finalArrayWithoutPose = []
finalArrayPoses = []
realTimestamps = []
seqs2 = []
final_trajectory = []
timeStart = []
for trajectoryFileNumber in range(0,500):
trajectoryFile = folderNamesSingle + '_' + str(trajectoryFileNumber) + '.yaml'
print('Munging ' + str(trajectoryFile))
# Path where all yaml files are
pathToYaml = 'trajectoryFiles/' + str(folderNamesSingle) + '/' + str(trajectoryFile)
try:
with open(pathToYaml, 'r') as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
except Exception as e:
if trajectoryFileNumber != 0:
break
else:
continue
# Read in yaml information and store in arrays
timestart = data['begin_time_stamp']
data_points = len(data['trajectory_data'])
x = np.zeros((data_points,))
y= np.zeros((data_points,))
R = np.zeros((3,3, data_points))
phi = np.zeros((3, data_points))
dx = 999.999*np.ones((data_points, ))
dy = 999.999*np.ones((data_points, ))
dr = 999.999*np.ones((data_points, ))
dphi = 999.999*np.ones((data_points, ))
for idx, [time, traj] in enumerate(data['trajectory_data'].items()):
x[idx] = np.array(traj[0])
y[idx] = np.array(traj[1])
R[:,:,idx] = np.reshape(np.array(traj[3:]), (3,3))
phi[:,idx] = np.array([np.arctan2(-R[1,2,idx],R[2,2,idx]),
np.arctan2(R[0,2,idx],np.sqrt(R[0,0,idx]**2 + R[0,1,idx]**2)),
np.arctan2(-R[0,1,idx], R[0,0,idx])])
realTimestamps.append(float(time) + float(timestart))
timeStart.append(timestart)
z = phi[2,idx]
points = np.array([x[idx], y[idx]])
final_trajectory.append([points, z])
for entry in range(0, len(final_trajectory)):
x = (final_trajectory[entry][0][0] )
y = final_trajectory[entry][0][1]
alpha = final_trajectory[entry][1]
q5 = geo.SE2_from_translation_angle([x,y],alpha)
seqs2.append(q5)
# Path to file with watchtower image timesteps
path = 'trajectoryFiles/' + str(folderNamesSingle) + '/image_timestamps.csv'
imagesDFcolumns = ['ImageName', 'Unused','Seconds', 'Nanoseconds']
imagesDF = pd.read_csv(path)
timeStampImagesSecondsArray = imagesDF.iloc[:,2]
timeStampImagesNanoSecArray = imagesDF.iloc[:,3]
imageNumber = imagesDF.iloc[:,0]
entryNumberTrajectory = 0
# For each watchtower image timesteps, calculate the pose with the trajectory data
for entry in range(0, len(timeStampImagesSecondsArray)-1):
if entryNumberTrajectory + 2 >= len(realTimestamps):
break
timeStampImagesSeconds = imagesDF.iloc[entry, 2]
timeStampImagesNanoSec = imagesDF.iloc[entry,3] * (10**(-9))
imageNumber = imagesDF.iloc[entry,0]
# Check if exact time is available in both dataframes
timeStampImages = float(timeStampImagesSeconds) + float(timeStampImagesNanoSec)
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory + 1])
if timeStampImages == timeStampTrajectory:
q2 = seqs2[entryNumberTrajectory]
finalArrayPoses.append(q2)
success, centerDistance, relativeHeading, tile = calculatePose(q2)
if success is False:
continue
finalArrayWithoutPose.append([imageNumber, timeStampImages, centerDistance, relativeHeading, tile])
elif timeStampImages < timeStampTrajectory:
if entryNumberTrajectory == 0:
continue
while timeStampImages < timeStampTrajectory:
if timeStampTrajectory > timeStampImages > (float(realTimestamps[entryNumberTrajectory - 1])):
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory-1])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory])
break
elif timeStampTrajectory > timeStampImages > (float(realTimestamps[entryNumberTrajectory - 2])):
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory - 2])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory])
break
else:
entryNumberTrajectory -= 1
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory + 1])
if timeStampTrajectory < timeStampImages < timeStampTrajectoryAfter:
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory + 1])
timeStamp1 = timeStampTrajectory
timeStampWanted = timeStampImages
timeStamp2 = timeStampTrajectoryAfter
param = (timeStampWanted- timeStamp1) / (timeStamp2 - timeStamp1)
q2 = seqs2[entryNumberTrajectory+1]
q1 = seqs2[entryNumberTrajectory]
qInter = interpolate(q1, q2, param)
success, centerDistance, relativeHeading, tile = calculatePose(qInter)
if success is False:
continue
finalArrayPoses.append(qInter)
finalArrayWithoutPose.append([imageNumber, timeStampImages, centerDistance, relativeHeading, tile])
continue
elif timeStampImages > timeStampTrajectory:
if timeStampImages < timeStampTrajectoryAfter:
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory + 1])
timeStamp1 = timeStampTrajectory
timeStampWanted = timeStampImages
timeStamp2 = timeStampTrajectoryAfter
param = (timeStampWanted- timeStamp1)/ (timeStamp2 - timeStamp1)
q2 = seqs2[entryNumberTrajectory+1]
q1 = seqs2[entryNumberTrajectory]
qInter = interpolate(q1, q2, param)
success, centerDistance, relativeHeading, tile = calculatePose(qInter)
if success is False:
continue
finalArrayPoses.append(qInter)
finalArrayWithoutPose.append([imageNumber, timeStampImages, centerDistance, relativeHeading, tile])
else:
while timeStampImages > timeStampTrajectoryAfter:
if entryNumberTrajectory + 2 >= len(realTimestamps):
break
if timeStampImages > timeStampTrajectory and timeStampImages > timeStampTrajectoryAfter:
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory + 2])
if timeStampTrajectory < timeStampImages < timeStampTrajectoryAfter:
break
else:
entryNumberTrajectory += 1
if timeStampTrajectoryAfter < timeStampImages:
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStampTrajectoryAfter = float(realTimestamps[entryNumberTrajectory + 1])
# if entryNumberTrajectory > 500:
# timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStampTrajectory = float(realTimestamps[entryNumberTrajectory])
timeStamp1 = timeStampTrajectory
timeStampWanted = timeStampImages
timeStamp2 = timeStampTrajectoryAfter
param = (timeStampWanted- timeStamp1) / (timeStamp2 - timeStamp1)
q2 = seqs2[entryNumberTrajectory+1]
q1 = seqs2[entryNumberTrajectory]
qInter = interpolate(q1, q2, param)
success, centerDistance, relativeHeading, tile = calculatePose(qInter)
if success is False:
continue
finalArrayPoses.append(qInter)
finalArrayWithoutPose.append([imageNumber, timeStampImages, centerDistance, relativeHeading, tile])
else:
print('unhandled')
finalArrayWithoutPose = np.array(finalArrayWithoutPose)
ArrayCol = ['ImageNumber',' timeStamp','centerDistance', 'relativeHeading', 'Tile']
finalArrayWithPose = pd.DataFrame(finalArrayWithoutPose, columns=ArrayCol)
fileName = 'trajectoryFiles/' + str(folderNamesSingle) + '/output_labeled.csv'
finalArrayWithPose.to_csv(fileName, index=False)
print('saved as: ' + str(fileName))