summaryrefslogtreecommitdiffhomepage
path: root/video-opencv
diff options
context:
space:
mode:
Diffstat (limited to 'video-opencv')
-rw-r--r--video-opencv/CMakeLists.txt13
-rw-r--r--video-opencv/impl-camera.cpp104
-rw-r--r--video-opencv/impl-metadata.cpp46
-rw-r--r--video-opencv/impl.hpp55
-rw-r--r--video-opencv/lang/de_DE.ts4
-rw-r--r--video-opencv/lang/nl_NL.ts4
-rw-r--r--video-opencv/lang/ru_RU.ts4
-rw-r--r--video-opencv/lang/stub.ts4
-rw-r--r--video-opencv/lang/zh_CN.ts4
-rw-r--r--video-opencv/video-property-page.cpp169
-rw-r--r--video-opencv/video-property-page.hpp13
11 files changed, 420 insertions, 0 deletions
diff --git a/video-opencv/CMakeLists.txt b/video-opencv/CMakeLists.txt
new file mode 100644
index 00000000..0b2460a4
--- /dev/null
+++ b/video-opencv/CMakeLists.txt
@@ -0,0 +1,13 @@
+include(opentrack-opencv)
+find_package(OpenCV QUIET)
+
+if(OpenCV_FOUND)
+ foreach(k core videoio imgcodecs imgproc)
+ otr_install_lib("opencv_${k}" "${opentrack-libexec}")
+ endforeach()
+ otr_module(video-opencv)
+ target_link_libraries(${self} opencv_core opencv_imgcodecs opencv_videoio opentrack-video)
+ if(WIN32)
+ target_link_libraries(${self} strmiids)
+ endif()
+endif()
diff --git a/video-opencv/impl-camera.cpp b/video-opencv/impl-camera.cpp
new file mode 100644
index 00000000..96081399
--- /dev/null
+++ b/video-opencv/impl-camera.cpp
@@ -0,0 +1,104 @@
+#include "impl.hpp"
+#include "compat/sleep.hpp"
+#include "video-property-page.hpp"
+#include <opencv2/core/utils/logger.hpp>
+
+namespace opencv_camera_impl {
+
+cam::cam(int idx) : idx(idx)
+{
+}
+
+cam::~cam()
+{
+ stop();
+}
+
+void cam::stop()
+{
+ if (cap)
+ {
+ if (cap->isOpened())
+ cap->release();
+ cap = std::nullopt;
+ }
+ mat = cv::Mat();
+ frame_ = { {}, false };
+}
+
+bool cam::is_open()
+{
+ return !!cap;
+}
+
+bool cam::start(info& args)
+{
+ stop();
+ cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_WARNING);
+ cap.emplace(idx, video_capture_backend);
+
+ if (args.width > 0 && args.height > 0)
+ {
+ cap->set(cv::CAP_PROP_FRAME_WIDTH, args.width);
+ cap->set(cv::CAP_PROP_FRAME_HEIGHT, args.height);
+ }
+ if (args.fps > 0)
+ cap->set(cv::CAP_PROP_FPS, args.fps);
+
+ if (args.use_mjpeg)
+ cap->set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
+
+ if (!cap->isOpened())
+ goto fail;
+
+ if (!get_frame_())
+ goto fail;
+
+ return true;
+
+fail:
+ stop();
+ return false;
+}
+
+bool cam::get_frame_()
+{
+ if (!is_open())
+ return false;
+
+ for (unsigned i = 0; i < 10; i++)
+ {
+ if (cap->read(mat))
+ {
+ frame_.data = mat.data;
+ frame_.width = mat.cols;
+ frame_.height = mat.rows;
+ frame_.stride = mat.step.p[0];
+ if (mat.step.p[0] == (unsigned)frame_.width * mat.elemSize())
+ frame_.stride = cv::Mat::AUTO_STEP;
+
+ frame_.channels = mat.channels();
+
+ return true;
+ }
+ portable::sleep(50);
+ }
+
+ return false;
+}
+
+std::tuple<const frame&, bool> cam::get_frame()
+{
+ bool ret = get_frame_();
+ return { frame_, ret };
+}
+
+bool cam::show_dialog()
+{
+ if (is_open())
+ return video_property_page::show_from_capture(*cap, idx);
+ else
+ return video_property_page::show(idx);
+}
+
+} // ns opencv_camera_impl
diff --git a/video-opencv/impl-metadata.cpp b/video-opencv/impl-metadata.cpp
new file mode 100644
index 00000000..7642c017
--- /dev/null
+++ b/video-opencv/impl-metadata.cpp
@@ -0,0 +1,46 @@
+#include "impl.hpp"
+#include "compat/camera-names.hpp"
+#include "video-property-page.hpp"
+
+namespace opencv_camera_impl {
+
+metadata::metadata() = default;
+
+std::unique_ptr<camera> metadata::make_camera(const QString& name)
+{
+ int idx = camera_name_to_index(name);
+ if (idx != -1)
+ return std::make_unique<cam>(idx);
+ else
+ return nullptr;
+}
+
+std::vector<QString> metadata::camera_names() const
+{
+ std::vector<std::tuple<QString, int>> names = get_camera_names();
+ std::vector<QString> ret;
+ for (const auto& [str, idx] : names)
+ ret.push_back(str);
+ return ret;
+}
+
+bool metadata::can_show_dialog(const QString& camera_name)
+{
+ return camera_name_to_index(camera_name) != -1;
+}
+
+bool metadata::show_dialog(const QString& camera_name)
+{
+ int idx = camera_name_to_index(camera_name);
+ if (idx != -1)
+ {
+ video_property_page::show(idx);
+ return true;
+ }
+ else
+ return false;
+}
+
+OTR_REGISTER_CAMERA(metadata)
+
+} // ns opencv_camera_impl
diff --git a/video-opencv/impl.hpp b/video-opencv/impl.hpp
new file mode 100644
index 00000000..ed5499b0
--- /dev/null
+++ b/video-opencv/impl.hpp
@@ -0,0 +1,55 @@
+/* Copyright (c) 2019 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 "video/camera.hpp"
+#include <optional>
+#include <opencv2/videoio.hpp>
+
+namespace opencv_camera_impl {
+
+using namespace video::impl;
+
+struct metadata : camera_
+{
+ metadata();
+ std::vector<QString> camera_names() const override;
+ std::unique_ptr<camera> make_camera(const QString& name) override;
+ bool can_show_dialog(const QString& camera_name) override;
+ bool show_dialog(const QString& camera_name) override;
+};
+
+struct cam final : camera
+{
+static constexpr int video_capture_backend =
+#ifdef _WIN32
+ cv::CAP_DSHOW;
+#elif !defined __APPLE__
+ cv::CAP_V4L2;
+#else
+ cv::CAP_ANY;
+#endif
+
+ cam(int idx);
+ ~cam() override;
+
+ bool start(info& args) override;
+ void stop() override;
+ bool is_open() override;
+ std::tuple<const frame&, bool> get_frame() override;
+ bool show_dialog() override;
+
+ bool get_frame_();
+
+ std::optional<cv::VideoCapture> cap;
+ cv::Mat mat;
+ frame frame_;
+ int idx = -1;
+};
+
+} // ns opencv_camera_impl
diff --git a/video-opencv/lang/de_DE.ts b/video-opencv/lang/de_DE.ts
new file mode 100644
index 00000000..1552582e
--- /dev/null
+++ b/video-opencv/lang/de_DE.ts
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="de_DE">
+</TS>
diff --git a/video-opencv/lang/nl_NL.ts b/video-opencv/lang/nl_NL.ts
new file mode 100644
index 00000000..9e739505
--- /dev/null
+++ b/video-opencv/lang/nl_NL.ts
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="nl_NL">
+</TS>
diff --git a/video-opencv/lang/ru_RU.ts b/video-opencv/lang/ru_RU.ts
new file mode 100644
index 00000000..f62cf2e1
--- /dev/null
+++ b/video-opencv/lang/ru_RU.ts
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="ru_RU">
+</TS>
diff --git a/video-opencv/lang/stub.ts b/video-opencv/lang/stub.ts
new file mode 100644
index 00000000..6401616d
--- /dev/null
+++ b/video-opencv/lang/stub.ts
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1">
+</TS>
diff --git a/video-opencv/lang/zh_CN.ts b/video-opencv/lang/zh_CN.ts
new file mode 100644
index 00000000..e5ca8aa9
--- /dev/null
+++ b/video-opencv/lang/zh_CN.ts
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="zh_CN">
+</TS>
diff --git a/video-opencv/video-property-page.cpp b/video-opencv/video-property-page.cpp
new file mode 100644
index 00000000..d56d4b91
--- /dev/null
+++ b/video-opencv/video-property-page.cpp
@@ -0,0 +1,169 @@
+/* Copyright (c) 2016 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 "video-property-page.hpp"
+
+#ifdef _WIN32
+
+#include "compat/camera-names.hpp"
+#include "compat/sleep.hpp"
+#include "compat/run-in-thread.hpp"
+#include "compat/library-path.hpp"
+#include "compat/thread-name.hpp"
+#include "impl.hpp"
+
+#include <cstring>
+
+#include <opencv2/videoio.hpp>
+
+#include <QApplication>
+#include <QProcess>
+#include <QThread>
+#include <QMessageBox>
+
+#include <QDebug>
+
+bool video_property_page::show_from_capture(cv::VideoCapture& cap, int /*index */)
+{
+ return cap.set(cv::CAP_PROP_SETTINGS, 0);
+}
+
+struct prop_settings_worker final : QThread
+{
+ explicit prop_settings_worker(int idx);
+ ~prop_settings_worker() override;
+
+private:
+ void open_prop_page();
+ void run() override;
+
+ cv::VideoCapture cap;
+ int idx = -1;
+};
+
+prop_settings_worker::prop_settings_worker(int idx_)
+{
+ int ret = (int)cap.get(cv::CAP_PROP_SETTINGS);
+
+ if (ret != 0)
+ {
+ run_in_thread_async(qApp, [] {
+ QMessageBox::warning(nullptr,
+ "Camera properties",
+ "Camera dialog already opened",
+ QMessageBox::Cancel,
+ QMessageBox::NoButton);
+ });
+ }
+ else
+ {
+ idx = idx_;
+ // DON'T MOVE IT
+ // ps3 eye will reset to default settings if done from another thread
+ open_prop_page();
+ }
+}
+
+void prop_settings_worker::open_prop_page()
+{
+ cap.open(idx + opencv_camera_impl::cam::video_capture_backend);
+
+ if (cap.isOpened())
+ {
+ cv::Mat tmp;
+
+ for (unsigned k = 0; k < 2000/50; k++)
+ {
+ if (cap.read(tmp))
+ {
+ qDebug() << "got frame" << tmp.rows << tmp.cols;
+ goto ok;
+ }
+ portable::sleep(50);
+ }
+ }
+
+ qDebug() << "property-page: can't open camera";
+ idx = -1;
+
+ return;
+
+ok:
+ portable::sleep(100);
+
+ qDebug() << "property-page: opening for" << idx;
+
+ if (!cap.set(cv::CAP_PROP_SETTINGS, 0))
+ {
+ run_in_thread_async(qApp, [] {
+ QMessageBox::warning(nullptr,
+ "Camera properties",
+ "Can't open camera dialog",
+ QMessageBox::Cancel,
+ QMessageBox::NoButton);
+ });
+ }
+}
+
+prop_settings_worker::~prop_settings_worker()
+{
+ if (idx != -1)
+ {
+ // ax filter is race condition-prone
+ portable::sleep(250);
+ cap.release();
+ // idem
+ portable::sleep(250);
+
+ qDebug() << "property-page: closed" << idx;
+ }
+}
+
+void prop_settings_worker::run()
+{
+ portable::set_curthread_name("dshow video property page");
+
+ if (idx != -1)
+ {
+ while (cap.get(cv::CAP_PROP_SETTINGS) > 0)
+ portable::sleep(1000);
+ }
+}
+
+bool video_property_page::show(int idx)
+{
+ auto thread = new prop_settings_worker(idx);
+
+ // XXX is this a race condition?
+ thread->moveToThread(qApp->thread());
+ QObject::connect(thread, &QThread::finished, qApp, [thread] { thread->deleteLater(); }, Qt::DirectConnection);
+
+ thread->start();
+
+ return true;
+}
+
+#elif defined(__linux__)
+# include <QProcess>
+# include "compat/camera-names.hpp"
+
+bool video_property_page::show(int idx)
+{
+ if ((unsigned)idx < get_camera_names().size())
+ return QProcess::startDetached("qv4l2", QStringList { "-d", QString("/dev/video%1").arg(idx) });
+ else
+ return false;
+}
+
+bool video_property_page::show_from_capture(cv::VideoCapture&, int idx)
+{
+ return show(idx);
+}
+#else
+bool video_property_page::show(int) { return false; }
+bool video_property_page::show_from_capture(cv::VideoCapture&, int) { return false; }
+#endif
diff --git a/video-opencv/video-property-page.hpp b/video-opencv/video-property-page.hpp
new file mode 100644
index 00000000..c2b9525d
--- /dev/null
+++ b/video-opencv/video-property-page.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <QString>
+#include <opencv2/videoio.hpp>
+
+struct video_property_page final
+{
+ video_property_page() = delete;
+ static bool show(int id);
+ static bool show_from_capture(cv::VideoCapture& cap, int index);
+private:
+};
+