diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2016-07-31 13:48:09 +0200 | 
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-07-31 13:48:09 +0200 | 
| commit | fa528a7ebafc9b5751deda05c02aec43a9a45ead (patch) | |
| tree | e23a0e74592670b23c5df3c6b58895255aaad581 | |
| parent | ccafc5f054733e883bb0ef8b0c698b4c9c86fff9 (diff) | |
tracker/{aruco, pt}, compat, api: move opencv camera class to cv module
| -rw-r--r-- | cv/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | cv/camera-dialog.cpp | 78 | ||||
| -rw-r--r-- | cv/camera-dialog.hpp | 44 | ||||
| -rw-r--r-- | cv/camera-dialog.hpp.OO4364 | 44 | ||||
| -rw-r--r-- | cv/export.hpp | 28 | ||||
| -rw-r--r-- | opentrack-compat/camera-names.cpp | 5 | ||||
| -rw-r--r-- | opentrack/opencv-camera-dialog.hpp | 108 | ||||
| -rw-r--r-- | tracker-aruco/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tracker-pt/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tracker-pt/ftnoir_tracker_pt.h | 2 | ||||
| -rw-r--r-- | tracker-pt/ftnoir_tracker_pt_dialog.h | 2 | 
11 files changed, 207 insertions, 114 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 diff --git a/opentrack-compat/camera-names.cpp b/opentrack-compat/camera-names.cpp index ef5c159d..a83a6536 100644 --- a/opentrack-compat/camera-names.cpp +++ b/opentrack-compat/camera-names.cpp @@ -26,7 +26,8 @@ OPENTRACK_COMPAT_EXPORT int camera_name_to_index(const QString &name)      return ret;  } -OPENTRACK_COMPAT_EXPORT QList<QString> get_camera_names() { +OPENTRACK_COMPAT_EXPORT QList<QString> get_camera_names() +{      QList<QString> ret;  #if defined(_WIN32)      // Create the System Device Enumerator. @@ -79,7 +80,7 @@ OPENTRACK_COMPAT_EXPORT QList<QString> get_camera_names() {      }      else          qDebug() << "failed CLSID_VideoInputDeviceCategory" << hr; -     +      pSysDevEnum->Release();  #endif  #ifdef __linux diff --git a/opentrack/opencv-camera-dialog.hpp b/opentrack/opencv-camera-dialog.hpp deleted file mode 100644 index 7300664c..00000000 --- a/opentrack/opencv-camera-dialog.hpp +++ /dev/null @@ -1,108 +0,0 @@ -/* 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 - -#if !defined(QT_MOC_RUN) - -#include <QTimer> -#include <QMutex> -#include <QMutexLocker> -#include <opencv2/videoio.hpp> -#include "opentrack-compat/camera-names.hpp" - -#ifdef __linux -#   include <QProcess> -#endif - -#include "opentrack-compat/sleep.hpp" - -#ifdef _WIN32 -#   include <objbase.h> -#   include <winerror.h> -#   include <windows.h> -#endif - -#ifdef _WIN32 -static void init_com_threading(void) -{ -    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); -    if (FAILED(hr)) -        qDebug() << "failed CoInitializeEx" << hr << "code" << GetLastError(); -} -#endif - -static void maybe_grab_frame(cv::VideoCapture& cap) -{ -    for (int i = 0; i < 60; i++) -    { -        if (cap.grab()) -            break; -        portable::sleep(50); -    } -} - -class camera_dialog -{ -public: -    inline virtual ~camera_dialog() {} -#ifdef __linux -    void open_camera_settings(cv::VideoCapture *, const QString &camera_name, QMutex *) -    { -        int idx = camera_name_to_index(camera_name); -        QProcess::startDetached("qv4l2", QStringList() << "-d" << ("/dev/video" + QString::number(idx))); -    } -#elif defined(_WIN32) -    void open_camera_settings(cv::VideoCapture* cap, const QString& camera_name, QMutex* camera_mtx) -    { -        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()) -            return; - -        // 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(); -    } -#else -    void open_camera_settings(cv::VideoCapture*, const QString&, QMutex*) {} -#endif -private: -#if defined(_WIN32) -    cv::VideoCapture fake_capture; -    QTimer t; -    void delete_capture() -    { -        fake_capture.open(""); -    } -#endif -}; - -#endif diff --git a/tracker-aruco/CMakeLists.txt b/tracker-aruco/CMakeLists.txt index 483eef92..c6f8f275 100644 --- a/tracker-aruco/CMakeLists.txt +++ b/tracker-aruco/CMakeLists.txt @@ -2,6 +2,6 @@ find_package(OpenCV 3.0 QUIET)  set(SDK_ARUCO_LIBPATH "" CACHE FILEPATH "Aruco paper marker tracker static library path")  if(SDK_ARUCO_LIBPATH)      opentrack_boilerplate(opentrack-tracker-aruco) -    target_link_libraries(opentrack-tracker-aruco ${SDK_ARUCO_LIBPATH} ${OpenCV_LIBS}) +    target_link_libraries(opentrack-tracker-aruco opentrack-cv ${SDK_ARUCO_LIBPATH} ${OpenCV_LIBS})      target_include_directories(opentrack-tracker-aruco SYSTEM PUBLIC ${OpenCV_INCLUDE_DIRS})  endif() diff --git a/tracker-pt/CMakeLists.txt b/tracker-pt/CMakeLists.txt index f646dd8b..b2539a74 100644 --- a/tracker-pt/CMakeLists.txt +++ b/tracker-pt/CMakeLists.txt @@ -1,6 +1,6 @@  find_package(OpenCV 3.0 QUIET)  if(OpenCV_FOUND)      opentrack_boilerplate(opentrack-tracker-pt) -    target_link_libraries(opentrack-tracker-pt ${OpenCV_LIBS}) +    target_link_libraries(opentrack-tracker-pt opentrack-cv ${OpenCV_LIBS})      target_include_directories(opentrack-tracker-pt SYSTEM PUBLIC ${OpenCV_INCLUDE_DIRS})  endif() diff --git a/tracker-pt/ftnoir_tracker_pt.h b/tracker-pt/ftnoir_tracker_pt.h index b5e11157..393f8e76 100644 --- a/tracker-pt/ftnoir_tracker_pt.h +++ b/tracker-pt/ftnoir_tracker_pt.h @@ -16,7 +16,7 @@  #include "point_tracker.h"  #include "pt_video_widget.h"  #include "opentrack-compat/timer.hpp" -#include "opentrack/opencv-camera-dialog.hpp" +#include "cv/camera-dialog.hpp"  #include "opentrack-compat/pi-constant.hpp"  #include <QThread> diff --git a/tracker-pt/ftnoir_tracker_pt_dialog.h b/tracker-pt/ftnoir_tracker_pt_dialog.h index b992e98d..87501b28 100644 --- a/tracker-pt/ftnoir_tracker_pt_dialog.h +++ b/tracker-pt/ftnoir_tracker_pt_dialog.h @@ -14,7 +14,7 @@  #include "trans_calib.h"  #include "pt_video_widget.h"  #include "ui_FTNoIR_PT_Controls.h" -#include "opentrack/opencv-camera-dialog.hpp" +#include "cv/camera-dialog.hpp"  #include <QTimer> | 
