-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTrackingThread.cpp
More file actions
441 lines (364 loc) · 13 KB
/
TrackingThread.cpp
File metadata and controls
441 lines (364 loc) · 13 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "TrackingThread.h"
#include <iostream>
#include <chrono>
#include <thread>
#include <QFileInfo>
#include "settings/Messages.h"
#include "settings/Settings.h"
#include "settings/ParamNames.h"
#include <QCoreApplication>
#include <QtOpenGL/qgl.h>
#include <biotracker/util/ScreenHelper.h>
#include <QPainter>
namespace BioTracker {
namespace Core {
using GUIPARAM::MediaType;
TrackingThread::TrackingThread(Settings &settings) :
m_imageStream(make_ImageStreamNoMedia()),
m_playing(false),
m_playOnce(false),
m_somethingIsLoaded(false),
m_status(TrackerStatus::NothingLoaded),
m_fps(30),
m_maxSpeed(false),
m_mediaType(MediaType::NoMedia),
m_settings(settings) {
Interpreter::Interpreter p;
}
TrackingThread::~TrackingThread(void) {
}
void TrackingThread::loadFromSettings() {
std::string filenameStr = m_settings.getValueOfParam<std::string>
(CAPTUREPARAM::CAP_VIDEO_FILE);
boost::filesystem::path filename {filenameStr};
m_imageStream = make_ImageStreamVideo(filename);
if (m_imageStream->type() == GUIPARAM::MediaType::NoMedia) {
// could not open video
std::string errorMsg = "unable to open file " + filename.string();
Q_EMIT notifyGUI(errorMsg, MSGS::MTYPE::FAIL);
m_status = TrackerStatus::Invalid;
return;
} else {
playOnce();
}
m_fps = m_imageStream->fps();
Q_EMIT fileOpened(filenameStr, m_imageStream->numFrames(), m_fps);
std::string note = "opened file: " + filenameStr + " (#frames: "
+ QString::number(m_imageStream->numFrames()).toStdString() + ")";
Q_EMIT notifyGUI(note, MSGS::MTYPE::FILE_OPEN);
}
void TrackingThread::loadVideo(const boost::filesystem::path &filename) {
m_imageStream = make_ImageStreamVideo(filename);
if (m_imageStream->type() == GUIPARAM::MediaType::NoMedia) {
// could not open video
std::string errorMsg = "unable to open file " + filename.string();
Q_EMIT notifyGUI(errorMsg, MSGS::MTYPE::FAIL);
m_status = TrackerStatus::Invalid;
return;
} else {
playOnce();
}
m_fps = m_imageStream->fps();
m_settings.setParam(CAPTUREPARAM::CAP_VIDEO_FILE, filename.string());
std::string note = filename.string() + " (#frames: "
+ QString::number(m_imageStream->numFrames()).toStdString() + ")";
Q_EMIT fileOpened(filename.string(), m_imageStream->numFrames(), m_fps);
Q_EMIT notifyGUI(note, MSGS::MTYPE::FILE_OPEN);
}
void TrackingThread::loadPictures(std::vector<boost::filesystem::path>
&&filenames) {
m_fps = 1;
m_imageStream = make_ImageStreamPictures(std::move(filenames));
if (m_imageStream->type() == GUIPARAM::MediaType::NoMedia) {
// could not open video
std::string errorMsg = "unable to open files [";
for (boost::filesystem::path filename: filenames) {
errorMsg += ", " + filename.string();
}
errorMsg += "]";
Q_EMIT notifyGUI(errorMsg, MSGS::MTYPE::FAIL);
m_status = TrackerStatus::Invalid;
return;
} else {
playOnce();
Q_EMIT fileOpened(m_imageStream->currentFilename(), m_imageStream->numFrames(),
m_fps);
}
}
void TrackingThread::openCamera(int device) {
m_imageStream = make_ImageStreamCamera(device);
if (m_imageStream->type() == GUIPARAM::MediaType::NoMedia) {
// could not open video
std::string errorMsg = "unable to open camera " + QString::number(
device).toStdString();
Q_EMIT notifyGUI(errorMsg, MSGS::MTYPE::FAIL);
m_status = TrackerStatus::Invalid;
return;
}
m_status = TrackerStatus::Running;
m_fps = m_imageStream->fps();
std::string note = "open camera " + QString::number(device).toStdString();
Q_EMIT notifyGUI(note, MSGS::MTYPE::NOTIFICATION);
m_somethingIsLoaded = true;
}
void TrackingThread::run() {
std::chrono::system_clock::time_point t;
bool firstLoop = true;
bool ignoreFps = false;
while (true) {
std::unique_lock<std::mutex> lk(m_tickMutex);
m_conditionVariable.wait(lk, [&] {return (m_playing || m_playOnce) && !m_isRendering;});
m_isRendering = true;
ignoreFps = m_playOnce && !m_playing;
m_playOnce = false;
if (m_imageStream->lastFrame()) {
setPause();
}
//if thread just started (or is unpaused) start clock here
//after this timestamp will be taken right before picture is drawn
//to take the amount of time into account it takes to draw the picture
if (firstLoop) { // measure the capture start time
t = std::chrono::system_clock::now();
}
firstLoop = false;
std::chrono::microseconds target_dur(static_cast<int>(1000000. / m_fps));
std::chrono::microseconds dur =
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now() - t);
if (!m_maxSpeed) {
if (dur <= target_dur) {
target_dur -= dur;
} else {
target_dur = std::chrono::microseconds(0);
}
} else {
target_dur = std::chrono::microseconds(0);
}
// calculate the running fps.
double runningFps = 1000000. / std::chrono::duration_cast<std::chrono::microseconds>
(dur + target_dur).count();
if (ignoreFps) {
runningFps = -1;
}
tick(runningFps);
std::this_thread::sleep_for(target_dur);
t = std::chrono::system_clock::now();
lk.unlock();
}
}
void TrackingThread::tick(const double fps) {
m_renderMutex.lock();
std::string fileName = m_imageStream->currentFilename();
doTracking();
const size_t currentFrame = m_imageStream->currentFrameNumber();
if (m_playing) {
nextFrame();
}
m_renderMutex.unlock();
m_paintMutex.lock();
Q_EMIT frameCalculated(currentFrame, fileName, fps);
m_paintMutex.unlock();
}
void TrackingThread::setFrameNumber(size_t frameNumber) {
m_renderMutex.lock();
if (m_imageStream->setFrameNumber(frameNumber)) {
if (m_tracker) {
m_tracker->setCurrentFrameNumber(frameNumber);
}
playOnce();
}
m_renderMutex.unlock();
}
void TrackingThread::nextFrame() {
if (m_imageStream->nextFrame()) { // increments the frame number if possible
if (m_tracker) {
m_tracker->setCurrentFrameNumber(m_imageStream->currentFrameNumber());
}
} else {
m_playing = false;
m_playOnce = false;
}
}
void TrackingThread::doTracking() {
MutexLocker trackerLock(m_trackerMutex);
if (!m_tracker) {
return;
}
// do nothing if we aint got a frame
if (m_imageStream->currentFrameIsEmpty()) {
return;
}
try {
m_tracker->track(m_imageStream->currentFrameNumber(),
m_imageStream->currentFrame());
} catch (const std::exception &err) {
Q_EMIT notifyGUI("critical error in selected tracking algorithm: " +
std::string(
err.what()), MSGS::FAIL);
}
}
size_t TrackingThread::getVideoLength() const {
return m_imageStream->numFrames();
}
void TrackingThread::mouseEvent(QMouseEvent *event) {
MutexLocker lock(m_trackerMutex);
if (m_tracker) {
QCoreApplication::sendEvent(m_tracker.get(), event);
}
}
void BioTracker::Core::TrackingThread::mouseWheelEvent(QWheelEvent *event) {
MutexLocker lock(m_trackerMutex);
if (m_tracker) {
QCoreApplication::sendEvent(m_tracker.get(), event);
}
}
void TrackingThread::keyboardEvent(QKeyEvent *event) {
MutexLocker lock(m_trackerMutex);
if (m_tracker) {
QCoreApplication::sendEvent(m_tracker.get(), event);
}
}
size_t TrackingThread::getFrameNumber() const {
return m_imageStream->currentFrameNumber();
}
void TrackingThread::setPause() {
m_playing = false;
m_status = TrackerStatus::Paused;
}
void TrackingThread::setPlay() {
m_playing = true;
m_status = TrackerStatus::Running;
m_conditionVariable.notify_all();
}
void TrackingThread::paintDone() {
if (m_somethingIsLoaded) {
m_isRendering = false;
m_conditionVariable.notify_all();
}
}
void TrackingThread::togglePlaying() {
if (m_playing) {
setPause();
} else {
setPlay();
}
}
void TrackingThread::playOnce() {
m_status = TrackerStatus::Paused;
m_playOnce = true;
m_somethingIsLoaded = true;
m_texture.set(m_imageStream->currentFrame());
m_conditionVariable.notify_all();
}
bool TrackingThread::isPaused() const {
return !m_playing;
}
bool TrackingThread::isRendering() const {
return m_isRendering;
}
double TrackingThread::getFps() const {
return m_fps;
}
void TrackingThread::setFps(double fps) {
m_fps = fps;
}
void TrackingThread::setTrackingAlgorithm(std::shared_ptr<TrackingAlgorithm>
trackingAlgorithm) {
{
MutexLocker lock(m_trackerMutex);
if (m_tracker) {
m_tracker.get()->disconnect();
}
m_tracker = trackingAlgorithm;
// connect the tracker
QObject::connect(m_tracker.get(), &TrackingAlgorithm::registerViews,
this, &TrackingThread::registerViewsFromTracker);
QObject::connect(m_tracker.get(), &TrackingAlgorithm::update,
this, &TrackingThread::requestPaint);
QObject::connect(m_tracker.get(), &TrackingAlgorithm::forceTracking,
this, &TrackingThread::requestTrackFromTracker);
QObject::connect(m_tracker.get(), &TrackingAlgorithm::notifyGUI,
this, &TrackingThread::notifyGUIFromTracker);
QObject::connect(m_tracker.get(), &TrackingAlgorithm::pausePlayback,
this, &TrackingThread::requestPauseFromTracker);
m_tracker.get()->postConnect();
}
Q_EMIT trackerSelected(trackingAlgorithm);
}
void TrackingThread::setMaxSpeed(bool enabled) {
m_maxSpeed = enabled;
}
void BioTracker::Core::TrackingThread::paint(const size_t w, const size_t h, QPainter &painter,
BioTracker::Core::PanZoomState &zoom, TrackingAlgorithm::View const &v) {
// clear background
painter.setBrush(QColor(0, 0, 0));
painter.drawRect(QRect(0, 0, w, h));
painter.setBrush(QColor(0, 0, 0, 0));
m_paintMutex.lock();
// using painters algorithm to draw in the right order
if (m_somethingIsLoaded) {
ProxyMat proxy(m_imageStream->currentFrame());
if (m_tracker) {
m_tracker.get()->paint(proxy, v);
}
if (proxy.isModified() || (m_lastFrameNumber != m_imageStream->currentFrameNumber())) {
m_texture.set(proxy.getMat());
m_lastFrameNumber = m_imageStream->currentFrameNumber();
}
// We use setWindow and setViewport to fit the video into the
// given video widget frame (with width "w" and height "h")
// we later need to adjust an offset caused the use of different
// dimensions for window and viewport.
// adjust the panning as the viewport is potentially scewed
// and mouse movements given by the window are not translated
// one-to-one anymore
QRect window;
QRect viewport;
const float viewport_skew = ScreenHelper::calculate_viewport(
m_texture.width(),
m_texture.height(),
w, h, window, viewport
);
painter.setWindow(window);
painter.setViewport(viewport);
float zoomFactor = 1 + zoom.zoomFactor;
painter.scale(zoomFactor, zoomFactor);
painter.translate(
QPointF(
-zoom.panX * viewport_skew / zoomFactor,
-zoom.panY * viewport_skew / zoomFactor
)
);
// TODO only paint that part that is visible
painter.drawImage(
QRectF(0, 0 , m_texture.width(), m_texture.height()),
m_texture.get(),
QRect(0, 0, m_texture.width(), m_texture.height()));
if (m_tracker) {
m_tracker.get()->paintOverlay(&painter, v);
}
paintDone();
}
m_paintMutex.unlock();
}
void BioTracker::Core::TrackingThread::registerViewsFromTracker(const std::vector<TrackingAlgorithm::View> views) {
Q_EMIT registerViews(views);
}
void BioTracker::Core::TrackingThread::requestPaintFromTracker() {
Q_EMIT requestPaint();
}
void BioTracker::Core::TrackingThread::requestTrackFromTracker() {
playOnce();
}
void BioTracker::Core::TrackingThread::notifyGUIFromTracker(std::string m, MSGS::MTYPE type) {
Q_EMIT notifyGUI(m, type);
}
void BioTracker::Core::TrackingThread::requestPauseFromTracker(bool pause) {
if (pause) {
setPause();
} else {
setPlay();
}
}
}
}