MYNT-EYE-S-SDK/samples/record.cc

163 lines
5.2 KiB
C++
Raw Normal View History

2019-09-02 05:17:06 +03:00
// 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 <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "mynteye/logger.h"
#include "mynteye/device/device.h"
#include "mynteye/device/utils.h"
#include "mynteye/util/times.h"
#include "dataset.h"
MYNTEYE_USE_NAMESPACE
int main(int argc, char *argv[]) {
glog_init _(argc, argv);
2019-10-22 06:29:22 +03:00
auto &&api = API::Create(argc, argv);
if (!api) return 1;
2019-09-02 05:17:06 +03:00
2019-10-22 06:29:22 +03:00
auto request = api->GetStreamRequest();
2019-09-02 05:17:06 +03:00
2019-10-22 06:29:22 +03:00
// struct StreamRequest {
// /** Stream width in pixels */
// std::uint16_t width;
// /** Stream height in pixels */
// std::uint16_t height;
// /** Stream pixel format */
// Format format;
// /** Stream frames per second */
// std::uint16_t fps;
// }
2019-09-02 05:17:06 +03:00
2019-10-22 11:56:54 +03:00
// request.fps = 10;
2019-10-22 06:29:22 +03:00
api->ConfigStreamRequest(request);
api->EnableMotionDatas();
api->EnableStreamData(Stream::DEPTH);
api->Start(Source::ALL);
2019-09-02 05:17:06 +03:00
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) {
2019-10-22 06:29:22 +03:00
api->WaitForStreams();
2019-09-02 05:17:06 +03:00
2019-10-22 06:29:22 +03:00
auto &&left_datas = api->GetStreamDatas(Stream::LEFT);
auto &&right_datas = api->GetStreamDatas(Stream::RIGHT);
2019-10-22 12:57:17 +03:00
auto &&depth_data = api->GetStreamData(Stream::DEPTH);
2019-10-22 13:07:16 +03:00
auto &&disparity_data = api->GetStreamData(Stream::DISPARITY);
2019-09-02 05:17:06 +03:00
img_count += left_datas.size();
2019-10-22 06:29:22 +03:00
auto &&motion_datas = api->GetMotionDatas();
2019-09-02 05:17:06 +03:00
imu_count += motion_datas.size();
cv::Mat img;
2019-10-22 12:07:34 +03:00
if (left_datas.size() > 0 && right_datas.size() > 0) {
2019-10-22 11:56:54 +03:00
auto &&left_frame = left_datas.back().frame_raw;
auto &&right_frame = right_datas.back().frame_raw;
2019-10-22 12:15:46 +03:00
if (right_frame->data() && left_frame->data()) {
if (left_frame->format() == Format::GREY) {
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::hconcat(left_img, right_img, img);
} else if (left_frame->format() == Format::YUYV) {
cv::Mat left_img(
left_frame->height(), left_frame->width(), CV_8UC2,
left_frame->data());
cv::Mat right_img(
right_frame->height(), right_frame->width(), CV_8UC2,
right_frame->data());
cv::cvtColor(left_img, left_img, cv::COLOR_YUV2BGR_YUY2);
cv::cvtColor(right_img, right_img, cv::COLOR_YUV2BGR_YUY2);
cv::hconcat(left_img, right_img, img);
} else if (left_frame->format() == Format::BGR888) {
cv::Mat left_img(
left_frame->height(), left_frame->width(), CV_8UC3,
left_frame->data());
cv::Mat right_img(
right_frame->height(), right_frame->width(), CV_8UC3,
right_frame->data());
cv::hconcat(left_img, right_img, img);
} else {
return -1;
}
cv::imshow("frame", img);
2019-10-22 11:56:54 +03:00
}
2019-09-02 05:17:06 +03:00
}
2019-10-22 11:56:54 +03:00
2019-09-02 05:17:06 +03:00
if (img_count > 10 && imu_count > 50) { // save
// save Stream::LEFT
2019-09-02 05:17:06 +03:00
for (auto &&left : left_datas) {
dataset.SaveStreamData(Stream::LEFT, left);
}
// save Stream::RIGHT
// for (auto &&right : right_datas) {
// dataset.SaveStreamData(Stream::RIGHT, right);
// }
2019-10-22 12:57:17 +03:00
// save Stream::DEPTH
2019-10-22 12:57:17 +03:00
if (!depth_data.frame.empty()) {
dataset.SaveStreamData(Stream::DEPTH, depth_data);
2019-10-22 12:50:16 +03:00
}
2019-10-22 12:57:17 +03:00
// save Stream::DISPARITY
2019-10-22 13:07:16 +03:00
if (!disparity_data.frame.empty()) {
dataset.SaveStreamData(Stream::DISPARITY, disparity_data);
}
2019-09-02 05:17:06 +03:00
for (auto &&motion : motion_datas) {
dataset.SaveMotionData(motion);
}
std::cout << "\rSaved " << img_count << " imgs"
<< ", " << imu_count << " imus" << std::flush;
}
char key = static_cast<char>(cv::waitKey(1));
if (key == 27 || key == 'q' || key == 'Q') { // ESC/Q
break;
}
}
std::cout << " to " << outdir << std::endl;
auto &&time_end = times::now();
2019-10-22 06:29:22 +03:00
api->Stop(Source::ALL);
2019-09-02 05:17:06 +03:00
float elapsed_ms =
times::count<times::microseconds>(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;
}