From fa528a7ebafc9b5751deda05c02aec43a9a45ead Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 31 Jul 2016 13:48:09 +0200 Subject: tracker/{aruco, pt}, compat, api: move opencv camera class to cv module --- cv/CMakeLists.txt | 6 ++++ cv/camera-dialog.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++ cv/camera-dialog.hpp | 44 +++++++++++++++++++++++++ cv/camera-dialog.hpp.OO4364 | 44 +++++++++++++++++++++++++ cv/export.hpp | 28 ++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 cv/CMakeLists.txt create mode 100644 cv/camera-dialog.cpp create mode 100644 cv/camera-dialog.hpp create mode 100644 cv/camera-dialog.hpp.OO4364 create mode 100644 cv/export.hpp (limited to 'cv') diff --git a/cv/CMakeLists.txt b/cv/CMakeLists.txt new file mode 100644 index 00000000..0f29c4f2 --- /dev/null +++ b/cv/CMakeLists.txt @@ -0,0 +1,6 @@ +find_package(OpenCV 3.0 QUIET) +if(OpenCV_FOUND) + opentrack_boilerplate(opentrack-cv STATIC) + target_link_libraries(opentrack-cv ${OpenCV_LIBS}) + target_include_directories(opentrack-cv SYSTEM PUBLIC ${OpenCV_INCLUDE_DIRS}) +endif() diff --git a/cv/camera-dialog.cpp b/cv/camera-dialog.cpp new file mode 100644 index 00000000..05161cb1 --- /dev/null +++ b/cv/camera-dialog.cpp @@ -0,0 +1,78 @@ +/* Copyright (c) 2015, Stanislaw Halik + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#include "cv/camera-dialog.hpp" +#include +#include + +void camera_dialog::maybe_grab_frame(cv::VideoCapture& cap) +{ + for (int i = 0; i < 60; i++) + { + if (cap.grab()) + break; + portable::sleep(50); + } +} + +void camera_dialog::init_com_threading() +{ + HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + if (FAILED(hr)) + qDebug() << "failed CoInitializeEx" << hr << "code" << GetLastError(); +} + +camera_dialog::~camera_dialog() {} + +void camera_dialog::open_camera_settings(cv::VideoCapture* cap, const QString& camera_name, QMutex* camera_mtx) +{ +#ifdef _WIN32 + if (cap) + { + QMutexLocker l(camera_mtx); + + if (cap->isOpened()) + { + init_com_threading(); + maybe_grab_frame(*cap); + cap->set(cv::CAP_PROP_SETTINGS, 1); + return; + } + } + + if (t.isActive()) + t.stop(); + + // don't hog the camera capture + if (!t.isSingleShot()) + QObject::connect(&t, &QTimer::timeout, [&]() -> void { delete_capture(); }); + + init_com_threading(); + fake_capture = cv::VideoCapture(camera_name_to_index(camera_name)); + maybe_grab_frame(fake_capture); + fake_capture.set(cv::CAP_PROP_SETTINGS, 1); + + t.setSingleShot(true); + t.setInterval(5000); + + // HACK: we're not notified when it's safe to close the capture + t.start(); +#elif defined(__linux) + int idx = camera_name_to_index(camera_name); + QProcess::startDetached("qv4l2", QStringList() << "-d" << ("/dev/video" + QString::number(idx))); +#else + // nothing +#endif +} + +#ifdef _WIN32 +void camera_dialog::delete_capture() +{ + fake_capture.open(""); +} +#endif diff --git a/cv/camera-dialog.hpp b/cv/camera-dialog.hpp new file mode 100644 index 00000000..e99b5f6e --- /dev/null +++ b/cv/camera-dialog.hpp @@ -0,0 +1,44 @@ +/* Copyright (c) 2015, Stanislaw Halik + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#pragma once + +#include "export.hpp" +#include "opentrack-compat/camera-names.hpp" +#include "opentrack-compat/sleep.hpp" + +#ifdef __linux +# include +#endif + +#ifdef _WIN32 +# include +# include +# include +# include +# include +#endif + +#include + +class OPENTRACK_CV_EXPORT camera_dialog +{ + static void maybe_grab_frame(cv::VideoCapture& cap); +#ifdef _WIN32 + static void init_com_threading(); +#endif + +public: + virtual ~camera_dialog(); + void open_camera_settings(cv::VideoCapture*, const QString&, QMutex*); +#if defined(_WIN32) + cv::VideoCapture fake_capture; + QTimer t; + void delete_capture(); +#endif +}; diff --git a/cv/camera-dialog.hpp.OO4364 b/cv/camera-dialog.hpp.OO4364 new file mode 100644 index 00000000..e99b5f6e --- /dev/null +++ b/cv/camera-dialog.hpp.OO4364 @@ -0,0 +1,44 @@ +/* Copyright (c) 2015, Stanislaw Halik + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#pragma once + +#include "export.hpp" +#include "opentrack-compat/camera-names.hpp" +#include "opentrack-compat/sleep.hpp" + +#ifdef __linux +# include +#endif + +#ifdef _WIN32 +# include +# include +# include +# include +# include +#endif + +#include + +class OPENTRACK_CV_EXPORT camera_dialog +{ + static void maybe_grab_frame(cv::VideoCapture& cap); +#ifdef _WIN32 + static void init_com_threading(); +#endif + +public: + virtual ~camera_dialog(); + void open_camera_settings(cv::VideoCapture*, const QString&, QMutex*); +#if defined(_WIN32) + cv::VideoCapture fake_capture; + QTimer t; + void delete_capture(); +#endif +}; diff --git a/cv/export.hpp b/cv/export.hpp new file mode 100644 index 00000000..6636e56b --- /dev/null +++ b/cv/export.hpp @@ -0,0 +1,28 @@ +#pragma once + +#ifdef BUILD_cv +# ifdef _WIN32 +# define OPENTRACK_CV_LINKAGE __declspec(dllexport) +# else +# define OPENTRACK_CV_LINKAGE +# endif + +# ifndef _MSC_VER +# define OPENTRACK_CV_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_CV_LINKAGE +# else +# define OPENTRACK_CV_EXPORT OPENTRACK_CV_LINKAGE +# endif + +#else + #ifdef _WIN32 + # define OPENTRACK_CV_LINKAGE __declspec(dllimport) + #else + # define OPENTRACK_CV_LINKAGE + #endif + + #ifndef _MSC_VER + # define OPENTRACK_CV_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_CV_LINKAGE + #else + # define OPENTRACK_CV_EXPORT OPENTRACK_CV_LINKAGE + #endif +#endif -- cgit v1.2.3