From 8e302b9d072ecaeb20900a64c5c08dbdf168fef5 Mon Sep 17 00:00:00 2001 From: John Zhao Date: Tue, 30 Oct 2018 15:56:09 +0800 Subject: [PATCH] Update dataset record --- tools/dataset/CMakeLists.txt | 6 ++ tools/dataset/dataset.cc | 28 ++++++++ tools/dataset/dataset.h | 4 ++ tools/dataset/record.cc | 56 +++++++--------- tools/dataset/record2.cc | 126 +++++++++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 32 deletions(-) create mode 100644 tools/dataset/record2.cc diff --git a/tools/dataset/CMakeLists.txt b/tools/dataset/CMakeLists.txt index 33d8809..6948cc9 100644 --- a/tools/dataset/CMakeLists.txt +++ b/tools/dataset/CMakeLists.txt @@ -27,3 +27,9 @@ make_executable(record LINK_LIBS mynteye ${OpenCV_LIBS} DLL_SEARCH_PATHS ${PRO_DIR}/_install/bin ${OpenCV_LIB_SEARCH_PATH} ) + +make_executable(record2 + SRCS record2.cc dataset.cc + LINK_LIBS mynteye ${OpenCV_LIBS} + DLL_SEARCH_PATHS ${PRO_DIR}/_install/bin ${OpenCV_LIB_SEARCH_PATH} +) diff --git a/tools/dataset/dataset.cc b/tools/dataset/dataset.cc index 390e1a3..628ae14 100644 --- a/tools/dataset/dataset.cc +++ b/tools/dataset/dataset.cc @@ -87,6 +87,34 @@ void Dataset::SaveMotionData(const device::MotionData &data) { ++motion_count_; } +void Dataset::SaveStreamData( + const Stream &stream, const api::StreamData &data) { + auto &&writer = GetStreamWriter(stream); + auto seq = stream_counts_[stream]; + writer->ofs << seq << ", " << data.img->frame_id << ", " + << data.img->timestamp << ", " << data.img->exposure_time + << std::endl; + if (!data.frame.empty()) { + std::stringstream ss; + ss << writer->outdir << MYNTEYE_OS_SEP << std::dec + << std::setw(IMAGE_FILENAME_WIDTH) << std::setfill('0') << seq << ".png"; + cv::imwrite(ss.str(), data.frame); + } + ++stream_counts_[stream]; +} + +void Dataset::SaveMotionData(const api::MotionData &data) { + auto &&writer = GetMotionWriter(); + auto seq = motion_count_; + writer->ofs << seq << ", " << data.imu->frame_id << ", " + << data.imu->timestamp << ", " << data.imu->accel[0] << ", " + << data.imu->accel[1] << ", " << data.imu->accel[2] << ", " + << data.imu->gyro[0] << ", " << data.imu->gyro[1] << ", " + << data.imu->gyro[2] << ", " << data.imu->temperature + << std::endl; + ++motion_count_; +} + Dataset::writer_t Dataset::GetStreamWriter(const Stream &stream) { try { return stream_writers_.at(stream); diff --git a/tools/dataset/dataset.h b/tools/dataset/dataset.h index 03edfa1..4e37740 100644 --- a/tools/dataset/dataset.h +++ b/tools/dataset/dataset.h @@ -21,6 +21,7 @@ #include #include "mynteye/mynteye.h" +#include "mynteye/api/api.h" #include "mynteye/device/callbacks.h" MYNTEYE_BEGIN_NAMESPACE @@ -43,6 +44,9 @@ class Dataset { void SaveStreamData(const Stream &stream, const device::StreamData &data); void SaveMotionData(const device::MotionData &data); + void SaveStreamData(const Stream &stream, const api::StreamData &data); + void SaveMotionData(const api::MotionData &data); + private: writer_t GetStreamWriter(const Stream &stream); writer_t GetMotionWriter(); diff --git a/tools/dataset/record.cc b/tools/dataset/record.cc index 63ada53..c9267ab 100644 --- a/tools/dataset/record.cc +++ b/tools/dataset/record.cc @@ -15,8 +15,7 @@ #include #include "mynteye/logger.h" -#include "mynteye/device/device.h" -#include "mynteye/device/utils.h" +#include "mynteye/api/api.h" #include "mynteye/util/times.h" #include "dataset/dataset.h" @@ -24,33 +23,31 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { - glog_init _(argc, argv); - - auto &&device = device::select(); - if (!device) + auto &&api = API::Create(argc, argv); + if (!api) return 1; /* { // auto-exposure - device->SetOptionValue(Option::EXPOSURE_MODE, 0); - device->SetOptionValue(Option::MAX_GAIN, 40); // [0.48] - device->SetOptionValue(Option::MAX_EXPOSURE_TIME, 120); // [0,240] - device->SetOptionValue(Option::DESIRED_BRIGHTNESS, 200); // [0,255] + api->SetOptionValue(Option::EXPOSURE_MODE, 0); + api->SetOptionValue(Option::MAX_GAIN, 40); // [0.48] + api->SetOptionValue(Option::MAX_EXPOSURE_TIME, 120); // [0,240] + api->SetOptionValue(Option::DESIRED_BRIGHTNESS, 200); // [0,255] } { // manual-exposure - device->SetOptionValue(Option::EXPOSURE_MODE, 1); - device->SetOptionValue(Option::GAIN, 20); // [0.48] - device->SetOptionValue(Option::BRIGHTNESS, 20); // [0,240] - device->SetOptionValue(Option::CONTRAST, 20); // [0,255] + api->SetOptionValue(Option::EXPOSURE_MODE, 1); + api->SetOptionValue(Option::GAIN, 20); // [0.48] + api->SetOptionValue(Option::BRIGHTNESS, 20); // [0,240] + api->SetOptionValue(Option::CONTRAST, 20); // [0,255] } - device->SetOptionValue(Option::IR_CONTROL, 80); - device->SetOptionValue(Option::FRAME_RATE, 25); - device->SetOptionValue(Option::IMU_FREQUENCY, 500); + api->SetOptionValue(Option::IR_CONTROL, 80); + api->SetOptionValue(Option::FRAME_RATE, 25); + api->SetOptionValue(Option::IMU_FREQUENCY, 500); */ - device->LogOptionInfos(); + api->LogOptionInfos(); // Enable this will cache the motion datas until you get them. - device->EnableMotionDatas(); - device->Start(Source::ALL); + api->EnableMotionDatas(); + api->Start(Source::ALL); const char *outdir; if (argc >= 2) { @@ -66,22 +63,17 @@ int main(int argc, char *argv[]) { std::size_t imu_count = 0; auto &&time_beg = times::now(); while (true) { - device->WaitForStreams(); + api->WaitForStreams(); - auto &&left_datas = device->GetStreamDatas(Stream::LEFT); - auto &&right_datas = device->GetStreamDatas(Stream::RIGHT); + auto &&left_datas = api->GetStreamDatas(Stream::LEFT); + auto &&right_datas = api->GetStreamDatas(Stream::RIGHT); img_count += left_datas.size(); - auto &&motion_datas = device->GetMotionDatas(); + auto &&motion_datas = api->GetMotionDatas(); imu_count += motion_datas.size(); - auto &&left_frame = left_datas.back().frame; - auto &&right_frame = right_datas.back().frame; - cv::Mat left_img( - left_frame->height(), left_frame->width(), CV_8UC1, left_frame->data()); - cv::Mat right_img( - right_frame->height(), right_frame->width(), CV_8UC1, - right_frame->data()); + auto &&left_img = left_datas.back().frame; + auto &&right_img = right_datas.back().frame; cv::Mat img; cv::hconcat(left_img, right_img, img); @@ -111,7 +103,7 @@ int main(int argc, char *argv[]) { std::cout << " to " << outdir << std::endl; auto &&time_end = times::now(); - device->Stop(Source::ALL); + api->Stop(Source::ALL); float elapsed_ms = times::count(time_end - time_beg) * 0.001f; diff --git a/tools/dataset/record2.cc b/tools/dataset/record2.cc new file mode 100644 index 0000000..63ada53 --- /dev/null +++ b/tools/dataset/record2.cc @@ -0,0 +1,126 @@ +// Copyright 2018 Slightech Co., Ltd. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include +#include + +#include "mynteye/logger.h" +#include "mynteye/device/device.h" +#include "mynteye/device/utils.h" +#include "mynteye/util/times.h" + +#include "dataset/dataset.h" + +MYNTEYE_USE_NAMESPACE + +int main(int argc, char *argv[]) { + glog_init _(argc, argv); + + auto &&device = device::select(); + if (!device) + return 1; + /* + { // auto-exposure + device->SetOptionValue(Option::EXPOSURE_MODE, 0); + device->SetOptionValue(Option::MAX_GAIN, 40); // [0.48] + device->SetOptionValue(Option::MAX_EXPOSURE_TIME, 120); // [0,240] + device->SetOptionValue(Option::DESIRED_BRIGHTNESS, 200); // [0,255] + } + { // manual-exposure + device->SetOptionValue(Option::EXPOSURE_MODE, 1); + device->SetOptionValue(Option::GAIN, 20); // [0.48] + device->SetOptionValue(Option::BRIGHTNESS, 20); // [0,240] + device->SetOptionValue(Option::CONTRAST, 20); // [0,255] + } + device->SetOptionValue(Option::IR_CONTROL, 80); + device->SetOptionValue(Option::FRAME_RATE, 25); + device->SetOptionValue(Option::IMU_FREQUENCY, 500); + */ + device->LogOptionInfos(); + + // Enable this will cache the motion datas until you get them. + device->EnableMotionDatas(); + device->Start(Source::ALL); + + const char *outdir; + if (argc >= 2) { + outdir = argv[1]; + } else { + outdir = "./dataset"; + } + tools::Dataset dataset(outdir); + + cv::namedWindow("frame"); + + std::size_t img_count = 0; + std::size_t imu_count = 0; + auto &&time_beg = times::now(); + while (true) { + device->WaitForStreams(); + + auto &&left_datas = device->GetStreamDatas(Stream::LEFT); + auto &&right_datas = device->GetStreamDatas(Stream::RIGHT); + img_count += left_datas.size(); + + auto &&motion_datas = device->GetMotionDatas(); + imu_count += motion_datas.size(); + + auto &&left_frame = left_datas.back().frame; + auto &&right_frame = right_datas.back().frame; + cv::Mat left_img( + left_frame->height(), left_frame->width(), CV_8UC1, left_frame->data()); + cv::Mat right_img( + right_frame->height(), right_frame->width(), CV_8UC1, + right_frame->data()); + + cv::Mat img; + cv::hconcat(left_img, right_img, img); + cv::imshow("frame", img); + + { // save + for (auto &&left : left_datas) { + dataset.SaveStreamData(Stream::LEFT, left); + } + for (auto &&right : right_datas) { + dataset.SaveStreamData(Stream::RIGHT, right); + } + + for (auto &&motion : motion_datas) { + dataset.SaveMotionData(motion); + } + + std::cout << "\rSaved " << img_count << " imgs" + << ", " << imu_count << " imus" << std::flush; + } + + char key = static_cast(cv::waitKey(1)); + if (key == 27 || key == 'q' || key == 'Q') { // ESC/Q + break; + } + } + std::cout << " to " << outdir << std::endl; + auto &&time_end = times::now(); + + device->Stop(Source::ALL); + + float elapsed_ms = + times::count(time_end - time_beg) * 0.001f; + LOG(INFO) << "Time beg: " << times::to_local_string(time_beg) + << ", end: " << times::to_local_string(time_end) + << ", cost: " << elapsed_ms << "ms"; + LOG(INFO) << "Img count: " << img_count + << ", fps: " << (1000.f * img_count / elapsed_ms); + LOG(INFO) << "Imu count: " << imu_count + << ", hz: " << (1000.f * imu_count / elapsed_ms); + return 0; +}