|
| 1 | +// |
| 2 | +// Copyright Institute of Automotive Engineering |
| 3 | +// of Technical University of Darmstadt 2020. |
| 4 | +// Licensed under the EUPL-1.2-or-later |
| 5 | +// |
| 6 | +// This work covered by the EUPL can be used/merged and distributed |
| 7 | +// in other works covered by GPL-2.0, GPL-3.0, LGPL, AGPL, CeCILL, |
| 8 | +// OSL, EPL, MPL and other licences listed as compatible in the EUPL |
| 9 | +// Appendix. This applies to the other (combined) work, while the |
| 10 | +// original project stays covered by the EUPL without re-licensing. |
| 11 | +// |
| 12 | +// Alternatively, the contents of this file may be used under the |
| 13 | +// terms of the Mozilla Public License, v. 2.0. If a copy of the MPL |
| 14 | +// was not distributed with this file, you can obtain one at |
| 15 | +// http://mozilla.org/MPL/2.0/. |
| 16 | +// |
| 17 | + |
| 18 | +#ifndef _USE_MATH_DEFINES |
| 19 | +#define _USE_MATH_DEFINES |
| 20 | +#endif |
| 21 | + |
| 22 | +#include "csvoutputdetectedobjects/CsvOutputDetectedObjects.hpp" |
| 23 | +#include <fstream> |
| 24 | +#include <iostream> |
| 25 | +#include <ctime> |
| 26 | + |
| 27 | +#ifdef _WIN32 |
| 28 | + #include <math.h> |
| 29 | + #include <direct.h> |
| 30 | +#else |
| 31 | + #include <cmath> |
| 32 | + #include <sys/stat.h> |
| 33 | +#endif |
| 34 | + |
| 35 | +using namespace model; |
| 36 | +using namespace osi3; |
| 37 | + |
| 38 | +void model::CsvOutputDetectedObjects::apply(SensorData &sensor_data) { |
| 39 | + log("Starting .csv output for detected objects"); |
| 40 | + |
| 41 | + if ((sensor_data.sensor_view_size()==0) || (!sensor_data.sensor_view(0).has_global_ground_truth())) { |
| 42 | + log("No sensor view or global ground truth"); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const auto &ground_truth = sensor_data.sensor_view(0).global_ground_truth(); |
| 47 | + |
| 48 | + auto time_nanos = ground_truth.timestamp().nanos(); |
| 49 | + auto time_seconds = ground_truth.timestamp().seconds(); |
| 50 | + double timestamp = (double) time_seconds + (double) time_nanos / 1000000000; |
| 51 | + |
| 52 | + auto no_of_moving_objects = sensor_data.moving_object_size(); |
| 53 | + auto no_of_stationary_objects = sensor_data.stationary_object_size(); |
| 54 | + auto no_of_objects = no_of_moving_objects + no_of_stationary_objects; |
| 55 | + |
| 56 | + /// Add tracking_id, x, y, z, roll, pitch, yaw, width, length, height, is_moving, existence_probability to csv |
| 57 | + if (no_of_objects > 0) { |
| 58 | + |
| 59 | + /// Write header line of .csv on first call |
| 60 | + if (first_call){ |
| 61 | + #include <csvoutputdetectedobjects/set_csv_file_path_detectedobjects.cpp> |
| 62 | + write_first_line_to_CSV(file_path_detectedobjects); |
| 63 | + first_call = false; |
| 64 | + } |
| 65 | + |
| 66 | + for (const auto &moving_object : sensor_data.moving_object()) { |
| 67 | + write_data_to_CSV(file_path_detectedobjects, |
| 68 | + timestamp, |
| 69 | + moving_object.header().tracking_id().value(), |
| 70 | + std::round(moving_object.base().position().x() * 1000) / 1000, |
| 71 | + std::round(moving_object.base().position().y() * 1000) / 1000, |
| 72 | + std::round(moving_object.base().position().z() * 1000) / 1000, |
| 73 | + std::round(moving_object.base().orientation().roll() * 1000) / 1000, |
| 74 | + std::round(moving_object.base().orientation().pitch() * 1000) / 1000, |
| 75 | + std::round(moving_object.base().orientation().yaw() * 1000) / 1000, |
| 76 | + std::round(moving_object.base().dimension().width() * 1000) / 1000, |
| 77 | + std::round(moving_object.base().dimension().length() * 1000) / 1000, |
| 78 | + std::round(moving_object.base().dimension().height() * 1000) / 1000, |
| 79 | + true, |
| 80 | + std::round(moving_object.header().existence_probability() * 1000) / 1000); |
| 81 | + } |
| 82 | + for (const auto &stationary_object : sensor_data.stationary_object()) { |
| 83 | + write_data_to_CSV(file_path_detectedobjects, |
| 84 | + timestamp, |
| 85 | + stationary_object.header().tracking_id().value(), |
| 86 | + std::round(stationary_object.base().position().x() * 1000) / 1000, |
| 87 | + std::round(stationary_object.base().position().y() * 1000) / 1000, |
| 88 | + std::round(stationary_object.base().position().z() * 1000) / 1000, |
| 89 | + std::round(stationary_object.base().orientation().roll() * 1000) / 1000, |
| 90 | + std::round(stationary_object.base().orientation().pitch() * 1000) / 1000, |
| 91 | + std::round(stationary_object.base().orientation().yaw() * 1000) / 1000, |
| 92 | + std::round(stationary_object.base().dimension().width() * 1000) / 1000, |
| 93 | + std::round(stationary_object.base().dimension().length() * 1000) / 1000, |
| 94 | + std::round(stationary_object.base().dimension().height() * 1000) / 1000, |
| 95 | + false, |
| 96 | + std::round(stationary_object.header().existence_probability() * 1000) / 1000); |
| 97 | + } |
| 98 | + } else { |
| 99 | + log("No detected objects for .csv output at timestamp " + std::to_string(timestamp)); |
| 100 | + return; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void CsvOutputDetectedObjects::write_first_line_to_CSV(const std::string& path) { |
| 105 | + std::fstream my_file; |
| 106 | + my_file.open(path, std::ios::app); |
| 107 | + my_file << "timestamp_in_s, tracking_id, x_in_m, y_in_m, z_in_m, roll_in_deg, pitch_in_deg, yaw_in_deg, width_in_m, length_in_m, height_in_m, is_moving, existence_probability" << std::endl; |
| 108 | + my_file.close(); |
| 109 | +} |
| 110 | + |
| 111 | +void |
| 112 | +CsvOutputDetectedObjects::write_data_to_CSV(const std::string &path, double timestamp, size_t tracking_id, double x, double y, double z, double roll, double pitch, double yaw, double width, double length, |
| 113 | + double height, bool is_moving, double existence_probability) { |
| 114 | + std::fstream my_file; |
| 115 | + my_file.open(path, std::ios::app); |
| 116 | + my_file << timestamp << ", " << tracking_id << ", " << x << ", " << y << ", " << z << ", " << roll << ", " << pitch << ", " << yaw << ", " << width << ", " << length << ", " << height << ", " << (is_moving ? "true": "false") << ", " << existence_probability << std::endl; |
| 117 | + my_file.close(); |
| 118 | +} |
0 commit comments