diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2019-03-18 15:20:09 +0100 | 
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2019-03-18 15:20:09 +0100 | 
| commit | 5023b54ba76325bb0b5598d59714bdad2d55d81e (patch) | |
| tree | 15cc639eff7dbfa12eeccaa52d7fd251f18969e6 /tracker-pt/module | |
| parent | 3aababf6fd53a7b0312c2c1492bab6b43584b613 (diff) | |
video: add support for camera modules
Issue: #910
Diffstat (limited to 'tracker-pt/module')
| -rw-r--r-- | tracker-pt/module/camera.cpp | 101 | ||||
| -rw-r--r-- | tracker-pt/module/camera.h | 17 | ||||
| -rw-r--r-- | tracker-pt/module/point_extractor.cpp | 2 | 
3 files changed, 42 insertions, 78 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 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  | 
