automatum-data-crossing / example_scripts /example_export_objects.py
AutomatumPaul's picture
Upload automatum_data_crossing — Sample_Data, docs, examples, archive
0343ffa verified
"""
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"])