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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/* 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.
*/
#if defined(OPENTRACK_API) && defined(_WIN32)
#include <windows.h>
#include <dshow.h>
#endif
#include "camera.h"
#include <string>
#include <QDebug>
using namespace cv;
#if defined(OPENTRACK_API) && (defined(__unix) || defined(__linux) || defined(__APPLE__))
#include <unistd.h>
#endif
#ifdef OPENTRACK_API
void get_camera_device_names(std::vector<std::string>& device_names) {
# if defined(_WIN32)
// Create the System Device Enumerator.
HRESULT hr;
ICreateDevEnum *pSysDevEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum);
if (FAILED(hr))
{
return;
}
// Obtain a class enumerator for the video compressor category.
IEnumMoniker *pEnumCat = NULL;
hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0);
if (hr == S_OK) {
// Enumerate the monikers.
IMoniker *pMoniker = NULL;
ULONG cFetched;
while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK) {
IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag);
if (SUCCEEDED(hr)) {
// To retrieve the filter's friendly name, do the following:
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
auto wstr = std::wstring(varName.bstrVal);
auto str = std::string(wstr.begin(), wstr.end());
device_names.push_back(str);
}
VariantClear(&varName);
////// To create an instance of the filter, do the following:
////IBaseFilter *pFilter;
////hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter,
//// (void**)&pFilter);
// Now add the filter to the graph.
//Remember to release pFilter later.
pPropBag->Release();
}
pMoniker->Release();
}
pEnumCat->Release();
}
pSysDevEnum->Release();
# else
for (int i = 0; i < 16; i++) {
char buf[128];
sprintf(buf, "/dev/video%d", i);
if (access(buf, R_OK | W_OK) == 0) {
device_names.push_back(std::string(buf));
}
}
# endif
}
#else
// ----------------------------------------------------------------------------
void get_camera_device_names(std::vector<std::string>& device_names)
{
videoInput VI;
VI.listDevices();
std::string device_name;
for(int index = 0; ; ++index) {
device_name = VI.getDeviceName(index);
if (device_name.empty()) break;
device_names.push_back(device_name);
}
}
#endif
// ----------------------------------------------------------------------------
void Camera::set_device_index(int index)
{
if (desired_index != index)
{
desired_index = index;
_set_device_index();
// reset fps
dt_valid = 0;
dt_mean = 0;
active_index = index;
}
}
void Camera::set_f(float f)
{
if (cam_desired.f != f)
{
cam_desired.f = f;
_set_f();
}
}
void Camera::set_fps(int fps)
{
if (cam_desired.fps != fps)
{
cam_desired.fps = fps;
_set_fps();
}
}
void Camera::set_res(int x_res, int y_res)
{
if (cam_desired.res_x != x_res || cam_desired.res_y != y_res)
{
cam_desired.res_x = x_res;
cam_desired.res_y = y_res;
_set_res();
_set_fps();
}
}
bool Camera::get_frame(float dt, cv::Mat* frame)
{
bool new_frame = _get_frame(frame);
// measure fps of valid frames
const float dt_smoothing_const = 0.9;
dt_valid += dt;
if (new_frame)
{
dt_mean = dt_smoothing_const * dt_mean + (1.0 - dt_smoothing_const) * dt_valid;
cam_info.fps = 1.0 / dt_mean;
dt_valid = 0;
}
return new_frame;
}
// ----------------------------------------------------------------------------
#ifdef OPENTRACK_API
void CVCamera::start()
{
cap = new VideoCapture(desired_index);
// extract camera info
if (cap->isOpened())
{
active = true;
active_index = desired_index;
cam_info.res_x = cap->get(CV_CAP_PROP_FRAME_WIDTH);
cam_info.res_y = cap->get(CV_CAP_PROP_FRAME_HEIGHT);
} else {
delete cap;
cap = nullptr;
}
}
void CVCamera::stop()
{
if (cap)
{
cap->release();
delete cap;
}
active = false;
}
bool CVCamera::_get_frame(Mat* frame)
{
if (cap && cap->isOpened())
{
Mat img;
for (int i = 0; i < 100 && !cap->read(img); i++)
;;
if (img.empty())
return false;
*frame = img;
return true;
}
return false;
}
void CVCamera::_set_index()
{
if (active) restart();
}
void CVCamera::_set_f()
{
cam_info.f = cam_desired.f;
}
void CVCamera::_set_fps()
{
if (cap) cap->set(CV_CAP_PROP_FPS, cam_desired.fps);
}
void CVCamera::_set_res()
{
if (cap)
{
cap->set(CV_CAP_PROP_FRAME_WIDTH, cam_desired.res_x);
cap->set(CV_CAP_PROP_FRAME_HEIGHT, cam_desired.res_y);
cam_info.res_x = cap->get(CV_CAP_PROP_FRAME_WIDTH);
cam_info.res_y = cap->get(CV_CAP_PROP_FRAME_HEIGHT);
}
}
void CVCamera::_set_device_index()
{
if (cap)
{
cap->release();
delete cap;
}
cap = new VideoCapture(desired_index);
}
#else
// ----------------------------------------------------------------------------
VICamera::VICamera() : frame_buffer(NULL)
{
VI.listDevices();
}
void VICamera::start()
{
if (desired_index >= 0)
{
if (cam_desired.res_x == 0 || cam_desired.res_y == 0)
VI.setupDevice(desired_index);
else
VI.setupDevice(desired_index, cam_desired.res_x, cam_desired.res_y);
active = true;
active_index = desired_index;
cam_info.res_x = VI.getWidth(active_index);
cam_info.res_y = VI.getHeight(active_index);
new_frame = cv::Mat(cam_info.res_y, cam_info.res_x, CV_8UC3);
// If matrix is not continuous we have to copy manually via frame_buffer
if (!new_frame.isContinuous()) {
unsigned int size = VI.getSize(active_index);
frame_buffer = new unsigned char[size];
}
}
}
void VICamera::stop()
{
if (active)
{
VI.stopDevice(active_index);
}
if (frame_buffer)
{
delete[] frame_buffer;
frame_buffer = NULL;
}
active = false;
}
bool VICamera::_get_frame(Mat* frame)
{
if (active && VI.isFrameNew(active_index))
{
if (new_frame.isContinuous())
{
VI.getPixels(active_index, new_frame.data, false, true);
}
else
{
// If matrix is not continuous we have to copy manually via frame_buffer
VI.getPixels(active_index, frame_buffer, false, true);
new_frame = cv::Mat(cam_info.res_y, cam_info.res_x, CV_8UC3, frame_buffer).clone();
}
*frame = new_frame;
return true;
}
return false;
}
void VICamera::_set_device_index()
{
if (active) restart();
}
void VICamera::_set_f()
{
cam_info.f = cam_desired.f;
}
void VICamera::_set_fps()
{
bool was_active = active;
if (active) stop();
VI.setIdealFramerate(desired_index, cam_desired.fps);
if (was_active) start();
}
void VICamera::_set_res()
{
if (active) restart();
}
#endif
// ----------------------------------------------------------------------------
Mat FrameRotation::rotate_frame(Mat frame)
{
switch (rotation)
{
case CLOCKWISE:
{
Mat dst;
transpose(frame, dst);
flip(dst, dst, 1);
return dst;
}
case COUNTER_CLOCKWISE:
{
Mat dst;
transpose(frame, dst);
flip(dst, dst, 0);
return dst;
}
default:
return frame;
}
}
|