forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs-convert.cpp
318 lines (260 loc) · 10.4 KB
/
rs-convert.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
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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2018 Intel Corporation. All Rights Reserved.
#include <iostream>
#include "librealsense2/rs.hpp"
#include "tclap/CmdLine.h"
#include "converters/converter-csv.hpp"
#include "converters/converter-png.hpp"
#include "converters/converter-raw.hpp"
#include "converters/converter-ply.hpp"
#include "converters/converter-bin.hpp"
#include "converters/converter-text.hpp"
#include <mutex>
#define SECONDS_TO_NANOSECONDS 1000000000
using namespace std;
using namespace TCLAP;
int main(int argc, char** argv) try
{
#ifdef BUILD_EASYLOGGINGPP
rs2::log_to_file(RS2_LOG_SEVERITY_WARN);
#endif
// Parse command line arguments
CmdLine cmd("librealsense rs-convert tool", ' ');
ValueArg<string> inputFilename("i", "input", "ROS-bag filename", true, "", "ros-bag-file");
ValueArg<string> outputFilenamePng("p", "output-png", "output PNG file(s) path", false, "", "png-path");
ValueArg<string> outputFilenameCsv("v", "output-csv", "output CSV (depth matrix) file(s) path", false, "", "csv-path");
ValueArg<string> outputFilenameRaw("r", "output-raw", "output RAW file(s) path", false, "", "raw-path");
ValueArg<string> outputFilenamePly("l", "output-ply", "output PLY file(s) path", false, "", "ply-path");
ValueArg<string> outputFilenameBin("b", "output-bin", "output BIN (depth matrix) file(s) path", false, "", "bin-path");
SwitchArg switchDepth("d", "depth", "convert depth frames (default - all supported)", false);
SwitchArg switchColor("c", "color", "convert color frames (default - all supported)", false);
SwitchArg switchTextOutput( "T", "output-text", "output text to stdout", false );
ValueArg <string> frameNumberStart("f", "first-framenumber", "ignore frames whose frame number is less than this value", false, "", "first-framenumber");
ValueArg <string> frameNumberEnd("t", "last-framenumber", "ignore frames whose frame number is greater than this value", false, "", "last-framenumber");
ValueArg <string> startTime("s", "start-time", "ignore frames whose timestamp is less than this value (the first frame is at time 0)", false, "", "start-time");
ValueArg <string> endTime("e", "end-time", "ignore frames whose timestamp is greater than this value (the first frame is at time 0)", false, "", "end-time");
cmd.add(inputFilename);
cmd.add(frameNumberEnd);
cmd.add(frameNumberStart);
cmd.add(endTime);
cmd.add(startTime);
cmd.add(outputFilenamePng);
cmd.add(outputFilenameCsv);
cmd.add(outputFilenameRaw);
cmd.add(outputFilenamePly);
cmd.add(outputFilenameBin);
cmd.add(switchDepth);
cmd.add(switchColor);
cmd.add( switchTextOutput );
cmd.parse(argc, argv);
vector<shared_ptr<rs2::tools::converter::converter_base>> converters;
shared_ptr<rs2::tools::converter::converter_ply> plyconverter;
rs2_stream streamType = switchDepth.isSet() ? rs2_stream::RS2_STREAM_DEPTH
: switchColor.isSet() ? rs2_stream::RS2_STREAM_COLOR
: rs2_stream::RS2_STREAM_ANY;
if (outputFilenameCsv.isSet())
{
converters.push_back(
make_shared<rs2::tools::converter::converter_csv>(
outputFilenameCsv.getValue()
, streamType));
}
if (outputFilenamePng.isSet())
{
converters.push_back(
make_shared<rs2::tools::converter::converter_png>(
outputFilenamePng.getValue()
, streamType));
}
if (outputFilenameRaw.isSet())
{
converters.push_back(
make_shared<rs2::tools::converter::converter_raw>(
outputFilenameRaw.getValue()
, streamType));
}
if (outputFilenameBin.isSet())
{
converters.push_back(
make_shared<rs2::tools::converter::converter_bin>(
outputFilenameBin.getValue()));
}
if( switchTextOutput.isSet() )
{
converters.push_back( make_shared< rs2::tools::converter::converter_text >() );
}
if (converters.empty() && !outputFilenamePly.isSet())
{
throw runtime_error("output not defined");
}
unsigned long long first_frame = 0;
unsigned long long last_frame = 0;
uint64_t start_time = 0;
uint64_t end_time = 0;
if (frameNumberStart.isSet())
{
first_frame = stoi(frameNumberStart.getValue());
}
if (frameNumberEnd.isSet())
{
last_frame = stoi(frameNumberEnd.getValue());
}
if (startTime.isSet())
{
start_time = (uint64_t) (SECONDS_TO_NANOSECONDS * (std::strtod( startTime.getValue().c_str(), nullptr )));
}
if (endTime.isSet())
{
end_time = (uint64_t) (SECONDS_TO_NANOSECONDS * (std::strtod( endTime.getValue().c_str(), nullptr )));
}
//in order to convert frames into ply we need synced depth and color frames,
//therefore we use pipeline
if (outputFilenamePly.isSet()) {
// Since we are running in blocking "non-real-time" mode,
// we don't want to prevent process termination if some of the frames
// did not find a match and hence were not serviced
auto pipe = std::shared_ptr<rs2::pipeline>(
new rs2::pipeline(), [](rs2::pipeline*) {});
plyconverter = make_shared<rs2::tools::converter::converter_ply>(
outputFilenamePly.getValue());
rs2::config cfg;
cfg.enable_device_from_file(inputFilename.getValue());
pipe->start(cfg);
auto device = pipe->get_active_profile().get_device();
rs2::playback playback = device.as<rs2::playback>();
playback.set_real_time(false);
auto duration = playback.get_duration();
int progress = 0;
auto frameNumber = 0ULL;
rs2::frameset frameset;
uint64_t posCurr = playback.get_position();
// try_wait_for_frames will keep repeating the last frame at the end of the file,
// so we need to exit the look in some other way!
while (pipe->try_wait_for_frames(&frameset, 1000))
{
int posP = static_cast<int>(posCurr * 100. / duration.count());
if (posP > progress)
{
progress = posP;
cout << posP << "%" << "\r" << flush;
}
frameNumber = frameset[0].get_frame_number();
bool process_frame = true;
if (frameNumberStart.isSet() && frameNumber < first_frame)
process_frame = false;
else if (frameNumberEnd.isSet() && frameNumber > last_frame)
process_frame = false;
else if (startTime.isSet() && posCurr < start_time)
process_frame = false;
else if (endTime.isSet() && posCurr > end_time)
process_frame = false;
if( process_frame )
{
plyconverter->convert(frameset);
plyconverter->wait();
}
auto posNext = playback.get_position();
if (posNext < posCurr)
break;
posCurr = posNext;
}
}
// for every converter other than ply,
// we get the frames from playback sensors
// and convert them one by one
if( ! converters.empty() )
{
rs2::context ctx;
auto playback = ctx.load_device(inputFilename.getValue());
playback.set_real_time(false);
std::vector<rs2::sensor> sensors = playback.query_sensors();
std::mutex mutex;
auto duration = playback.get_duration();
int progress = 0;
uint64_t posCurr = playback.get_position();
for (auto sensor : sensors)
{
if (!sensor.get_stream_profiles().size())
{
continue;
}
sensor.open(sensor.get_stream_profiles());
sensor.start([&](rs2::frame frame)
{
std::lock_guard<std::mutex> lock(mutex);
auto frameNumber = frame.get_frame_number();
if (frameNumberStart.isSet() && frameNumber < first_frame)
return;
if (frameNumberEnd.isSet() && frameNumber > last_frame)
return;
if (startTime.isSet() && posCurr < start_time)
return;
if (endTime.isSet() && posCurr > end_time)
return;
for_each(converters.begin(), converters.end(),
[&frame](shared_ptr<rs2::tools::converter::converter_base>& converter) {
converter->convert(frame);
});
for_each(converters.begin(), converters.end(),
[](shared_ptr<rs2::tools::converter::converter_base>& converter) {
converter->wait();
});
});
}
//we need to clear the output of ply progress ("100%") before writing
//the progress of the other converters in the same line
cout << "\r \r";
while (true)
{
int posP = static_cast<int>(posCurr * 100. / duration.count());
if (posP > progress)
{
progress = posP;
if( ! switchTextOutput.isSet() )
cout << posP << "%" << "\r" << flush;
}
const uint64_t posNext = playback.get_position();
if (posNext < posCurr)
break;
posCurr = posNext;
}
for (auto sensor : sensors)
{
if (!sensor.get_stream_profiles().size())
{
continue;
}
sensor.stop();
sensor.close();
}
}
if( !switchTextOutput.isSet() )
cout << endl;
//print statistics for ply converter.
if (outputFilenamePly.isSet()) {
cout << plyconverter->get_statistics() << endl;
}
if( ! switchTextOutput.isSet() )
for_each( converters.begin(),
converters.end(),
[]( shared_ptr< rs2::tools::converter::converter_base > & converter ) {
cout << converter->get_statistics() << endl;
} );
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
cerr << "RealSense error calling " << e.get_failed_function()
<< "(" << e.get_failed_args() << "):\n " << e.what() << endl;
return EXIT_FAILURE;
}
catch (const exception & e)
{
cerr << e.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cerr << "some error" << endl;
return EXIT_FAILURE;
}