-
Notifications
You must be signed in to change notification settings - Fork 3
/
camerathread.cpp
370 lines (295 loc) · 11.8 KB
/
camerathread.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Copyright (c) 2015-2016 University of Helsinki
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <QDebug>
#include <QDateTime>
#include <QTextStream>
#include <QLinkedList>
#include "boost/date_time/posix_time/posix_time.hpp"
#include "camerathread.h"
using namespace boost::posix_time;
using namespace cv;
// ---------------------------------------------------------------------
CameraThread::CameraThread(int i) : idx(i), is_active(false),
was_active(false)
{
setDefaultDesiredInputSize();
window_size = Size(240,135);
}
// ---------------------------------------------------------------------
CameraThread::CameraThread(int i, QString wxh) : idx(i),
is_active(false),
was_active(false)
{
if (wxh.contains('x')) {
QStringList wh = wxh.split('x');
bool ok = true;
desired_input_size.width = wh.at(0).toInt(&ok);
desired_input_size.height = wh.at(1).toInt(&ok);
if (!ok)
setDefaultDesiredInputSize();
} else
setDefaultDesiredInputSize();
window_size = Size(240,135);
}
// ---------------------------------------------------------------------
void CameraThread::setDefaultDesiredInputSize() {
desired_input_size.width = 1280;
desired_input_size.height = 720;
}
// ---------------------------------------------------------------------
// Q_DECL_OVERRIDE produces an error on OS X 10.11 / Qt 5.6:
void CameraThread::run() Q_DECL_OVERRIDE {
QString result;
time_duration td, td1, td2;
ptime nextFrameTimestamp, currentFrameTimestamp;
ptime initialLoopTimestamp, processingDoneTimestamp, finalLoopTimestamp;
// int delayFound = 0;
// initialize capture on default source
VideoCapture capture(idx);
if (!capture.isOpened()) {
emit errorMessage(QString("Warning: Failed to initialize camera %1.")
.arg(idx));
return;
}
#if defined(Q_OS_LINUX)
qDebug() << "Camera" << idx
<< ": Trying to set input size to" << desired_input_size.width
<< "x" << desired_input_size.height;
QString v4l2 = QString("/usr/bin/v4l2-ctl -d /dev/video%1 -v width=%2,height=%3")
.arg(idx).arg(desired_input_size.width).arg(desired_input_size.height);
int ret = system(v4l2.toStdString().c_str());
qDebug() << "Command [" << v4l2 << "] returned" << ret;
capture.set(CV_CAP_PROP_FRAME_WIDTH, desired_input_size.width);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, desired_input_size.height);
#endif
// Get the properties from the camera
input_size.width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
input_size.height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
// print camera frame size
qDebug() << "Camera" << idx
<< ": Input size: width:" << input_size.width
<< "height:" << input_size.height;
emit cameraInfo(idx, input_size.width, input_size.height);
outdir = "";
filename = QString("capture%1.avi").arg(idx);
record_video = false;
#if defined(Q_OS_WIN)
fourcc = -1;
#else
fourcc = CV_FOURCC('m','p','4','v');
#endif
// Note: These need to match the default values in AvRecorder::AvRecorder():
framerate = 25;
output_size = Size(640,360);
// initialize initial timestamps
nextFrameTimestamp = microsec_clock::local_time();
currentFrameTimestamp = nextFrameTimestamp;
td = (currentFrameTimestamp - nextFrameTimestamp);
QLinkedList<time_duration> tdlist;
stopLoop = false;
is_active = true;
double avgload = 0.0;
size_t nframe = 0;
for (;;) {
if (stopLoop) {
qDebug() << "Camera" << idx << "stopping";
break;
}
// Happens when Stop was pressed:
if (!record_video && video.isOpened())
video.release();
// determine time at start of loop
initialLoopTimestamp = microsec_clock::local_time();
Mat frame;
capture >> frame;
nframe++;
if (is_active) {
was_active = true;
if (frame.cols && frame.rows) {
if (output_size.width != 0)
resizeAR(frame, output_size);
QDateTime datetime = QDateTime::currentDateTime();
rectangle(frame, Point(2,frame.rows-22), Point(300, frame.rows-8),
Scalar(0,0,0), CV_FILLED);
putText(frame, datetime.toString().toStdString().c_str(),
Point(10,frame.rows-10), FONT_HERSHEY_PLAIN, 1.0,
Scalar(255,255,255));
// Save frame to video
if (record_video && video.isOpened())
video << frame;
Mat window;
resize(frame, window, window_size);
putText(window, QString::number(nframe).toStdString().c_str(),
Point(10, 20), FONT_HERSHEY_PLAIN, 1.5,
Scalar(0,0,255), 2);
putText(window, QString::number(avgload, 'f', 2).toStdString().c_str(),
Point(window.cols-60, 20), FONT_HERSHEY_PLAIN, 1.5,
Scalar(0,0,255), 2);
if (avgload>1.0)
putText(window, "CPU OVERLOAD",
Point(0,80), FONT_HERSHEY_PLAIN, 1.9,
Scalar(0,0,255), 2);
QImage qimg = Mat2QImage(window);
emit qimgReady(idx, qimg);
} else
qDebug() << "Camera" << idx << ": Skipped frame";
} else if (was_active) {
was_active = false;
QImage qimg = Mat2QImage(Mat::zeros(window_size, CV_8UC3));
emit qimgReady(idx, qimg);
}
//write previous and current frame timestamp to console
//qDebug() << nextFrameTimestamp << " " << currentFrameTimestamp << " ";
// determine time when all processing done
processingDoneTimestamp = microsec_clock::local_time();
// wait for X microseconds until 1second/framerate time has passed after previous frame write
while(td.total_microseconds() < 1000000/framerate){
//determine current elapsed time
currentFrameTimestamp = microsec_clock::local_time();
td = (currentFrameTimestamp - nextFrameTimestamp);
}
// add 1second/framerate time for next loop pause
nextFrameTimestamp = nextFrameTimestamp + microsec(1000000/framerate);
// reset time_duration so while loop engages
td = (currentFrameTimestamp - nextFrameTimestamp);
//determine and print out delay in ms, should be less than 1000/FPS
//occasionally, if delay is larger than said value, correction will occur
//if delay is consistently larger than said value, then CPU is not powerful
// enough to capture/decompress/record/compress that fast.
finalLoopTimestamp = microsec_clock::local_time();
td1 = (processingDoneTimestamp - initialLoopTimestamp);
td2 = (finalLoopTimestamp - initialLoopTimestamp);
tdlist << td1;
size_t tdlistsize = tdlist.size();
if (tdlistsize>100)
tdlist.removeFirst();
long total_td = 0;
QLinkedList<time_duration>::const_iterator it;
for (it = tdlist.constBegin(); it != tdlist.constEnd(); ++it)
total_td += it->total_microseconds();
avgload = total_td*framerate/(tdlistsize*1000000.0);
/*
QDebug dbg = qDebug(); dbg.noquote();
dbg << idx << ": Processing:"
<< QString("%1").arg(td1.total_microseconds(), 6, 10, QChar(' '))
<< "us. Final:"
<< QString("%1").arg(td2.total_microseconds(), 6, 10, QChar(' '))
<< "us. Target:" << 1000000/framerate << "us. ("
<< td2.total_microseconds()*framerate/10000.0 << "% ) Load:"
<< avgload << tdlistsize;
*/
} // for (;;) // td2.total_milliseconds()
emit resultReady(result);
}
// ---------------------------------------------------------------------
void CameraThread::resizeAR(Mat &frame, Size osize) {
float o_aspect_ratio = float(osize.width)/float(osize.height);
float f_aspect_ratio = float(frame.cols)/float(frame.rows);
//qDebug() << o_aspect_ratio << f_aspect_ratio << fabs(f_aspect_ratio-o_aspect_ratio);
if (fabs(f_aspect_ratio-o_aspect_ratio)<0.01) {
resize(frame, frame, osize);
return;
}
Mat newframe = Mat::zeros(osize, CV_8UC3);
size_t roi_width = size_t(f_aspect_ratio*osize.height);
size_t roi_displacement = (osize.width-roi_width)/2;
Mat roi(newframe, Rect(roi_displacement, 0, roi_width, osize.height));
resize(frame, roi, roi.size());
newframe.copyTo(frame);
//qDebug() << roi.cols << roi.rows;
return;
}
// ---------------------------------------------------------------------
void CameraThread::setOutputDirectory(const QString &d) {
outdir = d+"/";
}
// ---------------------------------------------------------------------
void CameraThread::onStateChanged(QMediaRecorder::State state) {
switch (state) {
case QMediaRecorder::RecordingState:
if (!is_active) {
record_video = false;
break;
}
if (!video.isOpened()) {
qDebug() << QString("CameraThread::onStateChanged(): initializing "
"VideoWriter for camera %1").arg(idx);
video.open(QString(outdir+filename).toStdString(), fourcc, framerate,
(output_size.width ? output_size : input_size));
}
if (!video.isOpened()) {
emit errorMessage(QString("ERROR: Failed to initialize camera %1")
.arg(idx));
} else {
qDebug() << QString("CameraThread::onStateChanged(): initialization "
"ready for camera %1").arg(idx);
record_video = true;
}
break;
case QMediaRecorder::PausedState:
record_video = false;
break;
case QMediaRecorder::StoppedState:
record_video = false;
break;
}
}
// ---------------------------------------------------------------------
QImage CameraThread::Mat2QImage(cv::Mat const& src) {
cv::Mat temp;
cvtColor(src, temp,CV_BGR2RGB);
QImage dest((const uchar *) temp.data, temp.cols, temp.rows,
temp.step, QImage::Format_RGB888);
dest.bits(); // enforce deep copy, see documentation
return dest;
}
// ---------------------------------------------------------------------
void CameraThread::setCameraOutput(QString wxh) {
qDebug() << "CameraThread::setCameraOutput(): " << wxh;
if (wxh == "Original") {
output_size = Size(0,0);
} else {
QStringList wh = wxh.split("x");
if (wh.length()==2) {
output_size = Size(wh.at(0).toInt(), wh.at(1).toInt());
}
}
}
// ---------------------------------------------------------------------
void CameraThread::setCameraFramerate(QString fps) {
qDebug() << "CameraThread::setCameraFramerate(): " << fps;
framerate = fps.toInt();
}
// ---------------------------------------------------------------------
void CameraThread::breakLoop() {
stopLoop = true;
}
// ---------------------------------------------------------------------
void CameraThread::setCameraPower(int i, int state) {
if (i == idx) {
qDebug() << "Camera" << idx << "power now" << state;
is_active = state;
}
}
// ---------------------------------------------------------------------
// Local Variables:
// c-basic-offset: 4
// End: