summaryrefslogtreecommitdiffhomepage
path: root/cv
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-07-31 13:48:09 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-07-31 13:48:09 +0200
commitfa528a7ebafc9b5751deda05c02aec43a9a45ead (patch)
treee23a0e74592670b23c5df3c6b58895255aaad581 /cv
parentccafc5f054733e883bb0ef8b0c698b4c9c86fff9 (diff)
tracker/{aruco, pt}, compat, api: move opencv camera class to cv module
Diffstat (limited to 'cv')
-rw-r--r--cv/CMakeLists.txt6
-rw-r--r--cv/camera-dialog.cpp78
-rw-r--r--cv/camera-dialog.hpp44
-rw-r--r--cv/camera-dialog.hpp.OO436444
-rw-r--r--cv/export.hpp28
5 files changed, 200 insertions, 0 deletions
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 <sthalik@misaki.pl>
+
+ * 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 <QDebug>
+#include <QMutexLocker>
+
+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 <sthalik@misaki.pl>
+
+ * 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 <QProcess>
+#endif
+
+#ifdef _WIN32
+# include <objbase.h>
+# include <winerror.h>
+# include <windows.h>
+# include <opencv2/videoio.hpp>
+# include <QTimer>
+#endif
+
+#include <QMutex>
+
+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 <sthalik@misaki.pl>
+
+ * 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 <QProcess>
+#endif
+
+#ifdef _WIN32
+# include <objbase.h>
+# include <winerror.h>
+# include <windows.h>
+# include <opencv2/videoio.hpp>
+# include <QTimer>
+#endif
+
+#include <QMutex>
+
+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