summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-03-20 06:24:08 +0100
committerStanislaw Halik <sthalik@misaki.pl>2017-03-20 06:24:33 +0100
commit7536ac46d919ab2a85d6ea3876e3a725af971e3f (patch)
treefd7dc713f2fe5ec7abbacddcbefbc91e3139ccd8 /tracker-pt
parent761fe827269d4cf34d33caadbef37d885cccc615 (diff)
tracker/pt: camera changes
- move dt handling here, from ITracker impl - don't depend on other PT headers. we'd like to reuse the code. - adjust return value convention. - get rid of dt_valid - return fps as a double Adjust usages in ITracker impl and dialog.
Diffstat (limited to 'tracker-pt')
-rw-r--r--tracker-pt/camera.cpp74
-rw-r--r--tracker-pt/camera.h47
-rw-r--r--tracker-pt/ftnoir_tracker_pt.cpp32
-rw-r--r--tracker-pt/ftnoir_tracker_pt.h3
-rw-r--r--tracker-pt/ftnoir_tracker_pt_dialog.cpp6
5 files changed, 92 insertions, 70 deletions
diff --git a/tracker-pt/camera.cpp b/tracker-pt/camera.cpp
index 68b0cf16..6a724fc7 100644
--- a/tracker-pt/camera.cpp
+++ b/tracker-pt/camera.cpp
@@ -8,7 +8,7 @@
#include "camera.h"
#include "compat/sleep.hpp"
-namespace impl {
+constexpr double Camera::dt_eps;
QString Camera::get_desired_name() const
{
@@ -20,7 +20,7 @@ QString Camera::get_active_name() const
return active_name;
}
-void CamInfo::get_focal_length(f& fx) const
+void CamInfo::get_focal_length(double& fx) const
{
using std::tan;
using std::atan;
@@ -37,47 +37,45 @@ void CamInfo::get_focal_length(f& fx) const
//static bool once = false; if (!once) { once = true; qDebug() << "f" << ret << "fov" << (fov * 180/M_PI); }
}
-DEFUN_WARN_UNUSED bool Camera::get_info(CamInfo& ret) const
+DEFUN_WARN_UNUSED Camera::result Camera::get_info() const
{
if (cam_info.res_x == 0 || cam_info.res_y == 0)
- return false;
- ret = cam_info;
- return true;
+ return result(false, CamInfo());
+ return result(true, cam_info);
}
-DEFUN_WARN_UNUSED bool Camera::get_frame(double dt, cv::Mat& frame, CamInfo& info)
+DEFUN_WARN_UNUSED Camera::result Camera::get_frame(cv::Mat& frame)
{
bool new_frame = _get_frame(frame);
- // measure fps of valid frames
- static constexpr double RC = .1; // seconds
- const double alpha = dt/(dt + RC);
- dt_valid += dt;
-
if (new_frame)
{
- if (dt_mean < 2e-3)
+ const double dt = t.elapsed_seconds();
+ t.start();
+
+ // measure fps of valid frames
+ static constexpr double RC = .1; // seconds
+ const double alpha = dt/(dt + RC);
+
+ if (dt_mean < dt_eps)
dt_mean = dt;
else
- dt_mean = (1-alpha) * dt_mean + alpha * dt_valid;
+ dt_mean = (1-alpha) * dt_mean + alpha * dt;
- cam_info.fps = dt_mean > 2e-3 ? int(1 / dt_mean + .65) : 0;
+ cam_info.fps = dt_mean > dt_eps ? 1 / dt_mean : 0;
cam_info.res_x = frame.cols;
cam_info.res_y = frame.rows;
- cam_info.fov = s.fov;
-
- info = cam_info;
-
- dt_valid = 0;
+ cam_info.fov = fov;
}
else
qDebug() << "pt camera: can't get frame";
- return new_frame;
+
+ return result(new_frame, cam_info);
}
-DEFUN_WARN_UNUSED bool Camera::start(int idx, int fps, int res_x, int res_y)
+DEFUN_WARN_UNUSED Camera::open_status Camera::start(int idx, int fps, int res_x, int res_y)
{
- if (idx >= 0 && fps >= 0 && res_x > 0 && res_y > 0)
+ if (idx >= 0 && fps >= 0 && res_x >= 0 && res_y >= 0)
{
if (!cap || !cap->isOpened() ||
cam_desired.idx != idx ||
@@ -89,32 +87,44 @@ DEFUN_WARN_UNUSED bool Camera::start(int idx, int fps, int res_x, int res_y)
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(cam_desired.idx));
- cap->set(cv::CAP_PROP_FRAME_WIDTH, cam_desired.res_x);
- cap->set(cv::CAP_PROP_FRAME_HEIGHT, cam_desired.res_y);
- cap->set(cv::CAP_PROP_FPS, cam_desired.fps);
+ if (cam_desired.res_x)
+ cap->set(cv::CAP_PROP_FRAME_WIDTH, cam_desired.res_x);
+ if (cam_desired.res_y)
+ cap->set(cv::CAP_PROP_FRAME_HEIGHT, cam_desired.res_y);
+ if (cam_desired.fps)
+ cap->set(cv::CAP_PROP_FPS, cam_desired.fps);
if (cap->isOpened())
{
qDebug() << "pt: opening camera";
cam_info = CamInfo();
- cam_info.idx = cam_desired.idx;
+ active_name = QString();
+ cam_info.idx = -1;
+ dt_mean = 0;
active_name = desired_name;
+ t.start();
- return true;
+ return open_ok_change;
}
else
{
stop();
- return false;
+ return open_error;
}
}
}
+ else
+ {
+ stop();
+ return open_error;
+ }
- return true;
+ return open_ok_no_change;
}
void Camera::stop()
@@ -134,7 +144,7 @@ DEFUN_WARN_UNUSED bool Camera::_get_frame(cv::Mat& frame)
{
if (cap->read(frame))
return true;
- portable::sleep(100);
+ portable::sleep(14);
}
}
return false;
@@ -149,5 +159,3 @@ void Camera::camera_deleter::operator()(cv::VideoCapture* cap)
delete cap;
}
}
-
-} // ns impl
diff --git a/tracker-pt/camera.h b/tracker-pt/camera.h
index 1d3b332c..449a2d3f 100644
--- a/tracker-pt/camera.h
+++ b/tracker-pt/camera.h
@@ -7,47 +7,47 @@
#pragma once
+#include "compat/ndebug-guard.hpp"
+
#undef NDEBUG
#include <cassert>
-#include "numeric.hpp"
-#include "ftnoir_tracker_pt_settings.h"
-
#include "compat/util.hpp"
+#include "compat/timer.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/videoio.hpp>
#include <memory>
+#include <tuple>
#include <QString>
-namespace impl {
-
-using namespace types;
-
struct CamInfo final
{
- CamInfo() : fov(0), res_x(0), res_y(0), fps(-1), idx(-1) {}
- void get_focal_length(f& fx) const;
+ CamInfo() : fov(0), fps(0), res_x(0), res_y(0), idx(-1) {}
+ void get_focal_length(double& fx) const;
double fov;
+ double fps;
int res_x;
int res_y;
- int fps;
int idx;
};
-class Camera final
+struct Camera final
{
-public:
- Camera() : dt_valid(0), dt_mean(0) {}
+ enum open_status : unsigned { open_error, open_ok_no_change, open_ok_change };
- DEFUN_WARN_UNUSED bool start(int idx, int fps, int res_x, int res_y);
+ using result = std::tuple<bool, CamInfo>;
+
+ Camera() : dt_mean(0), fov(0) {}
+
+ DEFUN_WARN_UNUSED open_status start(int idx, int fps, int res_x, int res_y);
void stop();
- DEFUN_WARN_UNUSED bool get_frame(double dt, cv::Mat& frame, CamInfo& info);
- DEFUN_WARN_UNUSED bool get_info(CamInfo &ret) const;
+ DEFUN_WARN_UNUSED result get_frame(cv::Mat& frame);
+ DEFUN_WARN_UNUSED result get_info() const;
CamInfo get_desired() const { return cam_desired; }
QString get_desired_name() const;
@@ -59,13 +59,15 @@ public:
const cv::VideoCapture* operator->() const { return cap.get(); }
operator bool() const { return cap && cap->isOpened(); }
+ void set_fov(double value) { fov = value; }
+
private:
DEFUN_WARN_UNUSED bool _get_frame(cv::Mat& frame);
- settings_pt s;
-
- double dt_valid;
double dt_mean;
+ double fov;
+
+ Timer t;
CamInfo cam_info;
CamInfo cam_desired;
@@ -79,9 +81,6 @@ private:
using camera_ptr = std::unique_ptr<cv::VideoCapture, camera_deleter>;
camera_ptr cap;
-};
-
-} // ns impl
-using impl::Camera;
-using impl::CamInfo;
+ static constexpr double dt_eps = 1./384;
+};
diff --git a/tracker-pt/ftnoir_tracker_pt.cpp b/tracker-pt/ftnoir_tracker_pt.cpp
index bf7e2c4b..f9a10723 100644
--- a/tracker-pt/ftnoir_tracker_pt.cpp
+++ b/tracker-pt/ftnoir_tracker_pt.cpp
@@ -24,6 +24,8 @@ Tracker_PT::Tracker_PT() :
ever_success(false)
{
connect(s.b.get(), SIGNAL(saving()), this, SLOT(apply_settings()), Qt::DirectConnection);
+ connect(&s.fov, SIGNAL(valueChanged(int)), this, SLOT(set_fov(int)), Qt::DirectConnection);
+ set_fov(s.fov);
}
Tracker_PT::~Tracker_PT()
@@ -61,14 +63,12 @@ void Tracker_PT::run()
while((commands & ABORT) == 0)
{
- const double dt = time.elapsed_seconds();
- time.start();
CamInfo cam_info;
bool new_frame;
{
QMutexLocker l(&camera_mtx);
- new_frame = camera.get_frame(dt, frame, cam_info);
+ std::tie(new_frame, cam_info) = camera.get_frame(frame);
}
if (new_frame)
@@ -133,17 +133,29 @@ void Tracker_PT::apply_settings()
QMutexLocker l(&camera_mtx);
- CamInfo info;
+ Camera::open_status status = camera.start(camera_name_to_index(s.camera_name), s.cam_fps, s.cam_res_x, s.cam_res_y);
- if (!camera.get_info(info) || frame.rows != info.res_y || frame.cols != info.res_x)
- frame = cv::Mat();
-
- if (!camera.start(camera_name_to_index(s.camera_name), s.cam_fps, s.cam_res_x, s.cam_res_y))
+ switch (status)
+ {
+ case Camera::open_error:
qDebug() << "can't start camera" << s.camera_name;
+ return;
+ case Camera::open_ok_change:
+ frame = cv::Mat();
+ break;
+ case Camera::open_ok_no_change:
+ break;
+ }
qDebug() << "pt: done applying settings";
}
+void Tracker_PT::set_fov(int value)
+{
+ QMutexLocker l(&camera_mtx);
+ camera.set_fov(value);
+}
+
void Tracker_PT::start_tracker(QFrame* video_frame)
{
//video_frame->setAttribute(Qt::WA_NativeWindow);
@@ -230,8 +242,10 @@ int Tracker_PT::get_n_points()
bool Tracker_PT::get_cam_info(CamInfo* info)
{
QMutexLocker lock(&camera_mtx);
+ bool ret;
- return camera.get_info(*info);
+ std::tie(ret, *info) = camera.get_info();
+ return ret;
}
#include "ftnoir_tracker_pt_dialog.h"
diff --git a/tracker-pt/ftnoir_tracker_pt.h b/tracker-pt/ftnoir_tracker_pt.h
index fba09c04..26d935f0 100644
--- a/tracker-pt/ftnoir_tracker_pt.h
+++ b/tracker-pt/ftnoir_tracker_pt.h
@@ -16,7 +16,6 @@
#include "camera.h"
#include "point_extractor.h"
#include "point_tracker.h"
-#include "compat/timer.hpp"
#include "cv/video-widget.hpp"
#include "compat/util.hpp"
@@ -53,6 +52,7 @@ public:
bool get_cam_info(CamInfo* info);
public slots:
void apply_settings();
+ void set_fov(int value);
protected:
void run() override;
private:
@@ -74,7 +74,6 @@ private:
qshared<QLayout> layout;
settings_pt s;
- Timer time;
cv::Mat frame, preview_frame;
std::vector<vec2> points;
diff --git a/tracker-pt/ftnoir_tracker_pt_dialog.cpp b/tracker-pt/ftnoir_tracker_pt_dialog.cpp
index 5c7c788b..6851e3fd 100644
--- a/tracker-pt/ftnoir_tracker_pt_dialog.cpp
+++ b/tracker-pt/ftnoir_tracker_pt_dialog.cpp
@@ -121,7 +121,7 @@ void TrackerDialog_PT::poll_tracker_info_impl()
CamInfo info;
if (tracker && tracker->get_cam_info(&info))
{
- ui.caminfo_label->setText(QStringLiteral("%1x%2 @ %3 FPS").arg(info.res_x).arg(info.res_y).arg(info.fps));
+ ui.caminfo_label->setText(QStringLiteral("%1x%2 @ %3 FPS").arg(info.res_x).arg(info.res_y).arg(iround(info.fps)));
// display point info
const int n_points = tracker->get_n_points();
@@ -151,7 +151,9 @@ void TrackerDialog_PT::show_camera_settings()
cv::VideoCapture& cap = *tracker->camera;
CamInfo info;
- if (tracker->camera.get_info(info))
+ bool status;
+ std::tie(status, info) = tracker->camera.get_info();
+ if (status)
video_property_page::show_from_capture(cap, info.idx);
}
}