MYNT-EYE-S-SDK/samples/uvc/camera.cc

166 lines
4.5 KiB
C++
Raw Normal View History

2018-05-10 09:46:34 +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.
2018-03-25 18:03:04 +03:00
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <chrono>
#include <condition_variable>
#include <iomanip>
#include <iostream>
#include <mutex>
2018-04-09 04:28:58 +03:00
#include "mynteye/glog_init.h"
2018-03-22 07:03:08 +02:00
#include "mynteye/mynteye.h"
2018-04-06 04:12:09 +03:00
#include "mynteye/types.h"
2018-03-22 07:03:08 +02:00
#include "uvc/uvc.h"
2018-03-25 18:03:04 +03:00
struct frame {
const void *data = nullptr;
2018-05-15 12:32:04 +03:00
std::function<void()> continuation = nullptr;
frame() {
// VLOG(2) << __func__;
}
2018-03-25 18:03:04 +03:00
~frame() {
2018-05-15 12:32:04 +03:00
// VLOG(2) << __func__;
2018-03-25 18:03:04 +03:00
data = nullptr;
2018-05-15 12:32:04 +03:00
if (continuation) {
continuation();
continuation = nullptr;
}
2018-03-25 18:03:04 +03:00
}
};
2018-03-22 07:03:08 +02:00
MYNTEYE_USE_NAMESPACE
int main(int argc, char *argv[]) {
glog_init _(argc, argv);
2018-03-25 18:03:04 +03:00
std::vector<std::shared_ptr<uvc::device>> mynteye_devices;
2018-03-22 07:03:08 +02:00
auto context = uvc::create_context();
auto devices = uvc::query_devices(context);
2018-05-15 12:32:04 +03:00
if (devices.size() <= 0) {
LOG(ERROR) << "No devices :(";
return 1;
}
2018-03-22 07:03:08 +02:00
for (auto &&device : devices) {
2018-03-25 18:03:04 +03:00
auto vid = uvc::get_vendor_id(*device);
// auto pid = uvc::get_product_id(*device);
// LOG(INFO) << "vid: " << vid << ", pid: " << pid;
if (vid == MYNTEYE_VID) {
mynteye_devices.push_back(device);
}
}
// std::string dashes(80, '-');
size_t n = mynteye_devices.size();
2018-05-15 12:32:04 +03:00
if (n <= 0) {
LOG(ERROR) << "No MYNT EYE devices :(";
return 1;
}
2018-03-25 18:03:04 +03:00
2018-03-31 10:54:37 +03:00
LOG(INFO) << "MYNT EYE devices: ";
2018-03-25 18:03:04 +03:00
for (size_t i = 0; i < n; i++) {
auto device = mynteye_devices[i];
2018-03-30 11:39:35 +03:00
auto name = uvc::get_video_name(*device);
2018-03-22 07:03:08 +02:00
auto vid = uvc::get_vendor_id(*device);
auto pid = uvc::get_product_id(*device);
2018-03-31 10:54:37 +03:00
LOG(INFO) << " index: " << i << ", name: " << name << ", vid: 0x"
<< std::hex << vid << ", pid: 0x" << std::hex << pid;
2018-03-25 18:03:04 +03:00
}
std::shared_ptr<uvc::device> device = nullptr;
if (n <= 1) {
device = mynteye_devices[0];
LOG(INFO) << "Only one MYNT EYE device, select index: 0";
} else {
while (true) {
size_t i;
LOG(INFO) << "There are " << n << " MYNT EYE devices, select index: ";
std::cin >> i;
if (i >= n) {
LOG(WARNING) << "Index out of range :(";
continue;
}
device = mynteye_devices[i];
break;
}
}
std::mutex mtx;
std::condition_variable cv;
2018-05-15 12:32:04 +03:00
std::shared_ptr<frame> frame = nullptr;
const auto frame_ready = [&frame]() { return frame != nullptr; };
const auto frame_empty = [&frame]() { return frame == nullptr; };
2018-03-25 18:03:04 +03:00
uvc::set_device_mode(
*device, 1280, 400, static_cast<int>(Format::RGB888), 20,
2018-05-15 12:32:04 +03:00
[&mtx, &cv, &frame, &frame_ready](
const void *data, std::function<void()> continuation) {
2018-03-25 18:03:04 +03:00
// reinterpret_cast<const std::uint8_t *>(data);
std::unique_lock<std::mutex> lock(mtx);
2018-05-15 12:32:04 +03:00
if (frame == nullptr) {
frame = std::make_shared<struct frame>();
2018-06-02 11:04:11 +03:00
} else {
if (frame->continuation) {
frame->continuation();
}
2018-05-15 12:32:04 +03:00
}
frame->data = data; // not copy here
frame->continuation = continuation;
2018-03-25 18:03:04 +03:00
if (frame_ready())
cv.notify_one();
});
2018-03-30 11:24:43 +03:00
LOG(INFO) << "Press ESC/Q on windows to terminate";
2018-03-25 18:03:04 +03:00
cv::namedWindow("frame");
uvc::start_streaming(*device, 0);
double t, fps = 0;
while (true) {
t = static_cast<double>(cv::getTickCount());
std::unique_lock<std::mutex> lock(mtx);
if (frame_empty()) {
if (!cv.wait_for(lock, std::chrono::seconds(2), frame_ready))
throw std::runtime_error("Timeout waiting for frame.");
}
2018-05-15 12:32:04 +03:00
// only lastest frame is valid
cv::Mat img(400, 1280, CV_8UC3, const_cast<void *>(frame->data));
// cv::cvtColor(img, img, cv::COLOR_YUV2BGR_YUY2);
2018-03-25 18:03:04 +03:00
cv::imshow("frame", img);
2018-05-15 12:32:04 +03:00
frame = nullptr;
2018-03-25 18:03:04 +03:00
char key = static_cast<char>(cv::waitKey(1));
if (key == 27 || key == 'q' || key == 'Q') { // ESC/Q
break;
}
t = static_cast<double>(cv::getTickCount() - t);
fps = cv::getTickFrequency() / t;
2018-03-22 07:03:08 +02:00
}
2018-03-25 18:03:04 +03:00
UNUSED(fps)
2018-03-22 07:03:08 +02:00
2018-03-25 18:03:04 +03:00
uvc::stop_streaming(*device);
// cv::destroyAllWindows();
2018-03-22 07:03:08 +02:00
return 0;
}