-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzeGT.py
329 lines (300 loc) · 14.1 KB
/
analyzeGT.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# -*- coding: utf-8 -*-
"""
last mod 2/4/19
"""
import numpy as np
lidar_files = '../object/training/velodyne/{:06d}.bin'
img_files = '../object/training/image_2/{:06d}.png'
gt_files = '../object/training_labels_orig/{:06d}.txt'
files_to_use = range(1000)
truncated_cutoffs = np.array((.15, .3, .5))
occluded_cutoffs = np.array((0, 1, 2))
height_cutoffs = np.array((40, 25, 25))
scored_classes = ('Car', 'Pedestrian', 'Cyclist')
nfiles_training = [154, 447, 233, 144, 314, 297, 270, 800, 390, 803, 294,
373, 78, 340, 106, 376, 209, 145, 339, 1059, 837]
def readGroundTruthFile(gtstr, classes_include = ('Car',)):
gtstr = gtstr.split('\n')
if gtstr[-1] == '': gtstr.pop()
classes = []
rectangles = []
imbbs = []
difficulties = []
scored = []
elevation = []
for gtrow in gtstr:
gtrow = gtrow.split(' ')
this_class = gtrow[0]
if this_class not in classes_include:
continue
classes.append(classes_include.index(this_class))
# 2D rectangle on ground, in xyalw form
gtang = 4.7124 - float(gtrow[14])
gtang = gtang - 6.2832 if gtang > 3.1416 else gtang
gtbox = (float(gtrow[13]), -float(gtrow[11]),
gtang, float(gtrow[10])/2, float(gtrow[9])/2)
rectangles.append(gtbox)
# 2D bounding box in image, top-bottom-left-right
imbbs.append((float(gtrow[5]),float(gtrow[7]),float(gtrow[4]),float(gtrow[6])))
# elevation as meters up from car bottom
elevation.append(1.65-float(gtrow[12]))
# copy kitti's scoring-or-ignoring strategy
truncation = float(gtrow[1])
occlusion = int(gtrow[2])
height = float(gtrow[7]) - float(gtrow[5]) # image bb height
difficulty = 0
for dd in range(3):
not_met = truncation > truncated_cutoffs[dd]
not_met |= occlusion > occluded_cutoffs[dd]
not_met |= height < height_cutoffs[dd]
if not_met: difficulty = dd + 1
difficulties.append(difficulty)
scored.append(difficulty < 3 and this_class in scored_classes)
return classes, rectangles, imbbs, difficulties, scored, elevation
def readGroundTruthFileTracking(gtstr, classes_include = ('Car',)):
gtstr = gtstr.split('\n')
if gtstr[-1] == '': gtstr.pop()
nfiles = max(int(gtrow.split(' ')[0]) for gtrow in gtstr)+1
outputs = [[] for file_idx in range(nfiles)]
dontcares = [[] for file_idx in range(nfiles)]
base_output = {'class':None,'box':None,'imbb':None,'difficulty':None,
'scored':None,'elevation':None,'id':None}
for gtrow in gtstr:
gtrow = gtrow.split(' ')
file_idx = int(gtrow[0])
# track id = int(gtrow[1])
row_output = base_output.copy()
row_output['id'] = int(gtrow[1])
gtrow = gtrow[2:]
this_class = gtrow[0]
if this_class == "DontCare":
dontcares[file_idx].append((float(gtrow[5]), float(gtrow[7]),
float(gtrow[4]), float(gtrow[6])))
if this_class not in classes_include:
continue
row_output['class'] = classes_include.index(this_class)
# 2D rectangle on ground, in xyalw form
gtang = 4.7124 - float(gtrow[14])
gtang = gtang - 6.2832 if gtang > 3.1416 else gtang
gtbox = (float(gtrow[13]), -float(gtrow[11]), gtang,
float(gtrow[10])/2, float(gtrow[9])/2)
row_output['box'] = gtbox
# 2D bounding box in image, top-bottom-left-right
row_output['imbb'] = (float(gtrow[5]),float(gtrow[7]),
float(gtrow[4]),float(gtrow[6]))
# elevation as meters up from car bottom
row_output['elevation'] = 1.65-float(gtrow[12])
# copy kitti's scoring-or-ignoring strategy
truncation = float(gtrow[1])
occlusion = int(gtrow[2])
height = float(gtrow[7]) - float(gtrow[5]) # image bb height
difficulty = 0
for dd in range(3):
not_met = truncation > truncated_cutoffs[dd]
not_met |= occlusion > occluded_cutoffs[dd]
not_met |= height < height_cutoffs[dd]
if not_met: difficulty = dd + 1
row_output['difficulty'] = difficulty
row_output['scored'] = difficulty < 3 and this_class in scored_classes
outputs[file_idx].append(row_output)
return outputs, dontcares
"""
prunes estimates based on whether kitti will actually have gt
also formats in standard kitti output
input: 2d array of boxes, (x,y,a (radians),l,w,z (bottom),h,score)
output: string version
"""
def formatForKittiScore(ests, calib_project, imgshape):
output_text_format = '{:s} {:.2f} {:d}' + ' {:.2f}'*13
outputstr = []
for msmt in ests:
cos,sin = np.cos(msmt[2]), np.sin(msmt[2])
corners = np.zeros((8,3))
corners[0,:2] = msmt[:2] + (cos*msmt[3]+sin*msmt[4], sin*msmt[3]-cos*msmt[4])
corners[1,:2] = msmt[:2] + (cos*msmt[3]-sin*msmt[4], sin*msmt[3]+cos*msmt[4])
corners[2,:2] = msmt[:2] - (cos*msmt[3]+sin*msmt[4], sin*msmt[3]-cos*msmt[4])
corners[3,:2] = msmt[:2] - (cos*msmt[3]-sin*msmt[4], sin*msmt[3]+cos*msmt[4])
corners[:4,2] = msmt[5]
corners[4:,:2] = corners[:4,:2]
corners[4:,2] = msmt[5]+msmt[6]
msmt_corners = corners.dot(calib_project[:3,:3].T) + calib_project[:3,3]
msmt_corners = msmt_corners[:,:2] / msmt_corners[:,2:]
topfull, leftfull = np.min(msmt_corners, axis=0)
bottomfull, rightfull = np.max(msmt_corners, axis=0)
top, bottom, left, right = (max(topfull, 0), min(bottomfull, imgshape[0]),
max(leftfull, 0), min(rightfull, imgshape[1]))
imbb_area = (bottom - top) * (right - left)
full_imbb_area = (bottomfull - topfull) * (rightfull - leftfull)
truncation_level = 1 - imbb_area / full_imbb_area
if truncation_level > .4:
continue
if bottom-top < 22:
continue
observation_angle = np.pi/2. - np.arctan2(msmt[1], msmt[0])
rotation_angle = np.pi/2. - msmt[2]
output = ('Car',0.,0,observation_angle,left,top,right,bottom,
msmt[6],msmt[4]*2,msmt[3]*2,-msmt[1],-msmt[5]+1.65,msmt[0],
rotation_angle, msmt[7])
outputstr.append(output_text_format.format(*output))
return '\n'.join(outputstr)
def formatForKittiScoreTracking(ests, estids, scores, fileidx,
groundtiles, calib_project, imgshape, dontcares,
scorecutoff=0., carheight=1.7):
output_text_format = '{:d} {:d} {:s} {:.2f} {:d}' + ' {:.2f}'*13
outputstr = []
nests = len(ests)
for estidx in range(nests):
msmt = ests[estidx]
if msmt[0]*.9 + 3 < abs(msmt[1]):
continue
if scores[estidx] < scorecutoff:
continue
tilex = min(19, max(0, int(msmt[0]/3+1)))
tiley = min(31, max(0, int(msmt[1]/3+16)))
groundtile = groundtiles[tilex, tiley]
elev = groundtile[3] - groundtile[0]*msmt[0] - groundtile[1]*msmt[1]
cos,sin = np.cos(msmt[2]), np.sin(msmt[2])
corners = np.zeros((8,3))
corners[0,:2] = msmt[:2] + (cos*msmt[3]+sin*msmt[4], sin*msmt[3]-cos*msmt[4])
corners[1,:2] = msmt[:2] + (cos*msmt[3]-sin*msmt[4], sin*msmt[3]+cos*msmt[4])
corners[2,:2] = msmt[:2] - (cos*msmt[3]+sin*msmt[4], sin*msmt[3]-cos*msmt[4])
corners[3,:2] = msmt[:2] - (cos*msmt[3]-sin*msmt[4], sin*msmt[3]+cos*msmt[4])
corners[:4,2] = elev
corners[4:,:2] = corners[:4,:2]
corners[4:,2] = elev + carheight
msmt_corners = corners.dot(calib_project[:3,:3].T) + calib_project[:3,3]
msmt_corners = msmt_corners[:,:2] / msmt_corners[:,2:]
topfull, leftfull = np.min(msmt_corners, axis=0)
bottomfull, rightfull = np.max(msmt_corners, axis=0)
top, bottom, left, right = (max(topfull, 0), min(bottomfull, imgshape[0]),
max(leftfull, 0), min(rightfull, imgshape[1]))
imbb_area = (bottom - top) * (right - left)
full_imbb_area = (bottomfull - topfull) * (rightfull - leftfull)
truncation_level = 1 - imbb_area / full_imbb_area
if truncation_level > .4:
continue
if bottom-top < 22:
continue
removed = False
for dontcare in dontcares[fileidx]:
overlap = max(0, min(bottom-dontcare[0], dontcare[1]-top))
overlap *= max(0, min(right-dontcare[2], dontcare[3]-left))
overlap /= (bottom-top)*(right-left)
if overlap > .6:
removed = True
if removed:
continue
observation_angle = np.pi/2. - np.arctan2(msmt[1], msmt[0])
rotation_angle = np.pi/2. - msmt[2]
estid = int(estids[estidx])
score = scores[estidx]
output = (fileidx,estid,'Car',0.,0,observation_angle,left,top,right,bottom,
1.7,#msmt[6],#
msmt[4]*2,msmt[3]*2,-msmt[1],-elev+1.65,msmt[0],
rotation_angle, score)
#outputstr.append(output_text_format.format(*output))
#return '\n'.join(outputstr)
outputstr.append((msmt[:5].copy(), estid, score))
return outputstr
## check out 3D dimensions of objects by class
if __name__ == '__main__' and False:
boxes = {}
for file_idx in files_to_use:
if gt_files is not None:
with open(gt_files.format(file_idx), 'r') as fd: gtstr = fd.read()
gtstr = gtstr.split('\n')
if gtstr[-1] == '': gtstr = gtstr[:-1]
gtboxes = []
for gtrow in gtstr:
gtrow = gtrow.split(' ')
if gtrow[0] == 'DontCare' or gtrow[0]=='Misc': continue
boxlen = float(gtrow[10])
boxwid = float(gtrow[9])
boxheight = float(gtrow[8])
boxclass = gtrow[0]
if boxclass in boxes.keys():
boxes[boxclass].append((boxlen, boxwid, boxheight, file_idx))
else:
boxes[boxclass] = [(boxlen, boxwid, boxheight, file_idx)]
cats = boxes.keys()
for cat in cats:
dims = np.array(boxes[cat])
print(cat)
print(np.min(dims[:,:3], axis=0))
print(np.max(dims[:,:3], axis=0))
## check out characteristics that decide object difficulty
if __name__ == '__main__' and False:
data = []
for file_idx in files_to_use:
if gt_files is not None:
with open(gt_files.format(file_idx), 'r') as fd: gtstr = fd.read()
gtstr = gtstr.split('\n')
if gtstr[-1] == '': gtstr = gtstr[:-1]
for gtrow in gtstr:
gtrow = gtrow.split(' ')
if gtrow[0] == 'Car' or gtrow[0] == 'Van':
truncation = float(gtrow[1])
occlusion = int(gtrow[2])
height = float(gtrow[7]) - float(gtrow[5])
distance = float(gtrow[13])
data.append((truncation, occlusion, height, gtrow[0]=='Van', distance))
data = np.array(data)
difficulties = np.zeros(len(data), dtype=np.uint8)
for difficulty in range(3):
this_hard = data[:,0] > truncated_cutoffs[difficulty]
this_hard |= data[:,1] > occluded_cutoffs[difficulty] + .001
this_hard |= data[:,2] < height_cutoffs[difficulty]
difficulties[this_hard] = difficulty+1
difficulties[data[:,3] > .1] = 3
## plot objects w/ ground truth
if __name__ == '__main__' and False:
from cv2 import imshow, waitKey, destroyWindow
from plotStuff import base_image, plotRectangle, drawLine, grayer
from plotStuff import reference as plotReference
from calibs import calib_map, view_by_day
from imageio import imread
def clear(): destroyWindow('a') # for convenience
view_distance = 30. # 2*x ahead, x to either side, meters
cutoffs = np.array([[.15,0,-40],[.3,1,-25],[.5,2,-25]])
colors_by_difficulty = np.array([[255,0,0],[200,200,0],[0,200,200],[0,0,255]]).astype(np.uint8)
classes = ['Car', 'Van']
for file_idx in files_to_use:
view_angle = view_by_day[calib_map[file_idx]]
img = imread(img_files.format(file_idx))[:,:,::-1]
img = grayer(img)
plot_img = base_image.copy()
# draw lines to show visualized part of map
linestart = (639, 320)
lineend = plotReference(int(.95/view_angle), .95)
drawx, drawy = drawLine(linestart[0], linestart[1], lineend[0], lineend[1])
lineend = plotReference(int(.95/view_angle), -.95)
drawx2, drawy2 = drawLine(linestart[0], linestart[1], lineend[0], lineend[1])
plot_img[drawx, drawy] = [240,240,240]
plot_img[drawx2, drawy2] = [240,240,240]
with open(gt_files.format(file_idx), 'r') as fd: gtstr = fd.read()
gtstr = gtstr.split('\n')
if gtstr[-1] == '': gtstr = gtstr[:-1]
for gtrow in gtstr:
gtrow = gtrow.split(' ')
if gtrow[0] not in classes: continue
gtbox = (float(gtrow[13]), -float(gtrow[11]),
1.5708-float(gtrow[14]), float(gtrow[10])/2, float(gtrow[9])/2)
box = np.array(gtbox)
box[[0,1,3,4]] /= view_distance
box[:2] = plotReference(*box[:2])
box[3:] *= 320
monitor_vals = (float(gtrow[1]), int(gtrow[2]), float(gtrow[5])-float(gtrow[7]))
difficulty = 0
for ddd in range(3):
if any(monitor_vals > cutoffs[ddd]):
difficulty = ddd + 1
color = colors_by_difficulty[difficulty]
plotRectangle(plot_img, box, color)
display_img = np.zeros((plot_img.shape[0]+img.shape[0], img.shape[1], 3),
dtype=np.uint8)
display_img[:plot_img.shape[0], (img.shape[1]-plot_img.shape[1])//2:
(img.shape[1]+plot_img.shape[1])//2] = plot_img
display_img[plot_img.shape[0]:] = img
imshow('a', display_img);
if waitKey(5000) == ord('q'):
break