RealSenseBagReader.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2019 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include <chrono>
28 #include <fstream>
29 #include <iostream>
30 #include <memory>
31 #include <string>
32 #include <thread>
33 
34 #include "open3d/Open3D.h"
35 
36 using namespace open3d;
37 namespace sc = std::chrono;
38 
39 void PrintUsage() {
40  PrintOpen3DVersion();
41  utility::LogInfo("Usage:");
42  // clang-format off
43  utility::LogInfo("RealSenseBagReader [-V] --input input.bag [--output path]");
44  // clang-format on
45 }
46 
47 int main(int argc, char **argv) {
48  if (!utility::ProgramOptionExists(argc, argv, "--input")) {
49  PrintUsage();
50  return 1;
51  }
52  if (utility::ProgramOptionExists(argc, argv, "-V")) {
53  utility::SetVerbosityLevel(utility::VerbosityLevel::Debug);
54  } else {
55  utility::SetVerbosityLevel(utility::VerbosityLevel::Info);
56  }
57  std::string bag_filename =
58  utility::GetProgramOptionAsString(argc, argv, "--input");
59 
60  bool write_image = false;
61  std::string output_path;
62  if (!utility::ProgramOptionExists(argc, argv, "--output")) {
63  utility::LogInfo("No output image path, only play bag.");
64  } else {
65  output_path = utility::GetProgramOptionAsString(argc, argv, "--output");
66  if (output_path.empty()) {
67  utility::LogError("Output path {} is empty, only play bag.", output_path);
68  return 1;
69  }
70  if (utility::filesystem::DirectoryExists(output_path)) {
71  utility::LogWarning("Output path {} already existing, only play bag.",
72  output_path);
73  return 1;
74  } else if (!utility::filesystem::MakeDirectory(output_path)) {
75  utility::LogWarning("Unable to create path {}, only play bag.",
76  output_path);
77  return 1;
78  } else {
79  utility::LogInfo("Decompress images to {}", output_path);
80  utility::filesystem::MakeDirectoryHierarchy(output_path + "/color");
81  utility::filesystem::MakeDirectoryHierarchy(output_path + "/depth");
82  write_image = true;
83  }
84  }
85 
86  t::io::RSBagReader bag_reader;
87  bag_reader.Open(bag_filename);
88  if (!bag_reader.IsOpened()) {
89  utility::LogError("Unable to open {}", bag_filename);
90  return 1;
91  }
92 
93  bool flag_exit = false;
94  bool flag_play = true;
95  visualization::VisualizerWithKeyCallback vis;
96  visualization::SetGlobalColorMap(
97  visualization::ColorMap::ColorMapOption::Gray);
98  vis.RegisterKeyCallback(GLFW_KEY_ESCAPE, [&](visualization::Visualizer *vis) {
99  flag_exit = true;
100  return true;
101  });
102  vis.RegisterKeyCallback(GLFW_KEY_SPACE, [&](visualization::Visualizer *vis) {
103  if (flag_play) {
104  utility::LogInfo("Playback paused, press [SPACE] to continue");
105  } else {
106  utility::LogInfo("Playback resumed, press [SPACE] to pause");
107  }
108  flag_play = !flag_play;
109  return true;
110  });
111  vis.RegisterKeyCallback(GLFW_KEY_LEFT, [&](visualization::Visualizer *vis) {
112  uint64_t now = bag_reader.GetTimestamp();
113  if (bag_reader.SeekTimestamp(now < 1'000'000 ? 0 : now - 1'000'000))
114  utility::LogInfo("Seek back 1s");
115  else
116  utility::LogWarning("Seek back 1s failed");
117  return true;
118  });
119  vis.RegisterKeyCallback(GLFW_KEY_RIGHT, [&](visualization::Visualizer *vis) {
120  uint64_t now = bag_reader.GetTimestamp();
121  if (bag_reader.SeekTimestamp(now + 1'000'000))
122  utility::LogInfo("Seek forward 1s");
123  else
124  utility::LogWarning("Seek forward 1s failed");
125  return true;
126  });
127 
128  vis.CreateVisualizerWindow("Open3D Intel RealSense bag player", 1920, 540);
129  utility::LogInfo("Starting to play. Press [SPACE] to pause. Press [ESC] to "
130  "exit.");
131 
132  bool is_geometry_added = false;
133  int idx = 0;
134  const auto bag_metadata = bag_reader.GetMetadata();
135  utility::LogInfo("Recorded with device {}", bag_metadata.device_name_);
136  utility::LogInfo(" Serial number: {}", bag_metadata.serial_number_);
137  utility::LogInfo("Video resolution: {}x{}", bag_metadata.width_,
138  bag_metadata.height_);
139  utility::LogInfo(" frame rate: {}", bag_metadata.fps_);
140  utility::LogInfo(" duration: {:.6f}s",
141  static_cast<double>(bag_metadata.stream_length_usec_) *
142  1e-6);
143  utility::LogInfo(" color pixel format: {}", bag_metadata.color_format_);
144  utility::LogInfo(" depth pixel format: {}", bag_metadata.depth_format_);
145 
146  if (write_image) {
147  io::WriteIJsonConvertibleToJSON(
148  fmt::format("{}/intrinsic.json", output_path), bag_metadata);
149  }
150  const auto frame_interval = sc::duration<double>(1. / bag_metadata.fps_);
151 
152  auto last_frame_time = std::chrono::steady_clock::now() - frame_interval;
153  using legacyRGBDImage = open3d::geometry::RGBDImage;
154  legacyRGBDImage im_rgbd;
155  while (!bag_reader.IsEOF() && !flag_exit) {
156  if (flag_play) {
157  std::this_thread::sleep_until(last_frame_time + frame_interval);
158  last_frame_time = std::chrono::steady_clock::now();
159  im_rgbd = bag_reader.NextFrame().ToLegacyRGBDImage();
160  // create shared_ptr with no-op deleter for stack RGBDImage
161  auto ptr_im_rgbd =
162  std::shared_ptr<legacyRGBDImage>(&im_rgbd, [](legacyRGBDImage *) {});
163  // Improve depth visualization by scaling
164  /* im_rgbd.depth_.LinearTransform(0.25); */
165  if (ptr_im_rgbd->IsEmpty())
166  continue;
167 
168  if (!is_geometry_added) {
169  vis.AddGeometry(ptr_im_rgbd);
170  is_geometry_added = true;
171  }
172 
173  ++idx;
174  if (write_image)
175 #pragma omp parallel sections
176  {
177 #pragma omp section
178  {
179  auto color_file =
180  fmt::format("{0}/color/{1:05d}.jpg", output_path, idx);
181  utility::LogInfo("Writing to {}", color_file);
182  io::WriteImage(color_file, im_rgbd.color_);
183  }
184 #pragma omp section
185  {
186  auto depth_file =
187  fmt::format("{0}/depth/{1:05d}.png", output_path, idx);
188  utility::LogInfo("Writing to {}", depth_file);
189  io::WriteImage(depth_file, im_rgbd.depth_);
190  }
191  }
192  vis.UpdateGeometry();
193  vis.UpdateRender();
194  }
195  vis.PollEvents();
196  }
197  bag_reader.Close();
198 }
#define GLFW_KEY_LEFT
Definition: glfw3.h:419
int main(int argc, char **argv)
#define GLFW_KEY_SPACE
Definition: glfw3.h:360
GLsizei const GLchar *const * string
e
Definition: rmse.py:177
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
GLint GLint GLsizei GLint GLenum format
unsigned __int64 uint64_t
Definition: stdint.h:90
void PrintUsage()
#define GLFW_KEY_RIGHT
Definition: glfw3.h:418


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39