RealSenseRecorder.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 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 <memory>
29 #include <string>
30 
31 #include "open3d/Open3D.h"
32 
33 using namespace open3d;
34 namespace tio = open3d::t::io;
35 namespace sc = std::chrono;
36 
37 void PrintUsage() {
38  PrintOpen3DVersion();
39  // clang-format off
40  utility::LogInfo(
41  "Open a RealSense camera and display live color and depth streams. You can set\n"
42  "frame sizes and frame rates for each stream and the depth stream can be\n"
43  "optionally aligned to the color stream. NOTE: An error of 'UNKNOWN: Couldn't\n"
44  "resolve requests' implies unsupported stream format settings.");
45  // clang-format on
46  utility::LogInfo("Usage:");
47  utility::LogInfo(
48  "RealSenseRecorder [-h|--help] [-V] [-l|--list-devices] [--align]\n"
49  "[--record rgbd_video_file.bag] [-c|--config rs-config.json]");
50 }
51 
52 int main(int argc, char **argv) {
53  // Parse command line arguments
54  if (utility::ProgramOptionExists(argc, argv, "--help") ||
55  utility::ProgramOptionExists(argc, argv, "-h")) {
56  PrintUsage();
57  return 0;
58  }
59  if (utility::ProgramOptionExists(argc, argv, "--list-devices") ||
60  utility::ProgramOptionExists(argc, argv, "-l")) {
61  tio::RealSenseSensor::ListDevices();
62  return 0;
63  }
64  if (utility::ProgramOptionExists(argc, argv, "-V")) {
65  utility::SetVerbosityLevel(utility::VerbosityLevel::Debug);
66  } else {
67  utility::SetVerbosityLevel(utility::VerbosityLevel::Info);
68  }
69  bool align_streams = false;
70  std::string config_file, bag_file;
71 
72  if (utility::ProgramOptionExists(argc, argv, "-c")) {
73  config_file = utility::GetProgramOptionAsString(argc, argv, "-c");
74  } else if (utility::ProgramOptionExists(argc, argv, "--config")) {
75  config_file = utility::GetProgramOptionAsString(argc, argv, "--config");
76  } else {
77  utility::LogError("config json file required.");
78  PrintUsage();
79  return 1;
80  }
81  if (utility::ProgramOptionExists(argc, argv, "--align")) {
82  align_streams = true;
83  }
84  if (utility::ProgramOptionExists(argc, argv, "--record")) {
85  bag_file = utility::GetProgramOptionAsString(argc, argv, "--record");
86  }
87 
88  // Read in camera configuration.
89  tio::RealSenseSensorConfig rs_cfg;
90  open3d::io::ReadIJsonConvertible(config_file, rs_cfg);
91 
92  // Initialize camera.
93  tio::RealSenseSensor rs;
94  rs.ListDevices();
95  rs.InitSensor(rs_cfg, 0, bag_file);
96  utility::LogInfo("{}", rs.GetMetadata().ToString());
97 
98  // Create windows to show depth and color streams.
99  bool flag_start = false, flag_record = flag_start, flag_exit = false;
100  visualization::VisualizerWithKeyCallback depth_vis, color_vis;
101  auto callback_exit = [&](visualization::Visualizer *vis) {
102  flag_exit = true;
103  if (flag_start) {
104  utility::LogInfo("Recording finished.");
105  } else {
106  utility::LogInfo("Nothing has been recorded.");
107  }
108  return false;
109  };
110  depth_vis.RegisterKeyCallback(GLFW_KEY_ESCAPE, callback_exit);
111  color_vis.RegisterKeyCallback(GLFW_KEY_ESCAPE, callback_exit);
112  auto callback_toggle_record = [&](visualization::Visualizer *vis) {
113  if (flag_record) {
114  rs.PauseRecord();
115  utility::LogInfo(
116  "Recording paused. "
117  "Press [SPACE] to continue. "
118  "Press [ESC] to save and exit.");
119  flag_record = false;
120  } else {
121  rs.ResumeRecord();
122  flag_record = true;
123  if (!flag_start) {
124  utility::LogInfo(
125  "Recording started. "
126  "Press [SPACE] to pause. "
127  "Press [ESC] to save and exit.");
128  flag_start = true;
129  } else {
130  utility::LogInfo(
131  "Recording resumed, video may be discontinuous. "
132  "Press [SPACE] to pause. "
133  "Press [ESC] to save and exit.");
134  }
135  }
136  return false;
137  };
138  if (!bag_file.empty()) {
139  depth_vis.RegisterKeyCallback(GLFW_KEY_SPACE, callback_toggle_record);
140  color_vis.RegisterKeyCallback(GLFW_KEY_SPACE, callback_toggle_record);
141  utility::LogInfo(
142  "In the visulizer window, "
143  "press [SPACE] to start recording, "
144  "press [ESC] to exit.");
145  } else {
146  utility::LogInfo("In the visulizer window, press [ESC] to exit.");
147  }
148 
149  using legacyRGBDImage = open3d::geometry::RGBDImage;
150  using legacyImage = open3d::geometry::Image;
151  std::shared_ptr<legacyImage> depth_image_ptr, color_image_ptr;
152 
153  // Loop over frames from device
154  legacyRGBDImage im_rgbd;
155  bool is_geometry_added = false;
156  size_t frame_id = 0;
157  rs.StartCapture(flag_start);
158  do {
159  im_rgbd = rs.CaptureFrame(true, align_streams).ToLegacyRGBDImage();
160 
161  // Improve depth visualization by scaling
162  /* im_rgbd.depth_.LinearTransform(0.25); */
163  depth_image_ptr = std::shared_ptr<open3d::geometry::Image>(
164  &im_rgbd.depth_, [](open3d::geometry::Image *) {});
165  color_image_ptr = std::shared_ptr<open3d::geometry::Image>(
166  &im_rgbd.color_, [](open3d::geometry::Image *) {});
167 
168  if (!is_geometry_added) {
169  if (!depth_vis.CreateVisualizerWindow(
170  "Open3D || RealSense || Depth", depth_image_ptr->width_,
171  depth_image_ptr->height_, 15, 50) ||
172  !depth_vis.AddGeometry(depth_image_ptr) ||
173  !color_vis.CreateVisualizerWindow(
174  "Open3D || RealSense || Color", color_image_ptr->width_,
175  color_image_ptr->height_, 675, 50) ||
176  !color_vis.AddGeometry(color_image_ptr)) {
177  utility::LogError("Window creation failed!");
178  return 0;
179  }
180  is_geometry_added = true;
181  }
182 
183  depth_vis.UpdateGeometry();
184  color_vis.UpdateGeometry();
185  depth_vis.PollEvents();
186  color_vis.PollEvents();
187  depth_vis.UpdateRender();
188  color_vis.UpdateRender();
189 
190  if (frame_id++ % 30 == 0) {
191  utility::LogInfo("Time: {}s, Frame {}",
192  static_cast<double>(rs.GetTimestamp()) * 1e-6,
193  frame_id - 1);
194  }
195  } while (!flag_exit);
196 
197  rs.StopCapture();
198  return 0;
199 }
::sensor_msgs::Image_< std::allocator< void > > Image
Definition: Image.h:78
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
void PrintUsage()


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