diff options
Diffstat (limited to 'tracker-pt/module/camera.cpp')
-rw-r--r-- | tracker-pt/module/camera.cpp | 101 |
1 files changed, 37 insertions, 64 deletions
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 |