forked from stella-cv/stella_vslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_camera_slam.cc
340 lines (288 loc) · 11.9 KB
/
run_camera_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
338
339
340
#ifdef USE_PANGOLIN_VIEWER
#include "pangolin_viewer/viewer.h"
#elif USE_SOCKET_PUBLISHER
#include "socket_publisher/publisher.h"
#endif
#include "stella_vslam/system.h"
#include "stella_vslam/config.h"
#include "stella_vslam/camera/base.h"
#include "stella_vslam/util/stereo_rectifier.h"
#include "stella_vslam/util/yaml.h"
#include <iostream>
#include <chrono>
#include <numeric>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <spdlog/spdlog.h>
#include <popl.hpp>
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
#ifdef USE_STACK_TRACE_LOGGER
#include <backward.hpp>
#endif
#ifdef USE_GOOGLE_PERFTOOLS
#include <gperftools/profiler.h>
#endif
void mono_tracking(const std::shared_ptr<stella_vslam::system>& slam,
const std::shared_ptr<stella_vslam::config>& cfg,
const unsigned int cam_num,
const std::string& mask_img_path,
const float scale,
const std::string& map_db_path) {
// load the mask image
const cv::Mat mask = mask_img_path.empty() ? cv::Mat{} : cv::imread(mask_img_path, cv::IMREAD_GRAYSCALE);
// create a viewer object
// and pass the frame_publisher and the map_publisher
#ifdef USE_PANGOLIN_VIEWER
pangolin_viewer::viewer viewer(
stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "PangolinViewer"), slam, slam->get_frame_publisher(), slam->get_map_publisher());
#elif USE_SOCKET_PUBLISHER
socket_publisher::publisher publisher(
stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "SocketPublisher"), slam, slam->get_frame_publisher(), slam->get_map_publisher());
#endif
auto video = cv::VideoCapture(cam_num);
if (!video.isOpened()) {
spdlog::critical("cannot open a camera {}", cam_num);
slam->shutdown();
return;
}
cv::Mat frame;
std::vector<double> track_times;
unsigned int num_frame = 0;
bool is_not_end = true;
// run the slam in another thread
std::thread thread([&]() {
while (is_not_end) {
// check if the termination of slam system is requested or not
if (slam->terminate_is_requested()) {
break;
}
is_not_end = video.read(frame);
if (frame.empty()) {
continue;
}
if (scale != 1.0) {
cv::resize(frame, frame, cv::Size(), scale, scale, cv::INTER_LINEAR);
}
const auto tp_1 = std::chrono::steady_clock::now();
// input the current frame and estimate the camera pose
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
double timestamp = std::chrono::duration_cast<std::chrono::duration<double>>(now.time_since_epoch()).count();
slam->feed_monocular_frame(frame, timestamp, mask);
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();
track_times.push_back(track_time);
++num_frame;
}
// wait until the loop BA is finished
while (slam->loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
}
});
// 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 (!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 stereo_tracking(const std::shared_ptr<stella_vslam::system>& slam,
const std::shared_ptr<stella_vslam::config>& cfg,
const unsigned int cam_num,
const std::string& mask_img_path,
const float scale,
const std::string& map_db_path) {
const cv::Mat mask = mask_img_path.empty() ? cv::Mat{} : cv::imread(mask_img_path, cv::IMREAD_GRAYSCALE);
// create a viewer object
// and pass the frame_publisher and the map_publisher
#ifdef USE_PANGOLIN_VIEWER
pangolin_viewer::viewer viewer(
stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "PangolinViewer"), slam, slam->get_frame_publisher(), slam->get_map_publisher());
#elif USE_SOCKET_PUBLISHER
socket_publisher::publisher publisher(
stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "SocketPublisher"), slam, slam->get_frame_publisher(), slam->get_map_publisher());
#endif
cv::VideoCapture videos[2];
for (int i = 0; i < 2; i++) {
videos[i] = cv::VideoCapture(cam_num + i);
if (!videos[i].isOpened()) {
spdlog::critical("cannot open a camera {}", cam_num + i);
slam->shutdown();
return;
}
}
const stella_vslam::util::stereo_rectifier rectifier(cfg, slam->get_camera());
cv::Mat frames[2];
cv::Mat frames_rectified[2];
std::vector<double> track_times;
unsigned int num_frame = 0;
bool is_not_end = true;
// run the slam in another thread
std::thread thread([&]() {
while (is_not_end) {
// check if the termination of slam system is requested or not
if (slam->terminate_is_requested()) {
break;
}
is_not_end = videos[0].read(frames[0]) && videos[1].read(frames[1]);
if (frames[0].empty() || frames[1].empty()) {
continue;
}
for (int i = 0; i < 2; i++) {
if (scale != 1.0) {
cv::resize(frames[i], frames[i], cv::Size(), scale, scale, cv::INTER_LINEAR);
}
}
rectifier.rectify(frames[0], frames[1], frames_rectified[0], frames_rectified[1]);
const auto tp_1 = std::chrono::steady_clock::now();
// input the current frame and estimate the camera pose
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
double timestamp = std::chrono::duration_cast<std::chrono::duration<double>>(now.time_since_epoch()).count();
slam->feed_stereo_frame(frames_rectified[0], frames_rectified[1], timestamp, mask);
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();
track_times.push_back(track_time);
++num_frame;
}
// wait until the loop BA is finished
while (slam->loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
}
});
// 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 (!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
backward::SignalHandling sh;
#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 cam_num = op.add<popl::Value<unsigned int>>("n", "number", "camera number");
auto config_file_path = op.add<popl::Value<std::string>>("c", "config", "config file path");
auto mask_img_path = op.add<popl::Value<std::string>>("", "mask", "mask image path", "");
auto scale = op.add<popl::Value<float>>("s", "scale", "scaling ratio of images", 1.0);
auto map_db_path_in = op.add<popl::Value<std::string>>("i", "map-db-in", "load a map from this path", "");
auto map_db_path_out = op.add<popl::Value<std::string>>("o", "map-db-out", "store a map database at this path after slam", "");
auto log_level = op.add<popl::Value<std::string>>("", "log-level", "log level", "info");
auto disable_mapping = op.add<popl::Switch>("", "disable-mapping", "disable mapping");
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 (!op.unknown_options().empty()) {
for (const auto& unknown_option : op.unknown_options()) {
std::cerr << "unknown_options: " << unknown_option << std::endl;
}
std::cerr << op << std::endl;
return EXIT_FAILURE;
}
if (!vocab_file_path->is_set() || !cam_num->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%$");
spdlog::set_level(spdlog::level::from_str(log_level->value()));
// load configuration
std::shared_ptr<stella_vslam::config> cfg;
try {
cfg = std::make_shared<stella_vslam::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
// build a slam system
auto slam = std::make_shared<stella_vslam::system>(cfg, vocab_file_path->value());
bool need_initialize = true;
if (map_db_path_in->is_set()) {
need_initialize = false;
const auto path = fs::path(map_db_path_in->value());
if (path.extension() == ".yaml") {
YAML::Node node = YAML::LoadFile(path);
for (const auto& map_path : node["maps"].as<std::vector<std::string>>()) {
slam->load_map_database(path.parent_path() / map_path);
}
}
else {
// load the prebuilt map
slam->load_map_database(path);
}
}
slam->startup(need_initialize);
if (disable_mapping->is_set()) {
slam->disable_mapping_module();
}
// run tracking
if (slam->get_camera()->setup_type_ == stella_vslam::camera::setup_type_t::Monocular) {
mono_tracking(slam,
cfg,
cam_num->value(),
mask_img_path->value(),
scale->value(),
map_db_path_out->value());
}
else if (slam->get_camera()->setup_type_ == stella_vslam::camera::setup_type_t::Stereo) {
stereo_tracking(slam,
cfg,
cam_num->value(),
mask_img_path->value(),
scale->value(),
map_db_path_out->value());
}
else {
throw std::runtime_error("Invalid setup type: " + slam->get_camera()->get_setup_type_string());
}
#ifdef USE_GOOGLE_PERFTOOLS
ProfilerStop();
#endif
return EXIT_SUCCESS;
}