File size: 7,855 Bytes
0343ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
    This is a demo script that was used to take the default dynamicWorld.json and extract every vehicle with its related objects and sorts them 
    to a new json file for each ego vehicle.
    This allows an analysis of each maneuver without accessing other vehicles in the dynamicWorld. 
    Please note that this will generate a lot of redundant information. 

"""
from openautomatumdronedata.dataset import droneDataset
import json 
import os
import shutil
import sys

# Get all present recording folders in the current dataset 
dataset_folders = list()
current_path = os.path.abspath(os.path.join(__file__ ,"../.."))
for item in os.listdir(current_path):
    if os.path.isdir(os.path.join(current_path, item)) and item != "img":
        dataset_folders.append(item)



for recording_folder in dataset_folders:
    path = os.path.join(current_path, recording_folder)

    # Create an output folder
    export_path = os.path.join(current_path, recording_folder, "export_single_objects")
    if not os.path.exists(export_path):
        os.mkdir(export_path)

    # Now we open each dataset and create a droneDataset object for it
    dataset = droneDataset(path)

    # Here we access the dynamic world, the global JSON file containing all recording infromation
    dynWorld = dataset.dynWorld  

    # Here we open the plain JSON file without the automatum pip utility in parallel
    f = open(os.path.join(path, "dynamicWorld.json"))
    json_dict = json.load(f)

    # Create a new dict to store all agregated values in
    relation_dict = dict()

    # Lets take every object (car, truck, etc.) from the plain JSON file and crate a new JSON containing only this object with all its surrounding objects
    for object in json_dict["objects"]:
        """ 
            Now we access the object_relation_dict_list, ttc_dict and tth_dict of the object to see which objects are the surrounding ones:
                "object_relation_dict_list": [
                {
                    "front_ego": null,
                    "behind_ego": "32499e60-30e9-4f41-8dc4-8699364db5dc",
                    "front_left": null,
                    "behind_left": null,
                    "front_right": "3e67c856-116a-4af5-96cc-39f5002f71a0",
                    "behind_right": "3002eaf3-a545-4e56-aa31-557f25e79643"
                },
                ...

                "ttc_dict_vec": [
                {
                    "front_ego": -1,
                    "behind_ego": null,
                    "front_left": null,
                    "behind_left": null,
                    "front_right": 477.62466112341815,
                    "behind_right": null
                },
                ...

                "tth_dict_vec": [
                {
                    "front_ego": null,
                    "behind_ego": 0.380621726114513,
                    "front_left": null,
                    "behind_left": null,
                    "front_right": -1,
                    "behind_right": 3.687973804225473
                },
                ...
        """
        for i, (object_relation_dict, ttc_dict, tth_dict, lat_dict, long_dict) in enumerate(zip(object["object_relation_dict_list"], object["ttc_dict_vec"], object["tth_dict_vec"], object["lat_dist_dict_vec"], object["long_dist_dict_vec"])):
            time_stamp = object["time"][i]
            for key in object_relation_dict.keys():  # key = "front_ego", relation = "UUID of the object in this position" for example
                relation = object_relation_dict[key]
                if relation is not None:  # Check if there is an object at this position at all
                    relation_object = dynWorld.get_dynObj_by_UUID(relation)  # Access the object with the automatum utility by its UUID
                    time_idx = relation_object.next_index_of_specific_time(time_stamp)  # Get the time index of the object for the time stamp of our current ego vehicle we generate the new JSON for


                    # Copy all values from this object at the specific time
                    relation_dict["UUID"] = relation_object.UUID
                    relation_dict["length"] = relation_object.length
                    relation_dict["width"] = relation_object.width
                    relation_dict["x"] = relation_object.x_vec[time_idx]
                    relation_dict["y"] = relation_object.y_vec[time_idx]
                    relation_dict["vx"] = relation_object.vx_vec[time_idx]
                    relation_dict["vy"] = relation_object.vy_vec[time_idx]
                    relation_dict["ax"] = relation_object.ax_vec[time_idx]
                    relation_dict["ay"] = relation_object.ay_vec[time_idx]
                    relation_dict["jerk_x"] = relation_object.vx_vec[time_idx]
                    relation_dict["jerk_y"] = relation_object.vx_vec[time_idx]
                    relation_dict["curvature"] = relation_object.vx_vec[time_idx]
                    relation_dict["psi"] = relation_object.psi_vec[time_idx]
                    relation_dict["lane_id"] = relation_object.lane_id_vec[time_idx]
                    relation_dict["road_id"] = relation_object.road_id_vec[time_idx]
                    relation_dict["road_type"] = relation_object.vx_vec[time_idx]
                    relation_dict["distance_left_lane_marking"] = relation_object.distance_left_lane_marking[time_idx]
                    relation_dict["distance_right_lane_marking"] = relation_object.distance_right_lane_marking[time_idx]
                    relation_dict["ttc"] = ttc_dict[key]
                    relation_dict["tth"] = tth_dict[key]
                    relation_dict["lat_dist"] = lat_dict[key]
                    relation_dict["long_dist"] = long_dict[key]

                else:
                    relation_dict = None

                 
                """
                    Now we replace the initial single UUID of the object with all information we accumulated about the object behind the UUID

                        "object_relation_dict_list": [
                        {
                            "front_left": 0decabdc-fa4f-4f25-93ed-88eed734bba0,
                        ...

                        "object_relation_dict_list": [
                        {
                            "front_left": {
                                "UUID": "0decabdc-fa4f-4f25-93ed-88eed734bba0",
                                "length": 4.172288426073395,
                                "width": 1.8141249203213998,
                                "vx": 46.54388406290268,
                                "vy": 0.005328263922638854,
                                "ax": 0.5608367460027531,
                                "ay": -0.5516711364613421,
                                "psi": -0.5643746012832805,
                                "x": 47.834595023288536,
                                "y": -32.82371510377445,
                                "lane_id": 3,
                                "road_id": 0,
                                "distance_left_lane_marking": 2.3102357971463827,
                                "distance_right_lane_marking": 1.6230526113559351
                            },
                        ---

                """
                object["object_relation_dict_list"][i][key] = relation_dict

                relation_dict = dict()  # Delete the dict for the next object


        # Delete redundant information
        del object["ttc_dict_vec"]
        del object["tth_dict_vec"]
        del object["lat_dist_dict_vec"]
        del object["long_dist_dict_vec"]

        # Finally we save each object as its own JSON    
        with open(os.path.join(export_path, object["UUID"] + ".json"), "w") as outfile:
            json.dump(object, outfile)
        print("Successfully exported object %s" % object["UUID"])