summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt/camera.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tracker-pt/camera.cpp')
-rw-r--r--tracker-pt/camera.cpp74
1 files changed, 41 insertions, 33 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