-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathextract_data.py
141 lines (117 loc) · 5.87 KB
/
extract_data.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
import json
import os
import re
def extract_data(root_path, save_path):
with open(root_path, 'r') as f :#, \
train_file = json.load(f)
test_data=dict()
# TODO: convert the data into test data, containing the importance, multiple choice questions, graph questions
for scene_id in train_file.keys():
scene_data = train_file[scene_id]['key_frames']
# for test file
test_data[scene_id] = dict()
test_data[scene_id]['key_frames'] = dict()
for frame_id in scene_data.keys():
frame_data_infos = scene_data[frame_id]['key_object_infos']
frame_data_qa = scene_data[frame_id]['QA']
image_paths = scene_data[frame_id]['image_paths']
# for test file
test_data[scene_id]['key_frames'][frame_id] = dict()
# test_data[scene_id]['key_frames'][frame_id]['key_object_infos'] = frame_data_infos
test_data[scene_id]['key_frames'][frame_id]['QA'] = dict()
test_data[scene_id]['key_frames'][frame_id]['image_paths'] = image_paths
test_data[scene_id]['key_frames'][frame_id]['QA']['perception'] = []
test_data[scene_id]['key_frames'][frame_id]['QA']['prediction'] = []
test_data[scene_id]['key_frames'][frame_id]['QA']['planning'] = []
test_data[scene_id]['key_frames'][frame_id]['QA']['behavior'] = []
# get the classes of the important objects
classes = []
for obj_id in frame_data_infos.keys():
obj_data = frame_data_infos[obj_id]
classes.append(obj_data['Visual_description'].split('.')[0])
print(classes)
# get the location of the important objects
locations = []
for obj_id in frame_data_infos.keys():
locations.append(obj_id)
print(locations)
# get the questions and answers of the perception
perception = frame_data_qa["perception"]
prediction = frame_data_qa["prediction"]
planning = frame_data_qa["planning"]
behavior = frame_data_qa["behavior"]
for qa in perception:
question = qa['Q']
answer = qa['A']
# according to the classes to select the corresponding question
flag = 1
for cl in classes:
if cl.lower() not in answer.lower():
flag = 0
if flag == 1:
qa['tag'] = [2]
test_data[scene_id]['key_frames'][frame_id]['QA']['perception'].append(qa)
break
# get the multiple choice questions and answers
for qa in perception:
question = qa['Q']
answer = qa['A']
if "What is the moving status of object".lower() in question.lower():
qa['tag'] = [0]
test_data[scene_id]['key_frames'][frame_id]['QA']['perception'].append(qa)
break
# get the graph questions and answers
for qa in prediction:
question = qa['Q']
answer = qa['A']
# according to the location to select the corresponding question
flag = 1
for loc in locations:
if loc.lower() not in answer.lower():
flag = 0
if flag == 1:
qa['tag'] = [3]
test_data[scene_id]['key_frames'][frame_id]['QA']['prediction'].append(qa)
break
# get the yes or no questions and answers
for qa in prediction:
question = qa['Q']
answer = qa['A']
if "yes" in answer.lower() or "no" in answer.lower():
qa['tag'] = [0]
test_data[scene_id]['key_frames'][frame_id]['QA']['prediction'].append(qa)
break
# get the three questions from the planning "safe actions", "collision", ""
actions_question_added = False
collision_question_added = False
safe_actions_question_added = False
for qa in planning:
question = qa['Q']
answer = qa['A']
if "What actions could the ego vehicle take".lower() in question.lower() and not actions_question_added:
qa['tag'] = [1]
test_data[scene_id]['key_frames'][frame_id]['QA']['planning'].append(qa)
actions_question_added = True
if "lead to a collision" in question.lower() and not collision_question_added:
qa['tag'] = [1]
test_data[scene_id]['key_frames'][frame_id]['QA']['planning'].append(qa)
collision_question_added = True
if "safe actions" in question.lower() and not safe_actions_question_added:
qa['tag'] = [1]
test_data[scene_id]['key_frames'][frame_id]['QA']['planning'].append(qa)
safe_actions_question_added = True
# Check if all question types have been added and exit the loop
if actions_question_added and collision_question_added and safe_actions_question_added:
break
for qa in behavior:
question = qa['Q']
answer = qa['A']
qa['tag'] = [0]
test_data[scene_id]['key_frames'][frame_id]['QA']['behavior'].append(qa)
with open(save_path, 'w') as f:
json.dump(test_data, f, indent=4)
if __name__ == "__main__":
# extract the data from the training json file
root_path = "data/train_sample.json"
save_path = "test.json"
extract_data(root_path, save_path)