forked from xdspacelab/openvslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tum_rgbd_slam.cc
337 lines (284 loc) · 11.8 KB
/
run_tum_rgbd_slam.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "util/tum_rgbd_util.h"
#ifdef USE_PANGOLIN_VIEWER
#include "pangolin_viewer/viewer.h"
#elif USE_SOCKET_PUBLISHER
#include "socket_publisher/publisher.h"
#endif
#include "openvslam/system.h"
#include "openvslam/config.h"
#include <iostream>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <iomanip>
#include <numeric>
#include <opencv2/core/core.hpp>
#include <spdlog/spdlog.h>
#include <popl.hpp>
#ifdef USE_STACK_TRACE_LOGGER
#include <glog/logging.h>
#endif
#ifdef USE_GOOGLE_PERFTOOLS
#include <gperftools/profiler.h>
#endif
void mono_tracking(const std::shared_ptr<openvslam::config>& cfg,
const std::string& vocab_file_path, const std::string& sequence_dir_path,
const unsigned int frame_skip, const bool no_sleep, const bool auto_term,
const bool eval_log, const std::string& map_db_path) {
tum_rgbd_sequence sequence(sequence_dir_path);
const auto frames = sequence.get_frames();
// build a SLAM system
openvslam::system SLAM(cfg, vocab_file_path);
// startup the SLAM process
SLAM.startup();
// create a viewer object
// and pass the frame_publisher and the map_publisher
#ifdef USE_PANGOLIN_VIEWER
pangolin_viewer::viewer viewer(cfg, &SLAM, SLAM.get_frame_publisher(), SLAM.get_map_publisher());
#elif USE_SOCKET_PUBLISHER
socket_publisher::publisher publisher(cfg, &SLAM, SLAM.get_frame_publisher(), SLAM.get_map_publisher());
#endif
std::vector<double> track_times;
track_times.reserve(frames.size());
// run the SLAM in another thread
std::thread thread([&]() {
for (unsigned int i = 0; i < frames.size(); ++i) {
const auto& frame = frames.at(i);
const auto rgb_img = cv::imread(frame.rgb_img_path_, cv::IMREAD_UNCHANGED);
const auto tp_1 = std::chrono::steady_clock::now();
if (!rgb_img.empty() && (i % frame_skip == 0)) {
// input the current frame and estimate the camera pose
SLAM.feed_monocular_frame(rgb_img, frame.timestamp_);
}
const auto tp_2 = std::chrono::steady_clock::now();
const auto track_time = std::chrono::duration_cast<std::chrono::duration<double>>(tp_2 - tp_1).count();
if (i % frame_skip == 0) {
track_times.push_back(track_time);
}
// wait until the timestamp of the next frame
if (!no_sleep && i < frames.size() - 1) {
const auto wait_time = frames.at(i + 1).timestamp_ - (frame.timestamp_ + track_time);
if (0.0 < wait_time) {
std::this_thread::sleep_for(std::chrono::microseconds(static_cast<unsigned int>(wait_time * 1e6)));
}
}
// check if the termination of SLAM system is requested or not
if (SLAM.terminate_is_requested()) {
break;
}
}
// wait until the loop BA is finished
while (SLAM.loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
}
// automatically close the viewer
#ifdef USE_PANGOLIN_VIEWER
if (auto_term) {
viewer.request_terminate();
}
#elif USE_SOCKET_PUBLISHER
if (auto_term) {
publisher.request_terminate();
}
#endif
});
// run the viewer in the current thread
#ifdef USE_PANGOLIN_VIEWER
viewer.run();
#elif USE_SOCKET_PUBLISHER
publisher.run();
#endif
thread.join();
// shutdown the SLAM process
SLAM.shutdown();
if (eval_log) {
// output the trajectories for evaluation
SLAM.save_frame_trajectory("frame_trajectory.txt", "TUM");
SLAM.save_keyframe_trajectory("keyframe_trajectory.txt", "TUM");
// output the tracking times for evaluation
std::ofstream ofs("track_times.txt", std::ios::out);
if (ofs.is_open()) {
for (const auto track_time : track_times) {
ofs << track_time << std::endl;
}
ofs.close();
}
}
if (!map_db_path.empty()) {
// output the map database
SLAM.save_map_database(map_db_path);
}
std::sort(track_times.begin(), track_times.end());
const auto total_track_time = std::accumulate(track_times.begin(), track_times.end(), 0.0);
std::cout << "median tracking time: " << track_times.at(track_times.size() / 2) << "[s]" << std::endl;
std::cout << "mean tracking time: " << total_track_time / track_times.size() << "[s]" << std::endl;
}
void rgbd_tracking(const std::shared_ptr<openvslam::config>& cfg,
const std::string& vocab_file_path, const std::string& sequence_dir_path,
const unsigned int frame_skip, const bool no_sleep, const bool auto_term,
const bool eval_log, const std::string& map_db_path) {
tum_rgbd_sequence sequence(sequence_dir_path);
const auto frames = sequence.get_frames();
// build a SLAM system
openvslam::system SLAM(cfg, vocab_file_path);
// startup the SLAM process
SLAM.startup();
// create a viewer object
// and pass the frame_publisher and the map_publisher
#ifdef USE_PANGOLIN_VIEWER
pangolin_viewer::viewer viewer(cfg, &SLAM, SLAM.get_frame_publisher(), SLAM.get_map_publisher());
#elif USE_SOCKET_PUBLISHER
socket_publisher::publisher publisher(cfg, &SLAM, SLAM.get_frame_publisher(), SLAM.get_map_publisher());
#endif
std::vector<double> track_times;
track_times.reserve(frames.size());
// run the SLAM in another thread
std::thread thread([&]() {
for (unsigned int i = 0; i < frames.size(); ++i) {
const auto& frame = frames.at(i);
const auto rgb_img = cv::imread(frame.rgb_img_path_, cv::IMREAD_UNCHANGED);
const auto depth_img = cv::imread(frame.depth_img_path_, cv::IMREAD_UNCHANGED);
const auto tp_1 = std::chrono::steady_clock::now();
if (!rgb_img.empty() && !depth_img.empty() && (i % frame_skip == 0)) {
// input the current frame and estimate the camera pose
SLAM.feed_RGBD_frame(rgb_img, depth_img, frame.timestamp_);
}
const auto tp_2 = std::chrono::steady_clock::now();
const auto track_time = std::chrono::duration_cast<std::chrono::duration<double>>(tp_2 - tp_1).count();
if (i % frame_skip == 0) {
track_times.push_back(track_time);
}
// wait until the timestamp of the next frame
if (!no_sleep && i < frames.size() - 1) {
const auto wait_time = frames.at(i + 1).timestamp_ - (frame.timestamp_ + track_time);
if (0.0 < wait_time) {
std::this_thread::sleep_for(std::chrono::microseconds(static_cast<unsigned int>(wait_time * 1e6)));
}
}
// check if the termination of SLAM system is requested or not
if (SLAM.terminate_is_requested()) {
break;
}
}
// wait until the loop BA is finished
while (SLAM.loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
}
// automatically close the viewer
#ifdef USE_PANGOLIN_VIEWER
if (auto_term) {
viewer.request_terminate();
}
#elif USE_SOCKET_PUBLISHER
if (auto_term) {
publisher.request_terminate();
}
#endif
});
// run the viewer in the current thread
#ifdef USE_PANGOLIN_VIEWER
viewer.run();
#elif USE_SOCKET_PUBLISHER
publisher.run();
#endif
thread.join();
// shutdown the SLAM process
SLAM.shutdown();
if (eval_log) {
// output the trajectories for evaluation
SLAM.save_frame_trajectory("frame_trajectory.txt", "TUM");
SLAM.save_keyframe_trajectory("keyframe_trajectory.txt", "TUM");
// output the tracking times for evaluation
std::ofstream ofs("track_times.txt", std::ios::out);
if (ofs.is_open()) {
for (const auto track_time : track_times) {
ofs << track_time << std::endl;
}
ofs.close();
}
}
if (!map_db_path.empty()) {
// output the map database
SLAM.save_map_database(map_db_path);
}
std::sort(track_times.begin(), track_times.end());
const auto total_track_time = std::accumulate(track_times.begin(), track_times.end(), 0.0);
std::cout << "median tracking time: " << track_times.at(track_times.size() / 2) << "[s]" << std::endl;
std::cout << "mean tracking time: " << total_track_time / track_times.size() << "[s]" << std::endl;
}
int main(int argc, char* argv[]) {
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#endif
// create options
popl::OptionParser op("Allowed options");
auto help = op.add<popl::Switch>("h", "help", "produce help message");
auto vocab_file_path = op.add<popl::Value<std::string>>("v", "vocab", "vocabulary file path");
auto data_dir_path = op.add<popl::Value<std::string>>("d", "data-dir", "directory path which contains dataset");
auto config_file_path = op.add<popl::Value<std::string>>("c", "config", "config file path");
auto frame_skip = op.add<popl::Value<unsigned int>>("", "frame-skip", "interval of frame skip", 1);
auto no_sleep = op.add<popl::Switch>("", "no-sleep", "not wait for next frame in real time");
auto auto_term = op.add<popl::Switch>("", "auto-term", "automatically terminate the viewer");
auto debug_mode = op.add<popl::Switch>("", "debug", "debug mode");
auto eval_log = op.add<popl::Switch>("", "eval-log", "store trajectory and tracking times for evaluation");
auto map_db_path = op.add<popl::Value<std::string>>("p", "map-db", "store a map database at this path after SLAM", "");
try {
op.parse(argc, argv);
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
std::cerr << std::endl;
std::cerr << op << std::endl;
return EXIT_FAILURE;
}
// check validness of options
if (help->is_set()) {
std::cerr << op << std::endl;
return EXIT_FAILURE;
}
if (!vocab_file_path->is_set() || !data_dir_path->is_set() || !config_file_path->is_set()) {
std::cerr << "invalid arguments" << std::endl;
std::cerr << std::endl;
std::cerr << op << std::endl;
return EXIT_FAILURE;
}
// setup logger
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] %^[%L] %v%$");
if (debug_mode->is_set()) {
spdlog::set_level(spdlog::level::debug);
}
else {
spdlog::set_level(spdlog::level::info);
}
// load configuration
std::shared_ptr<openvslam::config> cfg;
try {
cfg = std::make_shared<openvslam::config>(config_file_path->value());
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
#ifdef USE_GOOGLE_PERFTOOLS
ProfilerStart("slam.prof");
#endif
// run tracking
if (cfg->camera_->setup_type_ == openvslam::camera::setup_type_t::Monocular) {
mono_tracking(cfg, vocab_file_path->value(), data_dir_path->value(),
frame_skip->value(), no_sleep->is_set(), auto_term->is_set(),
eval_log->is_set(), map_db_path->value());
}
else if (cfg->camera_->setup_type_ == openvslam::camera::setup_type_t::RGBD) {
rgbd_tracking(cfg, vocab_file_path->value(), data_dir_path->value(),
frame_skip->value(), no_sleep->is_set(), auto_term->is_set(),
eval_log->is_set(), map_db_path->value());
}
else {
throw std::runtime_error("Invalid setup type: " + cfg->camera_->get_setup_type_string());
}
#ifdef USE_GOOGLE_PERFTOOLS
ProfilerStop();
#endif
return EXIT_SUCCESS;
}