forked from SpectacularAI/sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vio_mapper.cpp
211 lines (183 loc) · 7.1 KB
/
vio_mapper.cpp
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
#include <iostream>
#include <fstream>
#include <librealsense2/rs.hpp>
#include <spectacularAI/realsense/plugin.hpp>
#include <spectacularAI/mapping.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <cassert>
#include <string>
#include <cstdint>
#include <thread>
#include <chrono>
#include <deque>
#include <atomic>
#include <cstdlib>
#include <set>
#include "helpers.hpp"
namespace {
struct ImageToSave {
std::string fileName;
cv::Mat mat;
};
int colorFormatToOpenCVType(spectacularAI::ColorFormat colorFormat) {
switch (colorFormat) {
case spectacularAI::ColorFormat::GRAY: return CV_8UC1;
case spectacularAI::ColorFormat::GRAY16: return CV_16UC1;
case spectacularAI::ColorFormat::RGB: return CV_8UC3;
case spectacularAI::ColorFormat::RGBA: return CV_8UC4;
default: return -1;
}
}
std::function<void()> buildImageWriter(
std::deque<ImageToSave> &queue,
std::mutex &mutex,
std::atomic<bool> &shouldQuit)
{
return [&queue, &mutex, &shouldQuit]() {
while (!shouldQuit) {
std::unique_lock<std::mutex> lock(mutex);
constexpr int LOOP_SLEEP_MS = 10;
if (queue.empty()) {
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(LOOP_SLEEP_MS));
continue;
}
auto img = queue.front();
queue.pop_front();
lock.unlock();
// The SDK outputs RGB and OpenCV expects BGR.
cv::Mat bgrMat;
cv::cvtColor(img.mat, bgrMat, cv::COLOR_RGB2BGR);
// If this line crashes, OpenCV probably has been built without PNG support.
cv::imwrite(img.fileName.c_str(), bgrMat);
}
};
}
cv::Mat copyImage(std::shared_ptr<const spectacularAI::Bitmap> bitmap) {
int cvType = colorFormatToOpenCVType(bitmap->getColorFormat());
return cv::Mat(
bitmap->getHeight(),
bitmap->getWidth(),
cvType,
const_cast<std::uint8_t *>(bitmap->getDataReadOnly())
).clone();
}
std::string matrix4ToString(const spectacularAI::Matrix4d &matrix) {
std::stringstream ss;
ss << "[";
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
ss << matrix[i][j];
ss << "]";
return ss.str();
}
void serializePosesToFile(std::ofstream& posesFile, std::shared_ptr<const spectacularAI::mapping::KeyFrame> keyframe) {
auto& frameSet = keyframe->frameSet;
std::stringstream ss;
ss << "{\"frameId\": " << (keyframe->id) << ","
<< "\"poses\": {";
if (frameSet->rgbFrame) {
ss << "\"rgb\": " << matrix4ToString(frameSet->rgbFrame->cameraPose.pose.asMatrix());
}
if (frameSet->depthFrame) {
if (frameSet->rgbFrame) ss << ",";
ss << "\"depth\": " << matrix4ToString(frameSet->depthFrame->cameraPose.pose.asMatrix());
}
ss << "}}";
std::cout << "saving " << ss.str() << std::endl;
posesFile << ss.str() << std::endl;
}
} // namespace
int main(int argc, char** argv) {
// If a folder is given as an argument, record session there
std::string recordingFolder;
if (argc >= 2) {
recordingFolder = argv[1];
createFolders(recordingFolder);
} else {
std::cerr
<< "Usage: " << argv[0] << " /path/to/recording/folder" << std::endl;
return 1;
}
spectacularAI::rsPlugin::Configuration vioConfig;
vioConfig.useSlam = true;
spectacularAI::rsPlugin::Pipeline vioPipeline(vioConfig);
{
// Find RealSense device
rs2::context rsContext;
rs2::device_list devices = rsContext.query_devices();
if (devices.size() != 1) {
std::cout << "Connect exactly one RealSense device." << std::endl;
return EXIT_SUCCESS;
}
rs2::device device = devices.front();
vioPipeline.configureDevice(device);
}
// Start pipeline
rs2::config rsConfig;
vioPipeline.configureStreams(rsConfig);
// VIO works fine with BGR-flipped data too
rsConfig.enable_stream(RS2_STREAM_COLOR, RS2_FORMAT_BGR8);
// The RS callback thread should not be blocked for long.
// Using worker threads for image encoding and disk I/O
constexpr int N_WORKER_THREADS = 4;
std::mutex queueMutex;
std::deque<ImageToSave> imageQueue;
std::atomic<bool> shouldQuit(false);
std::vector<std::thread> imageWriterThreads;
for (int i = 0; i < N_WORKER_THREADS; ++i) {
imageWriterThreads.emplace_back(buildImageWriter(imageQueue, queueMutex, shouldQuit));
}
std::vector<char> fileNameBuf;
fileNameBuf.resize(1000, 0);
std::shared_ptr<spectacularAI::rsPlugin::Session> vioSession;
std::ofstream posesFile = std::ofstream(recordingFolder + "/poses.jsonl");
std::set<int64_t> savedFrames;
vioPipeline.setMapperCallback([&](std::shared_ptr<const spectacularAI::mapping::MapperOutput> output){
for (int64_t frameId : output->updatedKeyFrames) {
auto search = output->map->keyFrames.find(frameId);
if (search == output->map->keyFrames.end()) {
continue; // deleted frame
}
auto& frameSet = search->second->frameSet;
if (savedFrames.count(frameId) == 0) {
// Only save images once, despide frames pose possibly being updated several times
savedFrames.insert(frameId);
std::lock_guard<std::mutex> lock(queueMutex);
char *fileName = fileNameBuf.data();
// Copy images to ensure they are in memory later for saving
if (frameSet->rgbFrame && frameSet->rgbFrame->image) {
std::snprintf(fileName, fileNameBuf.size(), "%s/rgb_%04ld.png", recordingFolder.c_str(), frameId);
imageQueue.push_back(ImageToSave {fileName, copyImage(frameSet->rgbFrame->image)});
}
if (frameSet->depthFrame && frameSet->depthFrame->image) {
std::snprintf(fileName, fileNameBuf.size(), "%s/depth_%04ld.png", recordingFolder.c_str(), frameId);
imageQueue.push_back(ImageToSave {fileName, copyImage(frameSet->depthFrame->image)});
}
// TODO: Save pointclouds as JSON?
}
}
// Save only final fully optimized poses, might not contain poses for all frames in case they were deleted
if (output->finalMap) {
for (auto it = output->map->keyFrames.begin(); it != output->map->keyFrames.end(); it++) {
serializePosesToFile(posesFile, it->second);
}
}
});
vioSession = vioPipeline.startSession(rsConfig);
std::thread inputThread([&]() {
std::cerr << "Press Enter to quit." << std::endl << std::endl;
std::getchar();
shouldQuit = true;
});
while (!shouldQuit) {
auto vioOut = vioSession->waitForOutput();
}
vioSession = nullptr; // Ensure Vio is done before we quit
inputThread.join();
for (auto &t : imageWriterThreads) t.join();
std::cerr << "Bye!" << std::endl;
return 0;
}