forked from lawbreaker2022/LawBreaker-SourceCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TracePreprocess.py
590 lines (508 loc) · 29.5 KB
/
TracePreprocess.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import copy
import json
import numpy as np
import math
from numpy import linalg as LA
from shapely.geometry import Polygon
def point2norm(point1, point2):
if point1 is None or point2 is None:
return float('inf')
a = np.array([point1['x'], point1['y'], point1['z']])
b = np.array([point2['x'], point2['y'], point2['z']])
return LA.norm(a - b)
def polygon_point(polygonPointList): # [[x,y,z], [x, y,z] ]
polygon = []
for i in range(len(polygonPointList)):
point = [polygonPointList[i]['x'], polygonPointList[i]['y']]
polygon.append(point)
return polygon
def position_rotate(position_in_vehicle, rotation_theta):
'''
Args:
position_in_vehicle: the position in the vehicle frame
Returns: The position in the ENU frame.
'''
new_position = copy.deepcopy(position_in_vehicle)
x = new_position[0]
y = new_position[1]
x1 = x * math.cos(rotation_theta) - y * math.sin(rotation_theta)
y1 = x * math.sin(rotation_theta) + y * math.cos(rotation_theta)
new_position[0] = x1
new_position[1] = y1
return new_position
def get_ego_polygon(ego_state):
gps_offset = -1.348649
ego_position = list(ego_state['pose']['position'].values())
ego_length = ego_state['size']['length']
ego_width = ego_state['size']['width']
theta = ego_state['pose']['heading']
front_left = [ego_length/2 - gps_offset, ego_width/2.0]
front_right = [ego_length/2.0 - gps_offset, -ego_width/2.0]
back_left = [-ego_length/2.0 - gps_offset, ego_width/2.0]
back_right = [-ego_length/2.0 - gps_offset, -ego_width/2.0]
poly1 = [position_rotate(front_left, theta)[0] + ego_position[0], position_rotate(front_left,theta)[1] + ego_position[1]]
poly2 = [position_rotate(front_right, theta)[0] + ego_position[0], position_rotate(front_right,theta)[1] + ego_position[1]]
poly3 = [position_rotate(back_right, theta)[0] + ego_position[0], position_rotate(back_right,theta)[1] + ego_position[1]]
poly4 = [position_rotate(back_left, theta)[0] + ego_position[0], position_rotate(back_left, theta)[1] + ego_position[1]]
ego_polygon = [poly1, poly2, poly3, poly4]
return ego_polygon
class Trace:
'''
Main attributes in Trace class:
1. list of npc, pedestrian and static obstacle
agent: ['npc1', 'npc2', 'pedestrian1', 'pedestrian2',...]
2. distance dict
distance:
'perception':
'npc1': [10.3, 10.2, 10.1, ...]
'npc2': [10.3, 10.2, 10.1, ...]
'truth':
'npc1': [10.3, 10.2, 10.1, ...]
'npc2': [10.3, 10.2, 10.1, ...]
3. trace dict
trace:
'time': [0, 0.1, 0.2, 0.3,...]
'ego':
[(array(x,y,z), array(qx,qy,qz,qw), array(vx,vy,vz), array(ax, ay,az))]
'perception':
'npc1': [(array(x,y,z), array(vx,vy,vz), theta, [[x,y,z], [x,y,z])] position, velocity, heading, polygon
'pedestrian1': [(array(x,y,z), array(vx,vy,vz)), (array(x,y,z), array(vx,vy,vz)), (array(x,y,z), array(vx,vy,vz))]
'truth':
'npc1': [(array(x,y,z), array(vx,vy,vz)), (array(x,y,z), array(vx,vy,vz)), (array(x,y,z), array(vx,vy,vz))]
'pedestrian1': [(array(x,y,z), array(vx,vy,vz)), (array(x,y,z), array(vx,vy,vz)), (array(x,y,z), array(vx,vy,vz))]
'''
def __init__(self, origin_trace):
# origin_trac = execution trace
self.init_trace = copy.deepcopy(origin_trace['trace'])
self.is_groundtruth = origin_trace['ego']['groundTruthPerception']
self.time = []
self.trace = {}
self.distance = {}
self.perception_diff = {}
self.agent = origin_trace['AgentNames']
self.trace['time'] = []
self.trace['ego'] = {'position': [], 'velocity': [], 'heading': [], 'acceleration': [], 'shape': []}
self.trace['perception'] = dict()
self.trace['truth'] = dict()
for _item in self.agent:
self.perception_diff[_item] = {'type': [0.0]*len(self.init_trace),
'position': [0.0]*len(self.init_trace),
'velocity': [0.0]*len(self.init_trace),
'heading': [0.0]*len(self.init_trace),
'shape': [0.0]*len(self.init_trace)}
self.trace['perception'][_item] = {'position': [], 'velocity': [], 'heading': [], 'acceleration': [], 'shape': []}
self.trace['truth'][_item] = {'position': [], 'velocity': [], 'heading': [], 'acceleration': [], 'shape': []}
_distance = {}
perception_dis = {}
truth_dist = {}
for _item in self.agent:
perception_dis[_item] = []
truth_dist[_item] = []
self.distance['perception'] = perception_dis
self.distance['truth'] = truth_dist
self.trace["ego-forTrafficRule"] = {'highBeamOn':[], 'lowBeamOn':[], 'turnSignal':[], 'fogLightOn':[], 'hornOn':[], 'warningflashOn':[], 'gear':[], 'engineOn':[], 'direction':[], 'manualIntervention':[]}
self.trace["ego-driving-forTrafficRule"] = {'speed':[], 'acc':[], 'brake':[], 'isLaneChanging':[], 'isOverTaking':[], 'isTurningAround':[]}
self.trace["currentlane-forTrafficRule"] = {'number':[], 'direction':[]}
self.trace["speedLimit-forTrafficRule"] = {'lowerLimit':[], 'upperLimit':[]}
self.trace["road-forTrafficRule"] = {'honkingAllowed':[], 'crosswalkAhead':[], 'junctionAhead':[], 'stopSignAhead':[], 'signalAhead':[], 'stoplineAhead':[], 'streetLightOn':[]}
self.trace["specialLocationAhead-forTrafficRule"] = {'location':[], 'type':[]}
self.trace["trafficLightAhead-forTrafficRule"] = {'color':[], 'blink':[]}
self.trace["trafficLightAhead-arrow-forTrafficRule"] = dict() #not supported yet
self.trace["traffic-forTrafficRule"] = {'PriorityNPCAhead':[], 'PriorityPedsAhead':[],'isTrafficJam':[]}
self.trace["NPCAhead-forTrafficRule"] = {'Ahead':[], 'speed':[]}
self.trace["NearestNPC-forTrafficRule"] = {'Ahead':[], 'speed':[]}
self.trace["NPCOpposite-forTrafficRule"] = {'Ahead':[], 'speed':[]}
# self.trace["time-forTrafficRule"] = []
self.trace["trafficLightAhead-arrow-direction-forTrafficRule"] = { 'color': [], 'blink': []}
self.arrow_directions = ['forward', 'left', 'right', 'forwardOrLeft', 'forwardOrRight', 'Uturn']
for _item in self.arrow_directions:
self.trace["trafficLightAhead-arrow-forTrafficRule"][_item] = { 'color': [], 'blink': []}
self.extract()
def convert_velocity_to_speed(self, velocity):
x = velocity["x"]
y = velocity["y"]
z = velocity["z"]
return math.sqrt(x*x+y*y+z*z)
def append_boolean_value(self, original_data, position_to_append):
if original_data:
position_to_append.append(1)
else:
position_to_append.append(0)
def BUILD_single_traffic_rule_API(self, trace_state):
ego = trace_state['ego']
truth = trace_state['truth']
ego_chasis = ego['Chasis']
ego_currentLane = ego['currentLane']
traffic_light = trace_state['traffic_lights']
# hour = ego_chasis['hours']
# minute = ego_chasis['minutes']
# self.trace["time-forTrafficRule"].append( int(hour)*60+ int(minute))
self.append_boolean_value(ego_chasis['highBeamOn'], self.trace['ego-forTrafficRule']['highBeamOn'])
self.append_boolean_value(ego_chasis['lowBeamOn'], self.trace['ego-forTrafficRule']['lowBeamOn'])
self.append_boolean_value(ego_chasis['turnSignal'], self.trace['ego-forTrafficRule']['turnSignal'])
self.trace['ego-forTrafficRule']['fogLightOn'].append(0) #not support for apollo at now stage
self.append_boolean_value(ego_chasis['hornOn'], self.trace['ego-forTrafficRule']['hornOn'])
self.trace['ego-forTrafficRule']['warningflashOn'].append(0) #not support for apollo at now stage
self.trace['ego-forTrafficRule']['gear'].append(ego_chasis['gear'])
self.append_boolean_value(ego_chasis['engineOn'], self.trace['ego-forTrafficRule']['engineOn'])
self.trace['ego-forTrafficRule']['direction'].append(ego['planning_of_turn'])
if ego_chasis['error_code'] == '3' or ego_chasis['error_code'] == 3:
self.trace['ego-forTrafficRule']['manualIntervention'].append(1)
else:
self.trace['ego-forTrafficRule']['manualIntervention'].append(0)
# self.trace['ego-forTrafficRule']['highBeamOn'].append(ego_chasis['highBeamOn'])
# self.trace['ego-forTrafficRule']['lowBeamOn'].append(ego_chasis['lowBeamOn'])
# self.trace['ego-forTrafficRule']['turnSignal'].append(ego_chasis['turnSignal'])
# self.trace['ego-forTrafficRule']['fogLightOn'].append(0)
# self.trace['ego-forTrafficRule']['hornOn'].append(ego_chasis['hornOn'])
# self.trace['ego-forTrafficRule']['warningFlashOn'].append(0)
# self.trace['ego-forTrafficRule']['gear'].append(ego_chasis['gear'])
# self.trace['ego-forTrafficRule']['engineOn'].append(ego_chasis['engineOn'])
# self.trace['ego-forTrafficRule']['direction'].append(ego_chasis['direction'])
# speed_of_ego = self.convert_velocity_to_speed(ego['pose']['linearVelocity'])
# print(speed_of_ego)
speed_of_ego = 3.6*ego_chasis['speed']
# print(speed_of_ego)
acc_of_ego = self.convert_velocity_to_speed(ego['pose']['linearAcceleration'])
self.trace["ego-driving-forTrafficRule"]['speed'].append(speed_of_ego)
self.trace["ego-driving-forTrafficRule"]['acc'].append(acc_of_ego)
self.trace["ego-driving-forTrafficRule"]['brake'].append(ego_chasis['brake'])
self.append_boolean_value(ego['isLaneChanging'], self.trace["ego-driving-forTrafficRule"]['isLaneChanging'])
self.append_boolean_value(ego['isOverTaking'], self.trace["ego-driving-forTrafficRule"]['isOverTaking'])
self.append_boolean_value(ego['isTurningAround'], self.trace["ego-driving-forTrafficRule"]['isTurningAround'])
# self.trace["ego-driving-forTrafficRule"]['isLaneChanging'].append(ego['isLaneChanging'])
# self.trace["ego-driving-forTrafficRule"]['isOverTaking'].append(ego['isOverTaking'])
# self.trace["ego-driving-forTrafficRule"]['isTurningAround'].append(ego['isTurningAround'])
self.trace["currentlane-forTrafficRule"]['number'].append(ego_currentLane['number'])
if hasattr(ego_currentLane, 'turn'):
self.trace["currentlane-forTrafficRule"]['direction'].append(ego_currentLane['turn'])
else:
self.trace["currentlane-forTrafficRule"]['direction'].append(0)
max_speed = 1000
min_speed = 0
self.trace["speedLimit-forTrafficRule"]['lowerLimit'].append(min_speed)
self.trace["speedLimit-forTrafficRule"]['upperLimit'].append(max_speed)
self.trace["road-forTrafficRule"]['honkingAllowed'].append(1) #not support for apollo at now stage
self.trace["road-forTrafficRule"]['crosswalkAhead'].append(ego['crosswalkAhead'])
self.trace["road-forTrafficRule"]['junctionAhead'].append(ego['junctionAhead'])
self.trace["road-forTrafficRule"]['stopSignAhead'].append(ego['stopSignAhead'])
if traffic_light == {}:
self.trace["road-forTrafficRule"]['signalAhead'].append(0)
else:
self.append_boolean_value(traffic_light['containLights'], self.trace["road-forTrafficRule"]['signalAhead'])
self.trace["road-forTrafficRule"]['stoplineAhead'].append(ego['stoplineAhead'])
self.trace["road-forTrafficRule"]['streetLightOn'].append(0)
# self.trace["specialLocationAhead-forTrafficRule"] = {'location':[], 'type':[]}
self.trace["specialLocationAhead-forTrafficRule"]['location'].append(0)
self.trace["specialLocationAhead-forTrafficRule"]['type'].append(0)
if traffic_light == {}:
self.trace["trafficLightAhead-forTrafficRule"]['color'].append(3)
self.trace["trafficLightAhead-forTrafficRule"]['blink'].append(0)
else:
if traffic_light['containLights']:
_list = traffic_light['trafficLightList']
if len(_list) == 1:
current_signal = _list[0]
self.trace["trafficLightAhead-forTrafficRule"]['color'].append(current_signal['color'])
self.append_boolean_value(current_signal['blink'], self.trace["trafficLightAhead-forTrafficRule"]['blink'])
else:
print('warning: more than one traffic light, choose the closer one')
current_signal = _list[0]
self.trace["trafficLightAhead-forTrafficRule"]['color'].append(current_signal['color'])
self.append_boolean_value(current_signal['blink'], self.trace["trafficLightAhead-forTrafficRule"]['blink'])
else:
self.trace["trafficLightAhead-forTrafficRule"]['color'].append(3)
self.trace["trafficLightAhead-forTrafficRule"]['blink'].append(0)
self.trace["trafficLightAhead-arrow-direction-forTrafficRule"]['color'].append(3)
self.trace["trafficLightAhead-arrow-direction-forTrafficRule"]['blink'].append(0)
for _item in self.arrow_directions:
self.trace["trafficLightAhead-arrow-forTrafficRule"][_item]['color'].append(3)
self.trace["trafficLightAhead-arrow-forTrafficRule"][_item]['blink'].append(0)
self.append_boolean_value(ego['PriorityNPCAhead'], self.trace["traffic-forTrafficRule"]['PriorityNPCAhead'])
self.append_boolean_value(ego['PriorityPedsAhead'], self.trace["traffic-forTrafficRule"]['PriorityPedsAhead'])
self.append_boolean_value(ego['isTrafficJam'], self.trace["traffic-forTrafficRule"]['isTrafficJam'])
max_dis = 1000
min_dis = 0
name_of_NPCAhead = truth['NPCAhead']
name_of_NearestNPC = truth['NearestNPC']
name_of_NPCOpposite = truth['NPCOpposite']
for _i in truth['obsList']:
if _i['name'] == name_of_NPCAhead:
dist_to_NPCAhead = _i['distToEgo']
speed_to_NPCAhead = _i['speed']
if _i['name'] == name_of_NearestNPC:
dist_to_NearestNPC = _i['distToEgo']
speed_to_NearestNPC = _i['speed']
if _i['name'] == name_of_NPCOpposite:
dist_to_NPCOpposite = _i['distToEgo']
speed_to_NPCOpposite = _i['speed']
if name_of_NPCAhead != None:
self.trace["NPCAhead-forTrafficRule"]['Ahead'].append(dist_to_NPCAhead)
self.trace["NPCAhead-forTrafficRule"]['speed'].append(speed_to_NPCAhead)
else:
self.trace["NPCAhead-forTrafficRule"]['Ahead'].append(max_dis)
self.trace["NPCAhead-forTrafficRule"]['speed'].append(min_speed)
if name_of_NearestNPC != None:
self.trace["NearestNPC-forTrafficRule"]['Ahead'].append(dist_to_NearestNPC)
self.trace["NearestNPC-forTrafficRule"]['speed'].append(speed_to_NearestNPC)
else:
self.trace["NearestNPC-forTrafficRule"]['Ahead'].append(max_dis)
self.trace["NearestNPC-forTrafficRule"]['speed'].append(min_speed)
if name_of_NPCOpposite != None:
self.trace["NPCOpposite-forTrafficRule"]['Ahead'].append(dist_to_NPCOpposite)
self.trace["NPCOpposite-forTrafficRule"]['speed'].append(speed_to_NPCOpposite)
else:
self.trace["NPCOpposite-forTrafficRule"]['Ahead'].append(max_dis)
self.trace["NPCOpposite-forTrafficRule"]['speed'].append(min_speed)
def extract(self, sensing_range=100.0):
inf_dis = 1000.0
_trace_len = len(self.init_trace)
initial_time = self.init_trace[0]['timestamp']
for i in range(_trace_len):
trace_state = self.init_trace[i]
_state_time = (trace_state['timestamp'] - initial_time) /10
self.trace['time'].append(_state_time)
self.time.append(_state_time)
## extract traffic_rule related state
self.BUILD_single_traffic_rule_API(trace_state)
## extract the god-view information
ego_state = trace_state['ego']['pose']
# extract ego state
ego_state_position = np.array(list(ego_state['position'].values()))
ego_state_heading = ego_state['heading']
ego_state_velocity = np.array(list(ego_state['linearVelocity'].values()))
ego_state_acceleration = np.array(list(ego_state['linearAcceleration'].values()))
ego_state_polygon = get_ego_polygon(trace_state['ego'])
self.trace['ego']['position'].append(ego_state_position)
self.trace['ego']['velocity'].append(ego_state_velocity)
self.trace['ego']['heading'].append(ego_state_heading)
self.trace['ego']['acceleration'].append(ego_state_acceleration)
self.trace['ego']['shape'].append(ego_state_polygon)
# extract ground truth state
truth_state = trace_state['truth']['obsList']
truth_remaining_agent = self.agent.copy()
for k in range(len(truth_state)):
obs_k = truth_state[k]
if obs_k['name'] in self.agent:
truth_remaining_agent.remove(obs_k['name'])
obs_position = np.array(list(obs_k['position'].values())) # vector
obs_velocity = np.array(list(obs_k['velocity'].values())) # vector
obs_heading = obs_k['theta'] # float
obs_acceleration = np.array(list(obs_k['acceleration'].values()))
obs_polygon = polygon_point(obs_k['polygonPointList'])
dis2ego = obs_k['distToEgo']
self.trace['truth'][obs_k['name']]['position'].append(obs_position)
self.trace['truth'][obs_k['name']]['velocity'].append(obs_velocity)
self.trace['truth'][obs_k['name']]['heading'].append(obs_heading)
self.trace['truth'][obs_k['name']]['acceleration'].append(obs_acceleration)
self.trace['truth'][obs_k['name']]['shape'].append(obs_polygon)
self.distance['truth'][obs_k['name']].append(dis2ego)
if len(truth_remaining_agent): # obstacle is not within sensing range
for item in truth_remaining_agent:
self.trace['truth'][item]['position'].append(np.array([0, 0, 0]))
self.trace['truth'][item]['velocity'].append(np.array([0, 0, 0]))
self.trace['truth'][item]['heading'].append(0)
self.trace['truth'][item]['acceleration'].append(np.array([0, 0, 0]))
self.trace['truth'][item]['shape'].append([])
self.distance['truth'][item].append(inf_dis)
# extract perception state
if not self.is_groundtruth:
perception_state = trace_state['perception']['obsList']
remaining_agent = self.agent.copy()
for j in range(len(perception_state)):
obs_j = perception_state[j]
if obs_j['matchedGT'] in self.agent:
remaining_agent.remove(obs_j['matchedGT'])
obs_position_p = np.array(list(obs_j['position'].values()))
obs_velocity_p = np.array(list(obs_j['velocity'].values()))
obs_heading_p = obs_j['theta']
obs_acceleration_p = np.array(list(obs_j['acceleration'].values()))
obs_polygon_p = polygon_point(obs_j['polygonPointList'])
dis2ego_p = obs_j['distToEgo']
self.trace['perception'][obs_j['matchedGT']]['position'].append(obs_position_p)
self.trace['perception'][obs_j['matchedGT']]['velocity'].append(obs_velocity_p)
self.trace['perception'][obs_j['matchedGT']]['heading'].append(obs_heading_p)
self.trace['perception'][obs_j['matchedGT']]['acceleration'].append(obs_acceleration_p)
self.trace['perception'][obs_j['matchedGT']]['shape'].append(obs_polygon_p)
# self.trace['perception'][obs_j['matchedGT']].append({'position': obs_position_p,
# 'velocity': obs_velocity_p,
# 'heading': obs_heading_p,
# 'acceleration': obs_acceleration_p,
# 'shape': obs_polygon_p})
self.distance['perception'][obs_j['matchedGT']].append(dis2ego_p)
# For other objects not in perception results
if len(remaining_agent):
for item in remaining_agent:
if self.distance['truth'][item][i] <= sensing_range:
self.trace['perception'][item]['position'].append(None)
self.trace['perception'][item]['velocity'].append(None)
self.trace['perception'][item]['heading'].append(None)
self.trace['perception'][item]['acceleration'].append(None)
self.trace['perception'][item]['shape'].append([])
# self.trace['perception'][item].append((np.zeros(3), np.zeros(3), 0, [[0, 0, 0]]))
self.distance['perception'][item].append(self.distance['truth'][item][i]) # todo: determine a more suitable distance
else:
self.trace['perception'][item]['position'].append(np.array([inf_dis, inf_dis, inf_dis]))
self.trace['perception'][item]['velocity'].append(np.array([0, 0, 0]))
self.trace['perception'][item]['heading'].append(0)
self.trace['perception'][item]['acceleration'].append(np.array([0, 0, 0]))
self.trace['perception'][item]['shape'].append([])
self.distance['perception'][item].append(inf_dis)
# compute perception difference for each agent
for item in self.agent:
if self.distance['truth'][item][i] <= sensing_range:
jj = 0
perception_result = dict()
for jj in range(len(perception_state)): # for each detected obstacle obs_jj
obs_jj = perception_state[jj]
if obs_jj['matchedGT'] == item:
perception_result = obs_jj
break
if jj == len(perception_state): # the obstacle item is not detected in the sensing range
self.perception_diff[item]['type'][i] = 1.0
self.perception_diff[item]['position'][i] = inf_dis
self.perception_diff[item]['velocity'][i] = inf_dis
self.perception_diff[item]['heading'][i] = inf_dis
self.perception_diff[item]['shape'][i] = 0
continue
for k in range(len(truth_state)): # obtain the ground truth of item
obs_k = truth_state[k]
if obs_k['name'] == item:
truth_result = obs_k
break
# type check
if perception_result['typeName'] == truth_result['typeName']:
type_error = 0.0
else:
type_error = 1.0
# position check
position_error = point2norm(perception_result['position'], truth_result['position'])
velocity_error = point2norm(perception_result['velocity'], truth_result['velocity'])
heading_error = np.abs(perception_result['theta'] - truth_result['theta'])
polygon1 = Polygon(polygon_point(perception_result['polygonPointList']))
polygon2 = Polygon(polygon_point(truth_result['polygonPointList']))
shape_error = polygon1.intersection(polygon2).area / polygon2.area
self.perception_diff[item]['type'][i] = type_error
self.perception_diff[item]['position'][i] = position_error
self.perception_diff[item]['velocity'][i] = velocity_error
self.perception_diff[item]['heading'][i] = heading_error
self.perception_diff[item]['shape'][i] = shape_error
else:
for item in self.agent:
self.distance['perception'][item].append(self.distance['truth'][item][i])
# self.trace['perception'][item].append(self.trace['truth'][item][i])
self.trace['perception'][item]['position'].append(self.trace['truth'][item]['position'][i])
self.trace['perception'][item]['velocity'].append(self.trace['truth'][item]['velocity'][i])
self.trace['perception'][item]['heading'].append(self.trace['truth'][item]['heading'][i])
self.trace['perception'][item]['acceleration'].append(self.trace['truth'][item]['acceleration'][i])
self.trace['perception'][item]['shape'].append(self.trace['truth'][item]['shape'][i])
if __name__ == "__main__":
output_file = 'data/result01.json'
with open(output_file) as f:
data = json.load(f) # read as a msg from apollo via websocket
trace = Trace(data)
# print(len(trace.trace['ego-forTrafficRule']['highBeamOn']))
print(trace.trace['time'])
print()
print(trace.trace['ego-forTrafficRule'])
print()
print(trace.trace['ego-driving-forTrafficRule'])
print()
print(trace.trace['currentlane-forTrafficRule'])
print()
print(trace.trace['speedLimit-forTrafficRule'])
print()
print(trace.trace['road-forTrafficRule'])
print()
print(trace.trace['specialLocationAhead-forTrafficRule'])
print()
print(trace.trace['trafficLightAhead-forTrafficRule'])
print()
print(trace.trace['traffic-forTrafficRule'])
print()
print(trace.trace['NPCAhead-forTrafficRule'])
print()
print(trace.trace['NearestNPC-forTrafficRule'])
print()
# print(trace.trace['perception']['npc1'][34])
# print(trace.trace['truth']['npc1'][34])
# print(trace.distance['perception']['npc1'][34])
# print(trace.distance['truth']['npc1'][34])
# print(len(trace.trace['perception']['npc1']))
# p1 = {'x': 1.0, 'y': 2.0, 'z': 3.0}
# p2 = {'x': 2.0, 'y': 3.0, 'z': 4.0}
# print(point2norm(p1, p2))
# ego_state = { "size": {
# "length": 4.7,
# "width": 2.06
# },
# "pose": {
# "position": {
# "x": 552829.3482471938,
# "y": 4183198.3280027835,
# "z": 10.124656677246094
# },
# "orientation": {
# "qx": -5.909786864322086e-07,
# "qy": -4.6492385052943064e-08,
# "qz": 0.4581255614757538,
# "qw": 0.8888875842094421
# },
# "linearVelocity": {
# "x": 1.073107159754727e-05,
# "y": -8.155493560479954e-06,
# "z": -2.4668872356414795e-05
# },
# "linearAcceleration": {
# "x": -4.7755669017582286e-05,
# "y": -2.7896752591041223e-05,
# "z": 9.809666633611167
# },
# "angularVelocity": {
# "x": -3.4123785269863597e-06,
# "y": -4.314480573963567e-06,
# "z": 1.2359062541833489e-09
# },
# "heading": 2.522566710369909,
# "linearAccelerationVrf": {
# "x": -5.493117350852117e-05,
# "y": 1.198328027385287e-05,
# "z": 9.809666633605957
# },
# "angularVelocityVrf": {
# "x": -5.493908247444779e-06,
# "y": 2.7574944283514924e-07,
# "z": 1.2336869303908315e-09
# },
# "eulerAngles": {
# "x": -1.0080285838030164e-06,
# "y": -6.241378660286242e-07,
# "z": 0.9517705564328693
# },
# "lane_position": {
# "lane": "lane_231",
# "offset": 94.80424083205679
# }
# },
# "lane": "lane_231",
# "laneOffset": 94.80424083205679,
# "road": "road_230_231",
# "junction": None
# }
# ego_polygon = get_ego_polygon(ego_state)
# print(ego_polygon)
# file_name = 'result-trace.json'
# agent_name = ['npc1']
# with open(file_name) as f:
# data = json.load(f)
# # trace_data = data['trace']
# trace = Trace(data)
# print('distance sequence to the ego vehicle: {}'.format(trace.distance))
# print('perception difference: {}'.format(trace.perception_diff))
# print(len(trace.trace['truth']['npc1']))
# print(trace.trace['ego'][34])
# print(trace.trace['perception']['npc1'][34])
# print(trace.trace['truth']['npc1'][34])
# print(trace.distance['perception']['npc1'][34])
# print(trace.distance['truth']['npc1'][34])
# print(len(trace.trace['perception']['npc1']))