summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt/module/camera.cpp
blob: 1b948932af3c120d6eb8d4494141d38d229d18de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright (c) 2012 Patrick Ruoff
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#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>

using namespace pt_module;
namespace pt_module {

Camera::Camera(const QString& module_name) : s { module_name }
{
}

QString Camera::get_desired_name() const
{
    return desired_name;
}

QString Camera::get_active_name() const
{
    return active_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);
}

Camera::result Camera::get_info() const
{
    if (cam_info.res_x == 0 || cam_info.res_y == 0)
        return { false, pt_camera_info() };
    else
        return { true, cam_info };
}

Camera::result Camera::get_frame(pt_frame& frame_)
{
    cv::Mat& frame = frame_.as<Frame>()->mat;

    const bool new_frame = get_frame_(frame);

    if (new_frame)
    {
        const double dt = t.elapsed_seconds();
        t.start();

        // measure fps of valid frames
        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;

        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 = fov;

        return { true, cam_info };
    }
    else
        return { false, {} };
}

bool Camera::start(int idx, int fps, int res_x, int res_y)
{
    if (idx >= 0 && fps >= 0 && res_x >= 0 && res_y >= 0)
    {
        if (cam_desired.idx != idx ||
            cam_desired.fps != fps ||
            cam_desired.res_x != res_x ||
            cam_desired.res_y != res_y ||
            !cap || !cap->isOpened() || !cap->grab())
        {
            stop();

            desired_name = get_camera_names().value(idx);
            cam_desired.idx = idx;
            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));

            if (cam_desired.res_x)
                cap->set(cv::CAP_PROP_FRAME_WIDTH,  res_x);
            if (cam_desired.res_y)
                cap->set(cv::CAP_PROP_FRAME_HEIGHT, res_y);
            if (fps > 0)
                cap->set(cv::CAP_PROP_FPS, fps);

            if (cap->isOpened())
            {
                cam_info = pt_camera_info();
                cam_info.idx = idx;
                dt_mean = 0;
                active_name = desired_name;

                cv::Mat tmp;

                if (get_frame_(tmp))
                {
                    t.start();
                    return true;
                }
            }

            cap = nullptr;
            return false;
        }

        return true;
    }

    stop();
    return false;
}

void Camera::stop()
{
    cap = nullptr;
    desired_name = QString{};
    active_name = QString{};
    cam_info = {};
    cam_desired = {};
}

bool Camera::get_frame_(cv::Mat& frame)
{
    if (cap && cap->isOpened())
    {
        for (unsigned i = 0; i < 10; i++)
        {
            if (cap->read(frame))
                return true;
            portable::sleep(50);
        }
    }
    return false;
}

void Camera::camera_deleter::operator()(cv::VideoCapture* cap)
{
    if (cap)
    {
        if (cap->isOpened())
            cap->release();
        delete cap;
    }
}

} // ns pt_module