basalt/src/rs_t265_record.cpp

472 lines
15 KiB
C++
Raw Normal View History

2019-06-13 13:37:17 +02:00
/**
BSD 3-Clause License
This file is part of the Basalt project.
https://gitlab.com/VladyslavUsenko/basalt.git
Copyright (c) 2019, Vladyslav Usenko, Michael Loipführer and Nikolaus Demmel.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <math.h>
#include <atomic>
2019-06-03 13:37:53 +02:00
#include <cstring>
#include <fstream>
#include <iomanip>
#include <thread>
#include <librealsense2/rs.hpp>
#include <pangolin/display/image_view.h>
#include <pangolin/gl/gldraw.h>
#include <pangolin/image/image.h>
#include <pangolin/image/image_io.h>
#include <pangolin/image/typed_image.h>
#include <pangolin/pangolin.h>
2019-06-13 14:01:03 +02:00
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
2019-06-03 13:37:53 +02:00
#include <tbb/concurrent_queue.h>
2019-06-13 13:37:17 +02:00
#include <basalt/device/rs_t265.h>
#include <basalt/serialization/headers_serialization.h>
2019-06-03 13:37:53 +02:00
#include <CLI/CLI.hpp>
#include <cereal/archives/json.hpp>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
constexpr int UI_WIDTH = 200;
2019-06-13 18:21:37 +02:00
basalt::RsT265Device::Ptr t265_device;
2019-06-13 13:37:17 +02:00
std::shared_ptr<pangolin::DataLog> imu_log;
2019-06-03 13:37:53 +02:00
pangolin::Var<int> webp_quality("ui.webp_quality", 90, 0, 101);
pangolin::Var<int> skip_frames("ui.skip_frames", 1, 1, 10);
2019-06-13 13:37:17 +02:00
pangolin::Var<float> exposure("ui.exposure", 5.0, 1, 20);
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
tbb::concurrent_bounded_queue<basalt::OpticalFlowInput::Ptr> image_data_queue;
tbb::concurrent_bounded_queue<basalt::ImuData::Ptr> imu_data_queue;
tbb::concurrent_bounded_queue<basalt::RsPoseData> pose_data_queue;
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
std::atomic<bool> stop_workers;
std::atomic<bool> recording;
2019-06-03 13:37:53 +02:00
std::string dataset_dir;
2019-06-13 18:21:37 +02:00
static constexpr int NUM_CAMS = basalt::RsT265Device::NUM_CAMS;
2019-06-13 13:37:17 +02:00
static constexpr int NUM_WORKERS = 8;
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
std::ofstream cam_data[NUM_CAMS], exposure_data[NUM_CAMS], imu0_data, pose_data;
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
std::vector<std::thread> worker_threads;
std::thread imu_worker_thread, pose_worker_thread;
// manual exposure mode, if not enabled will also record pose data
bool manual_exposure;
2019-06-03 13:37:53 +02:00
void image_save_worker() {
2019-06-13 13:37:17 +02:00
basalt::OpticalFlowInput::Ptr img;
2019-06-03 13:37:53 +02:00
while (!stop_workers) {
2019-06-13 13:37:17 +02:00
if (image_data_queue.try_pop(img)) {
if (recording) {
2019-06-13 13:37:17 +02:00
for (size_t cam_id = 0; cam_id < NUM_CAMS; ++cam_id) {
#if CV_MAJOR_VERSION >= 3
cam_data[cam_id] << img->t_ns << "," << img->t_ns << ".webp"
<< std::endl;
#else
cam_data[cam_id] << img->t_ns << "," << img->t_ns << ".jpg"
<< std::endl;
#endif
exposure_data[cam_id] << img->t_ns << ","
<< int64_t(img->img_data[cam_id].exposure * 1e9)
<< std::endl;
}
for (size_t cam_id = 0; cam_id < NUM_CAMS; ++cam_id) {
basalt::ManagedImage<uint16_t>::Ptr image_raw =
img->img_data[cam_id].img;
2019-06-13 13:37:17 +02:00
if (!image_raw.get()) continue;
2019-06-13 13:37:17 +02:00
cv::Mat image(image_raw->h, image_raw->w, CV_8U);
2019-06-13 13:37:17 +02:00
uint8_t *dst = image.ptr();
const uint16_t *src = image_raw->ptr;
2019-06-13 13:37:17 +02:00
for (size_t i = 0; i < image_raw->size(); i++) {
dst[i] = (src[i] >> 8);
}
2019-06-13 13:37:17 +02:00
2019-06-03 15:44:59 +02:00
#if CV_MAJOR_VERSION >= 3
std::string filename = dataset_dir + "mav0/cam" +
std::to_string(cam_id) + "/data/" +
std::to_string(img->t_ns) + ".webp";
2019-06-03 13:37:53 +02:00
std::vector<int> compression_params = {cv::IMWRITE_WEBP_QUALITY,
webp_quality};
cv::imwrite(filename, image, compression_params);
2019-06-03 15:29:52 +02:00
#else
std::string filename = dataset_dir + "mav0/cam" +
std::to_string(cam_id) + "/data/" +
std::to_string(img->t_ns) + ".jpg";
2019-06-03 13:37:53 +02:00
std::vector<int> compression_params = {cv::IMWRITE_JPEG_QUALITY,
webp_quality};
cv::imwrite(filename, image, compression_params);
2019-06-03 15:29:52 +02:00
#endif
}
2019-06-13 13:37:17 +02:00
}
2019-06-03 13:37:53 +02:00
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
2019-06-13 13:37:17 +02:00
void imu_save_worker() {
basalt::ImuData::Ptr data;
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
while (!stop_workers) {
if (imu_data_queue.try_pop(data)) {
if (imu_log.get())
imu_log->Log(data->accel[0], data->accel[1], data->accel[2]);
if (recording) {
imu0_data << data->t_ns << "," << data->gyro[0] << "," << data->gyro[1]
<< "," << data->gyro[2] << "," << data->accel[0] << ","
<< data->accel[1] << "," << data->accel[2] << "\n";
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
2019-06-03 13:37:53 +02:00
}
}
2019-06-13 13:37:17 +02:00
void pose_save_worker() {
basalt::RsPoseData data;
while (!stop_workers) {
if (pose_data_queue.try_pop(data)) {
if (recording) {
pose_data << data.t_ns << "," << data.data.translation().x() << ","
<< data.data.translation().y() << ","
<< data.data.translation().z() << ","
<< data.data.unit_quaternion().w() << ","
<< data.data.unit_quaternion().x() << ","
<< data.data.unit_quaternion().y() << ","
<< data.data.unit_quaternion().z() << std::endl;
2019-06-03 13:37:53 +02:00
}
2019-06-13 13:37:17 +02:00
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
2019-06-03 13:37:53 +02:00
}
2019-06-13 13:37:17 +02:00
}
}
2019-06-03 13:37:53 +02:00
2019-06-13 18:21:37 +02:00
void save_calibration(const basalt::RsT265Device::Ptr &device) {
2019-06-13 13:37:17 +02:00
auto calib = device->exportCalibration();
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
if (calib) {
std::ofstream os(dataset_dir + "/calibration.json");
cereal::JSONOutputArchive archive(os);
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
archive(*calib);
2019-06-03 13:37:53 +02:00
}
2019-06-13 13:37:17 +02:00
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
inline std::string get_date() {
constexpr int MAX_DATE = 64;
time_t now;
char the_date[MAX_DATE];
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
the_date[0] = '\0';
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
now = time(nullptr);
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
if (now != -1) {
strftime(the_date, MAX_DATE, "%Y_%m_%d_%H_%M_%S", gmtime(&now));
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
return std::string(the_date);
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
void startRecording(const std::string &dir_path) {
if (!recording) {
dataset_dir = dir_path + "dataset_" + get_date() + "/";
fs::create_directory(dataset_dir);
fs::create_directory(dataset_dir + "mav0/");
fs::create_directory(dataset_dir + "mav0/cam0/");
fs::create_directory(dataset_dir + "mav0/cam0/data/");
fs::create_directory(dataset_dir + "mav0/cam1/");
fs::create_directory(dataset_dir + "mav0/cam1/data/");
fs::create_directory(dataset_dir + "mav0/imu0/");
cam_data[0].open(dataset_dir + "mav0/cam0/data.csv");
cam_data[1].open(dataset_dir + "mav0/cam1/data.csv");
exposure_data[0].open(dataset_dir + "mav0/cam0/exposure.csv");
exposure_data[1].open(dataset_dir + "mav0/cam1/exposure.csv");
imu0_data.open(dataset_dir + "mav0/imu0/data.csv");
if (!manual_exposure) {
fs::create_directory(dataset_dir + "mav0/realsense0/");
pose_data.open(dataset_dir + "mav0/realsense0/data.csv");
pose_data << "#timestamp [ns], p_RS_R_x [m], p_RS_R_y [m], p_RS_R_z [m], "
"q_RS_w [], q_RS_x [], q_RS_y [], q_RS_z []\n";
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
cam_data[0] << "#timestamp [ns], filename\n";
cam_data[1] << "#timestamp [ns], filename\n";
exposure_data[0] << "#timestamp [ns], exposure time[ns]\n";
exposure_data[1] << "#timestamp [ns], exposure time[ns]\n";
imu0_data << "#timestamp [ns],w_RS_S_x [rad s^-1],w_RS_S_y [rad "
"s^-1],w_RS_S_z [rad s^-1],a_RS_S_x [m s^-2],a_RS_S_y "
"[m s^-2],a_RS_S_z [m s^-2]\n";
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
save_calibration(t265_device);
t265_device->image_data_queue->clear();
t265_device->imu_data_queue->clear();
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
std::cout << "Started recording dataset in " << dataset_dir << std::endl;
recording = true;
} else {
std::cout << "Already recording" << std::endl;
2019-06-03 13:37:53 +02:00
}
2019-06-13 13:37:17 +02:00
}
void stopRecording() {
if (recording) {
recording = false;
cam_data[0].close();
cam_data[1].close();
exposure_data[0].close();
exposure_data[1].close();
imu0_data.close();
pose_data.close();
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
std::cout << "Stopped recording dataset in " << dataset_dir << std::endl;
}
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
void toggleRecording(const std::string &dir_path) {
if (recording) {
stopRecording();
} else {
startRecording(dir_path);
}
2019-06-03 13:37:53 +02:00
}
int main(int argc, char *argv[]) {
CLI::App app{"Record RealSense T265 Data"};
2019-06-13 13:37:17 +02:00
std::string dataset_path;
app.add_option("--dataset-path", dataset_path, "Path to dataset");
app.add_flag("--manual-exposure", manual_exposure,
"If set will enable manual exposure.");
2019-06-03 13:37:53 +02:00
try {
app.parse(argc, argv);
} catch (const CLI::ParseError &e) {
return app.exit(e);
}
if (dataset_path[dataset_path.length() - 1] != '/') {
dataset_path += '/';
}
2019-06-03 13:37:53 +02:00
bool show_gui = true;
stop_workers = false;
2019-06-13 13:37:17 +02:00
if (worker_threads.empty()) {
for (int i = 0; i < NUM_WORKERS; i++) {
worker_threads.emplace_back(image_save_worker);
2019-06-03 13:37:53 +02:00
}
2019-06-13 13:37:17 +02:00
}
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
imu_worker_thread = std::thread(imu_save_worker);
pose_worker_thread = std::thread(pose_save_worker);
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
image_data_queue.set_capacity(1000);
imu_data_queue.set_capacity(10000);
pose_data_queue.set_capacity(10000);
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
// realsense
2019-06-13 18:21:37 +02:00
t265_device.reset(new basalt::RsT265Device(manual_exposure, skip_frames,
webp_quality, exposure));
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
t265_device->image_data_queue = &image_data_queue;
t265_device->imu_data_queue = &imu_data_queue;
t265_device->pose_data_queue = &pose_data_queue;
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
t265_device->start();
imu_log.reset(new pangolin::DataLog);
2019-06-03 13:37:53 +02:00
if (show_gui) {
pangolin::CreateWindowAndBind("Record RealSense T265", 1200, 800);
2019-06-13 13:37:17 +02:00
pangolin::Var<std::function<void(void)>> record_btn(
"ui.record", [&] { return toggleRecording(dataset_path); });
pangolin::Var<std::function<void(void)>> export_calibration(
"ui.export_calib", [&] { return save_calibration(t265_device); });
2019-06-03 13:37:53 +02:00
std::atomic<int64_t> record_t_ns;
record_t_ns = 0;
glEnable(GL_DEPTH_TEST);
pangolin::View &img_view_display =
pangolin::CreateDisplay()
.SetBounds(0.4, 1.0, pangolin::Attach::Pix(UI_WIDTH), 1.0)
.SetLayout(pangolin::LayoutEqual);
pangolin::View &plot_display = pangolin::CreateDisplay().SetBounds(
0.0, 0.4, pangolin::Attach::Pix(UI_WIDTH), 1.0);
pangolin::CreatePanel("ui").SetBounds(0.0, 1.0, 0.0,
pangolin::Attach::Pix(UI_WIDTH));
std::vector<std::shared_ptr<pangolin::ImageView>> img_view;
2019-06-13 18:21:37 +02:00
while (img_view.size() < basalt::RsT265Device::NUM_CAMS) {
2019-06-03 13:37:53 +02:00
int idx = img_view.size();
std::shared_ptr<pangolin::ImageView> iv(new pangolin::ImageView);
iv->extern_draw_function = [&, idx](pangolin::View &v) {
glLineWidth(1.0);
glColor3f(1.0, 0.0, 0.0); // red
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2019-06-13 13:37:17 +02:00
if (t265_device->last_img_data.get())
2019-06-03 13:37:53 +02:00
pangolin::GlFont::I()
.Text("Exposure: %.3f ms.",
2019-06-13 13:37:17 +02:00
t265_device->last_img_data->img_data[idx].exposure * 1000.0)
2019-06-03 13:37:53 +02:00
.Draw(30, 30);
if (idx == 0) {
pangolin::GlFont::I()
2019-06-13 13:37:17 +02:00
.Text("Queue: %d.", image_data_queue.size())
2019-06-03 13:37:53 +02:00
.Draw(30, 60);
}
2019-06-13 13:37:17 +02:00
if (idx == 0 && recording) {
2019-06-03 13:37:53 +02:00
pangolin::GlFont::I().Text("Recording").Draw(30, 90);
}
};
iv->OnSelectionCallback =
[&](pangolin::ImageView::OnSelectionEventData o) {
int64_t curr_t_ns = std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count();
if (std::abs(record_t_ns - curr_t_ns) > int64_t(2e9)) {
toggleRecording(dataset_path);
record_t_ns = curr_t_ns;
}
};
2019-06-03 13:37:53 +02:00
img_view.push_back(iv);
img_view_display.AddDisplay(*iv);
}
2019-06-13 13:37:17 +02:00
imu_log->Clear();
2019-06-03 13:37:53 +02:00
std::vector<std::string> labels;
labels.push_back(std::string("accel x"));
labels.push_back(std::string("accel y"));
labels.push_back(std::string("accel z"));
2019-06-13 13:37:17 +02:00
imu_log->SetLabels(labels);
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
pangolin::Plotter plotter(imu_log.get(), 0.0f, 2000.0f, -15.0f, 15.0f, 0.1f,
2019-06-03 13:37:53 +02:00
0.1f);
plotter.SetBounds(0.0, 1.0, 0.0, 1.0);
plotter.Track("$i");
plot_display.AddDisplay(plotter);
plotter.ClearSeries();
plotter.AddSeries("$i", "$0", pangolin::DrawingModeLine,
pangolin::Colour::Red(), "accel x");
plotter.AddSeries("$i", "$1", pangolin::DrawingModeLine,
pangolin::Colour::Green(), "accel y");
plotter.AddSeries("$i", "$2", pangolin::DrawingModeLine,
pangolin::Colour::Blue(), "accel z");
while (!pangolin::ShouldQuit()) {
{
pangolin::GlPixFormat fmt;
fmt.glformat = GL_LUMINANCE;
2019-06-13 13:37:17 +02:00
fmt.gltype = GL_UNSIGNED_SHORT;
fmt.scalable_internal_format = GL_LUMINANCE16;
if (t265_device->last_img_data.get())
2019-06-13 18:21:37 +02:00
for (size_t cam_id = 0; cam_id < basalt::RsT265Device::NUM_CAMS;
cam_id++) {
2019-06-13 13:37:17 +02:00
if (t265_device->last_img_data->img_data[cam_id].img.get())
img_view[cam_id]->SetImage(
t265_device->last_img_data->img_data[cam_id].img->ptr,
t265_device->last_img_data->img_data[cam_id].img->w,
t265_device->last_img_data->img_data[cam_id].img->h,
t265_device->last_img_data->img_data[cam_id].img->pitch, fmt);
}
}
if (manual_exposure && exposure.GuiChanged()) {
t265_device->setExposure(exposure);
}
if (webp_quality.GuiChanged()) {
t265_device->setWebpQuality(webp_quality);
}
if (skip_frames.GuiChanged()) {
t265_device->setSkipFrames(skip_frames);
2019-06-03 13:37:53 +02:00
}
pangolin::FinishFrame();
std::this_thread::sleep_for(std::chrono::milliseconds(15));
}
}
2019-06-13 13:37:17 +02:00
if (recording) stopRecording();
2019-06-03 13:37:53 +02:00
stop_workers = true;
2019-06-13 13:37:17 +02:00
for (auto &t : worker_threads) t.join();
imu_worker_thread.join();
pose_worker_thread.join();
2019-06-03 13:37:53 +02:00
2019-06-13 13:37:17 +02:00
return EXIT_SUCCESS;
2019-06-03 13:37:53 +02:00
}