converter-csv.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3 
4 
5 #include "converter-csv.hpp"
6 #include <iomanip>
7 #include <algorithm>
8 #include <iostream>
9 
10 using namespace rs2::tools::converter;
11 
13  unsigned long long frame_number,
14  long long frame_ts, long long backend_ts, long long arrival_time,
15  double p1, double p2, double p3,
16  double p4, double p5, double p6, double p7) :
17  _stream_type(stream_type),
18  _stream_idx(stream_index),
19  _frame_number(frame_number),
20  _frame_ts(frame_ts),
21  _backend_ts(backend_ts),
22  _arrival_time(arrival_time),
23  _params({ p1, p2, p3, p4, p5, p6, p7 }){}
24 
26 {
27  std::stringstream ss;
28  ss << rs2_stream_to_string(_stream_type) << "," << _frame_number << ","
29  << std::fixed << std::setprecision(3) << _frame_ts << "," << _backend_ts << "," << _arrival_time;
30 
31  // IMU and Pose frame hold the sample data in addition to the frame's header attributes
32  size_t specific_attributes = 0;
34  specific_attributes = 3;
36  specific_attributes = 7;
37 
38  for (auto i = 0; i < specific_attributes; i++)
39  ss << "," << _params[i];
40  ss << std::endl;
41  return ss.str().c_str();
42 }
43 
44 converter_csv::converter_csv(const std::string& filePath, rs2_stream streamType)
45  : _filePath(filePath)
46  , _streamType(streamType)
48  , _sub_workers_joined(false)
49  , _m()
50  , _cv()
51 {
52 }
53 
55 {
56  if (frames_map_get_and_set(depthframe.get_profile().stream_type(), depthframe.get_frame_number())) {
57  return;
58  }
59 
61  [this, &depthframe] {
62 
63  std::stringstream filename;
64  filename << _filePath
65  << "_" << depthframe.get_profile().stream_name()
66  << "_" << std::setprecision(14) << std::fixed << depthframe.get_timestamp()
67  << ".csv";
68 
69  std::stringstream metadata_file;
70  metadata_file << _filePath
71  << "_" << depthframe.get_profile().stream_name()
72  << "_metadata_" << std::setprecision(14) << std::fixed << depthframe.get_timestamp()
73  << ".txt";
74 
75  std::string filenameS = filename.str();
76  std::string metadataS = metadata_file.str();
77 
79  [filenameS, metadataS, depthframe] {
80  std::ofstream fs(filenameS, std::ios::trunc);
81 
82  if (fs) {
83  for (int y = 0; y < depthframe.get_height(); y++) {
84  auto delim = "";
85 
86  for (int x = 0; x < depthframe.get_width(); x++) {
87  fs << delim << depthframe.get_distance(x, y);
88  delim = ",";
89  }
90  fs << '\n';
91  }
92  fs.flush();
93  }
94  metadata_to_txtfile(depthframe, metadataS);
95  });
97  });
98 }
99 
101 {
102  auto t = time(nullptr);
103  char buffer[20] = {};
104  const tm* time = localtime(&t);
105  if (nullptr != time)
106  strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", time);
107 
108  return std::string(buffer);
109 }
110 
112 {
113  if (!_imu_pose_collection.size())
114  {
115  std::cout << "No imu or pose data collected" << std::endl;
116  return;
117  }
118 
119  // Report amount of frames collected
120  std::vector<uint64_t> frames_per_stream;
121  for (const auto& kv : _imu_pose_collection)
122  frames_per_stream.emplace_back(kv.second.size());
123 
124  std::sort(frames_per_stream.begin(), frames_per_stream.end());
125 
126  // Serialize and store data into csv-like format
127  static auto time_Str = get_time_string();
128  std::stringstream filename;
129  filename << _filePath << "_" << time_Str <<"_imu_pose.csv";
130  std::ofstream csv(filename.str());
131  if (!csv.is_open())
132  throw std::runtime_error(stringify() << "Cannot open the requested output file " << _filePath << ", please check permissions");
133 
134  for (const auto& elem : _imu_pose_collection)
135  {
136  csv << "\n\nStream Type,F#,HW Timestamp (ms),Backend Timestamp(ms),Host Timestamp(ms)"
137  << (val_in_range(elem.first.first, { RS2_STREAM_GYRO,RS2_STREAM_ACCEL }) ? ",3DOF_x,3DOF_y,3DOF_z" : "")
138  << (val_in_range(elem.first.first, { RS2_STREAM_POSE }) ? ",t_x,t_y,t_z,r_x,r_y,r_z,r_w" : "")
139  << std::endl;
140 
141  for (auto i = 0; i < elem.second.size(); i++)
142  csv << elem.second[i].to_string();
143  }
144 
145 }
146 
148 {
150  return;
151  }
152 
153  start_worker(
154  [this, f] {
155 
157  [this, f] {
158  auto stream_uid = std::make_pair(f.get_profile().stream_type(),
159  f.get_profile().stream_index());
160 
161  long long frame_timestamp = 0LL;
164 
165  long long backend_timestamp = 0LL;
168 
169  long long time_of_arrival = 0LL;
172 
175  f.get_frame_number(),
177  backend_timestamp,
178  time_of_arrival};
179 
180  if (auto motion = f.as<rs2::motion_frame>())
181  {
182  auto axes = motion.get_motion_data();
183  record._params = { axes.x, axes.y, axes.z };
184  }
185 
186  if (auto pf = f.as<rs2::pose_frame>())
187  {
188  auto pose = pf.get_pose_data();
189  record._params = { pose.translation.x, pose.translation.y, pose.translation.z,
190  pose.rotation.x,pose.rotation.y,pose.rotation.z,pose.rotation.w };
191  }
192 
193  _imu_pose_collection[stream_uid].emplace_back(record);
194  });
196  _sub_workers_joined = true;
197  _cv.notify_all();
198  });
199  std::unique_lock<std::mutex> lck(_m);
200  while (!_sub_workers_joined)
201  _cv.wait(lck);
203 }
204 
206 {
208  return;
209 
210  auto depthframe = frame.as<rs2::depth_frame>();
211  if (depthframe)
212  {
213  convert_depth(depthframe);
214  return;
215  }
216 
217  auto motionframe = frame.as<rs2::motion_frame>();
218  auto poseframe = frame.as<rs2::pose_frame>();
219  if (motionframe || poseframe)
220  {
221  convert_motion_pose(frame);
222  return;
223  }
224 }
GLint y
def axes(out, pos, rotation=np.eye(3), size=0.075, thickness=2)
std::string stream_name() const
Definition: rs_frame.hpp:113
double get_timestamp() const
Definition: rs_frame.hpp:476
GLenum GLfloat * buffer
std::string stringify(const T &e)
Definition: catch.hpp:1648
void sort(sort_type m_sort_type, const std::string &in, const std::string &out)
::realsense_legacy_msgs::pose_< std::allocator< void > > pose
Definition: pose.h:88
GLdouble t
float get_distance(int x, int y) const
Definition: rs_frame.hpp:837
std::ostream & cout()
bool frames_map_get_and_set(rs2_stream streamType, frame_number_t frameNumber)
Definition: converter.cpp:31
GLdouble x
converter_csv(const std::string &filePath, rs2_stream streamType=rs2_stream::RS2_STREAM_ANY)
motion_pose_frame_record(rs2_stream stream_type, int stream_index, unsigned long long frame_number, long long frame_ts, long long backend_ts, long long arrival_time, double p1=0., double p2=0., double p3=0., double p4=0., double p5=0., double p6=0., double p7=0.)
T as() const
Definition: rs_frame.hpp:582
void convert(rs2::frame &frame) override
int stream_index() const
Definition: rs_frame.hpp:34
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
int get_height() const
Definition: rs_frame.hpp:673
unsigned long long get_frame_number() const
Definition: rs_frame.hpp:523
void metadata_to_txtfile(const rs2::frame &frm, const std::string &filename)
Definition: converter.cpp:9
int get_width() const
Definition: rs_frame.hpp:661
const char * rs2_stream_to_string(rs2_stream stream)
Definition: to-string.cpp:635
GLdouble f
std::map< std::pair< rs2_stream, int >, std::vector< motion_pose_frame_record > > _imu_pose_collection
int i
void convert_depth(rs2::depth_frame &depthframe)
rs2_metadata_type get_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:499
bool val_in_range(const T &val, const std::initializer_list< T > &list)
bool supports_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:511
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
stream_profile get_profile() const
Definition: rs_frame.hpp:559


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:43:16