summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2019-03-18 15:20:09 +0100
committerStanislaw Halik <sthalik@misaki.pl>2019-03-18 15:20:09 +0100
commit5023b54ba76325bb0b5598d59714bdad2d55d81e (patch)
tree15cc639eff7dbfa12eeccaa52d7fd251f18969e6 /tracker-pt
parent3aababf6fd53a7b0312c2c1492bab6b43584b613 (diff)
video: add support for camera modules
Issue: #910
Diffstat (limited to 'tracker-pt')
-rw-r--r--tracker-pt/CMakeLists.txt2
-rw-r--r--tracker-pt/ftnoir_tracker_pt.cpp3
-rw-r--r--tracker-pt/ftnoir_tracker_pt_dialog.cpp13
-rw-r--r--tracker-pt/module/camera.cpp101
-rw-r--r--tracker-pt/module/camera.h17
-rw-r--r--tracker-pt/module/point_extractor.cpp2
-rw-r--r--tracker-pt/pt-api.hpp5
7 files changed, 52 insertions, 91 deletions
diff --git a/tracker-pt/CMakeLists.txt b/tracker-pt/CMakeLists.txt
index f12f530b..304a6b3d 100644
--- a/tracker-pt/CMakeLists.txt
+++ b/tracker-pt/CMakeLists.txt
@@ -2,7 +2,7 @@ find_package(OpenCV QUIET)
if(OpenCV_FOUND)
otr_module(tracker-pt-base STATIC)
target_include_directories(${self} SYSTEM PUBLIC ${OpenCV_INCLUDE_DIRS})
- target_link_libraries(${self} opencv_imgproc opentrack-cv opencv_core)
+ target_link_libraries(${self} opencv_imgproc opentrack-cv opencv_core opentrack-video)
set_property(TARGET ${self} PROPERTY OUTPUT_NAME "pt-base")
endif()
add_subdirectory(module)
diff --git a/tracker-pt/ftnoir_tracker_pt.cpp b/tracker-pt/ftnoir_tracker_pt.cpp
index 3854e531..4b796af7 100644
--- a/tracker-pt/ftnoir_tracker_pt.cpp
+++ b/tracker-pt/ftnoir_tracker_pt.cpp
@@ -8,7 +8,6 @@
#include "ftnoir_tracker_pt.h"
#include "video/video-widget.hpp"
-#include "compat/camera-names.hpp"
#include "compat/math-imports.hpp"
#include "pt-api.hpp"
@@ -121,7 +120,7 @@ bool Tracker_PT::maybe_reopen_camera()
{
QMutexLocker l(&camera_mtx);
- return camera->start(camera_name_to_index(s.camera_name),
+ return camera->start(s.camera_name,
s.cam_fps, s.cam_res_x, s.cam_res_y);
}
diff --git a/tracker-pt/ftnoir_tracker_pt_dialog.cpp b/tracker-pt/ftnoir_tracker_pt_dialog.cpp
index 2b06c823..edf689a9 100644
--- a/tracker-pt/ftnoir_tracker_pt_dialog.cpp
+++ b/tracker-pt/ftnoir_tracker_pt_dialog.cpp
@@ -7,10 +7,9 @@
*/
#include "ftnoir_tracker_pt_dialog.h"
-
#include "compat/math.hpp"
-#include "compat/camera-names.hpp"
-#include "cv/video-property-page.hpp"
+#include "video/camera.hpp"
+
#include <opencv2/core.hpp>
#include <QString>
@@ -33,7 +32,8 @@ TrackerDialog_PT::TrackerDialog_PT(const QString& module_name) :
ui.setupUi(this);
- ui.camdevice_combo->addItems(get_camera_names());
+ for (const QString& str : video::camera_names())
+ ui.camdevice_combo->addItem(str);
tie_setting(s.camera_name, ui.camdevice_combo);
tie_setting(s.cam_res_x, ui.res_x_spin);
@@ -231,10 +231,7 @@ void TrackerDialog_PT::show_camera_settings()
tracker->camera->show_camera_settings();
}
else
- {
- const int idx = camera_name_to_index(s.camera_name);
- video_property_page::show(idx);
- }
+ (void)video::show_dialog(s.camera_name);
}
void TrackerDialog_PT::trans_calib_step()
diff --git a/tracker-pt/module/camera.cpp b/tracker-pt/module/camera.cpp
index 1afecc92..687f5bff 100644
--- a/tracker-pt/module/camera.cpp
+++ b/tracker-pt/module/camera.cpp
@@ -8,15 +8,9 @@
#include "camera.h"
#include "frame.hpp"
-#include "compat/sleep.hpp"
-#include "compat/camera-names.hpp"
#include "compat/math-imports.hpp"
-#include <opencv2/imgproc.hpp>
-
-#include "cv/video-property-page.hpp"
-
-#include <cstdlib>
+#include <opencv2/core.hpp>
namespace pt_module {
@@ -26,22 +20,18 @@ Camera::Camera(const QString& module_name) : s { module_name }
QString Camera::get_desired_name() const
{
- return desired_name;
+ return cam_desired.name;
}
QString Camera::get_active_name() const
{
- return active_name;
+ return cam_info.name;
}
void Camera::show_camera_settings()
{
- const int idx = camera_name_to_index(s.camera_name);
-
- if (cap && cap->isOpened())
- video_property_page::show_from_capture(*cap, idx);
- else
- video_property_page::show(idx);
+ if (cap)
+ (void)cap->show_dialog();
}
Camera::result Camera::get_info() const
@@ -83,59 +73,53 @@ Camera::result Camera::get_frame(pt_frame& frame_)
return { false, {} };
}
-bool Camera::start(int idx, int fps, int res_x, int res_y)
+bool Camera::start(const QString& name, int fps, int res_x, int res_y)
{
- if (idx >= 0 && fps >= 0 && res_x >= 0 && res_y >= 0)
+ if (fps >= 0 && res_x >= 0 && res_y >= 0)
{
- if (cam_desired.idx != idx ||
+ if (cam_desired.name != name ||
(int)cam_desired.fps != fps ||
cam_desired.res_x != res_x ||
cam_desired.res_y != res_y ||
- !cap || !cap->isOpened() || !cap->grab())
+ !cap || !cap->is_open())
{
stop();
- desired_name = get_camera_names().value(idx);
- cam_desired.idx = idx;
+ cam_desired.name = name;
cam_desired.fps = fps;
cam_desired.res_x = res_x;
cam_desired.res_y = res_y;
cam_desired.fov = fov;
- cap = camera_ptr(new cv::VideoCapture(idx));
+ cap = video::make_camera(name);
- if (cam_desired.res_x > 0 && cam_desired.res_y > 0)
- {
- cap->set(cv::CAP_PROP_FRAME_WIDTH, res_x);
- cap->set(cv::CAP_PROP_FRAME_HEIGHT, res_y);
- }
+ if (!cap)
+ goto fail;
- if (fps > 0)
- cap->set(cv::CAP_PROP_FPS, fps);
+ camera::info info {};
+ info.fps = fps;
+ info.width = res_x;
+ info.height = res_y;
- if (cap->isOpened())
- {
- cam_info = pt_camera_info();
- cam_info.idx = idx;
- dt_mean = 0;
- active_name = desired_name;
+ if (!cap->start(info))
+ goto fail;
- cv::Mat tmp;
+ cam_info = pt_camera_info();
+ cam_info.name = name;
+ dt_mean = 0;
- if (get_frame_(tmp))
- {
- t.start();
- return true;
- }
- }
+ cv::Mat tmp;
- cap = nullptr;
- return false;
- }
+ if (!get_frame_(tmp))
+ goto fail;
- return true;
+ t.start();
+ }
}
+ return true;
+
+fail:
stop();
return false;
}
@@ -143,34 +127,23 @@ bool Camera::start(int idx, int fps, int res_x, int res_y)
void Camera::stop()
{
cap = nullptr;
- desired_name = QString{};
- active_name = QString{};
cam_info = {};
cam_desired = {};
}
-bool Camera::get_frame_(cv::Mat& frame)
+bool Camera::get_frame_(cv::Mat& img)
{
- if (cap && cap->isOpened())
+ if (cap && cap->is_open())
{
- for (unsigned i = 0; i < 10; i++)
+ auto [ frame, ret ] = cap->get_frame();
+ if (ret)
{
- if (cap->read(frame))
- return true;
- portable::sleep(50);
+ img = cv::Mat(frame.height, frame.width, CV_8UC(frame.channels), (void*)frame.data, frame.stride);
+ return true;
}
}
- return false;
-}
-void Camera::camera_deleter::operator()(cv::VideoCapture* cap)
-{
- if (cap)
- {
- if (cap->isOpened())
- cap->release();
- delete cap;
- }
+ return false;
}
} // ns pt_module
diff --git a/tracker-pt/module/camera.h b/tracker-pt/module/camera.h
index 2ea633d0..02e2fe4d 100644
--- a/tracker-pt/module/camera.h
+++ b/tracker-pt/module/camera.h
@@ -9,11 +9,11 @@
#include "pt-api.hpp"
#include "compat/timer.hpp"
+#include "video/camera.hpp"
#include <memory>
#include <opencv2/core.hpp>
-#include <opencv2/videoio.hpp>
#include <QString>
@@ -23,7 +23,7 @@ struct Camera final : pt_camera
{
Camera(const QString& module_name);
- bool start(int idx, int fps, int res_x, int res_y) override;
+ bool start(const QString& name, int fps, int res_x, int res_y) override;
void stop() override;
result get_frame(pt_frame& Frame) override;
@@ -37,23 +37,16 @@ struct Camera final : pt_camera
void show_camera_settings() override;
private:
+ using camera = typename video::impl::camera;
+
[[nodiscard]] bool get_frame_(cv::Mat& frame);
f dt_mean = 0, fov = 30;
Timer t;
pt_camera_info cam_info;
pt_camera_info cam_desired;
- QString desired_name, active_name;
-
- struct camera_deleter final
- {
- void operator()(cv::VideoCapture* cap);
- };
-
- using camera_ptr = std::unique_ptr<cv::VideoCapture, camera_deleter>;
-
- camera_ptr cap;
+ std::unique_ptr<camera> cap;
pt_settings s;
static constexpr f dt_eps = f{1}/256;
diff --git a/tracker-pt/module/point_extractor.cpp b/tracker-pt/module/point_extractor.cpp
index 298d8752..2cb5db97 100644
--- a/tracker-pt/module/point_extractor.cpp
+++ b/tracker-pt/module/point_extractor.cpp
@@ -13,8 +13,6 @@
#include "cv/numeric.hpp"
#include "compat/math.hpp"
-#include <opencv2/videoio.hpp>
-
#undef PREVIEW
//#define PREVIEW
diff --git a/tracker-pt/pt-api.hpp b/tracker-pt/pt-api.hpp
index b44cfea2..741576a1 100644
--- a/tracker-pt/pt-api.hpp
+++ b/tracker-pt/pt-api.hpp
@@ -12,6 +12,7 @@
#include <opencv2/core.hpp>
#include <QImage>
+#include <QString>
#ifdef __clang__
# pragma clang diagnostic push
@@ -30,7 +31,7 @@ struct pt_camera_info final
int res_x = 0;
int res_y = 0;
- int idx = -1;
+ QString name;
};
struct pt_pixel_pos_mixin
@@ -74,7 +75,7 @@ struct pt_camera
pt_camera();
virtual ~pt_camera();
- [[nodiscard]] virtual bool start(int idx, int fps, int res_x, int res_y) = 0;
+ [[nodiscard]] virtual bool start(const QString& name, int fps, int res_x, int res_y) = 0;
virtual void stop() = 0;
virtual result get_frame(pt_frame& frame) = 0;