-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathImageStream.cpp
More file actions
771 lines (677 loc) · 27.2 KB
/
ImageStream.cpp
File metadata and controls
771 lines (677 loc) · 27.2 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#include "ImageStream.h"
#include "util/stdext.h"
#include <cassert> // assert
#include <stdexcept> // std::invalid_argument
#include <chrono>
#include <mutex>
#include <thread>
#include <limits>
#include <boost/circular_buffer.hpp>
#include "util/Exceptions.h"
#include "QSharedPointer"
#include "Utility/misc.h"
#include "View/CameraDevice.h"
#include "util/VideoCoder.h"
#include "Controller/IControllerCfg.h"
#if HAS_PYLON
#include "util/camera/pylon.h"
#include <opencv2/opencv.hpp>
#endif
#include <iostream>
namespace BioTracker
{
namespace Core
{
ImageStream::ImageStream(QObject* parent, Config* cfg)
: QObject(parent)
, m_current_frame(cv::Mat(cv::Size(0, 0), CV_8UC3))
, m_current_frame_number(0)
{
_cfg = cfg;
if (cfg)
m_frame_stride = cfg->FrameStride;
}
size_t ImageStream::currentFrameNumber() const
{
return m_current_frame_number;
}
cv::Mat ImageStream::currentFrame() const
{
return m_current_frame;
}
bool ImageStream::setFrameNumber(size_t frame_number)
{
// valid new frame number
if (frame_number < this->numFrames()) {
// skip update if frame number doesn't change
if (frame_number == this->currentFrameNumber()) {
return true;
} else {
const bool success = this->setFrameNumber_impl(
frame_number);
m_current_frame_number = frame_number;
return success;
}
}
// invalid new frame number
else {
this->clearImage();
return false;
}
}
bool ImageStream::lastFrame() const
{
return this->currentFrameNumber() + 1 == this->numFrames();
}
bool ImageStream::end() const
{
return (this->currentFrameNumber() >= this->numFrames() &&
this->numFrames() >= 0);
}
bool ImageStream::currentFrameIsEmpty() const
{
return this->currentFrame().empty();
}
bool ImageStream::nextFrame()
{
const size_t new_frame_number = this->currentFrameNumber() +
m_frame_stride;
if (new_frame_number < this->numFrames()) {
const bool success = this->nextFrame_impl();
m_current_frame_number = new_frame_number;
return success;
} else {
this->clearImage();
return false;
}
}
bool ImageStream::previousFrame()
{
if (this->currentFrameNumber() > 0) {
const size_t new_frame_numer = this->currentFrameNumber() - 1;
const bool success = this->previousFrame_impl();
m_current_frame_number = new_frame_numer;
return success;
} else {
this->clearImage();
return false;
}
}
void ImageStream::set_current_frame(cv::Mat img)
{
m_current_frame = img;
}
void ImageStream::clearImage()
{
m_current_frame = cv::Mat(cv::Size(0, 0), CV_8UC3);
m_current_frame_number = this->numFrames();
}
bool ImageStream::nextFrame_impl()
{
assert(this->currentFrameNumber() + 1 < this->numFrames());
const size_t new_frame_number = this->currentFrameNumber() + 1;
return this->setFrameNumber_impl(new_frame_number);
}
bool ImageStream::previousFrame_impl()
{
assert(this->currentFrameNumber() > 0);
const size_t new_frame_number = this->currentFrameNumber() - 1;
return this->setFrameNumber_impl(new_frame_number);
}
bool ImageStream::hasNextInBatch()
{
return false;
}
void ImageStream::stepToNextInBatch()
{
}
std::vector<std::string> ImageStream::getBatchItems()
{
return {};
}
ImageStream::~ImageStream() = default;
/*********************************************************/
class ImageStream3NoMedia : public ImageStream
{
public:
explicit ImageStream3NoMedia() = default;
virtual GuiParam::MediaType type() const override
{
return GuiParam::MediaType::NoMedia;
}
virtual size_t numFrames() const override
{
return 0;
}
virtual bool toggleRecord() override
{
return false;
}
virtual double fps() const override
{
return 1.0;
}
virtual std::string currentFilename() const override
{
return "No Media"; // TODO make this nicer..
}
private:
virtual bool setFrameNumber_impl(size_t) override
{
return false;
}
};
/*********************************************************/
class ImageStream3Pictures : public ImageStream
{
public:
explicit ImageStream3Pictures(
Config* cfg,
std::vector<boost::filesystem::path> picture_files)
: ImageStream(0, cfg)
, m_picture_files(std::move(picture_files))
, m_currentFrame(0)
{
// Grab the codec from config file
double fps = _cfg->RecordFPS;
if (fps > 0) {
m_fps = fps;
} else {
m_fps = 1;
}
// load first image
if (this->numFrames() > 0) {
this->setFrameNumber_impl(0);
auto filename = m_picture_files[0].string();
auto new_frame = cv::imread(filename);
m_w = new_frame.size().width;
m_h = new_frame.size().height;
m_recording = false;
vCoder = std::make_shared<VideoCoder>(m_fps, _cfg);
}
}
virtual GuiParam::MediaType type() const override
{
return GuiParam::MediaType::Images;
}
virtual size_t numFrames() const override
{
return m_picture_files.size();
}
virtual bool toggleRecord() override
{
if (this->numFrames() <= 0) {
return false;
}
m_recording = vCoder->toggle(m_w, m_h, m_fps);
return m_recording;
}
virtual double fps() const override
{
return m_fps;
}
virtual std::string currentFilename() const override
{
assert(currentFrameNumber() < m_picture_files.size());
return m_picture_files[currentFrameNumber()].string();
}
private:
virtual bool nextFrame_impl() override
{
m_currentFrame += static_cast<int>(m_frame_stride);
if (this->numFrames() > m_currentFrame) {
auto filename = m_picture_files[m_currentFrame].string();
auto new_frame = cv::imread(filename);
this->set_current_frame(new_frame);
if (m_recording) {
if (vCoder)
vCoder->add(new_frame);
}
return true;
}
return false;
}
virtual bool setFrameNumber_impl(size_t frame_number) override
{
auto filename = m_picture_files[frame_number].string();
auto new_frame = cv::imread(filename);
this->set_current_frame(new_frame);
m_currentFrame = static_cast<int>(frame_number);
if (m_recording) {
if (vCoder)
vCoder->add(new_frame);
}
return !new_frame.empty();
}
std::vector<boost::filesystem::path> m_picture_files;
std::shared_ptr<VideoCoder> vCoder;
double m_w;
double m_h;
bool m_recording;
int m_currentFrame;
double m_fps;
};
/*********************************************************/
class ImageStream3Video : public ImageStream
{
public:
/**
* @throw file_not_found when the file does not exists
* @throw video_open_error when there is an error with the video
* @brief ImageStreamVideo
* @param filename path to the file
*/
explicit ImageStream3Video(
Config* cfg,
const std::vector<boost::filesystem::path>& files)
: ImageStream(0, cfg)
{
openMedia(files);
}
virtual GuiParam::MediaType type() const override
{
return GuiParam::MediaType::Video;
}
virtual size_t numFrames() const override
{
return m_num_frames;
}
virtual bool toggleRecord() override
{
if (!m_capture.isOpened()) {
return false;
}
m_recording = vCoder->toggle(m_w, m_h, m_fps);
return m_recording;
}
virtual double fps() const override
{
return m_fps;
}
virtual std::string currentFilename() const override
{
return m_fileName;
}
virtual bool hasNextInBatch() override
{
return !(m_batch.empty());
}
virtual void stepToNextInBatch() override
{
if (m_batch.empty()) {
throw video_open_error("batch is empty");
}
openMedia(m_batch);
}
virtual std::vector<std::string> getBatchItems() override
{
std::vector<std::string> batchItems;
for (auto x : m_batch) {
batchItems.push_back(x.string());
}
return batchItems;
}
private:
void openMedia(std::vector<boost::filesystem::path> files)
{
m_capture.open(files.front().string());
m_num_frames = static_cast<size_t>(
m_capture.get(cv::CAP_PROP_FRAME_COUNT));
m_fps = m_capture.get(cv::CAP_PROP_FPS);
m_fileName = files.front().string();
if (!boost::filesystem::exists(files.front())) {
throw file_not_found("Could not find file " +
files.front().string());
}
if (!m_capture.isOpened()) {
throw video_open_error(":(");
}
m_batch = files;
m_batch.erase(m_batch.begin(), m_batch.begin() + 1);
// Grab the fps from config file
double fps = _cfg->RecordFPS;
if (fps != -1) {
m_fps = fps;
}
m_w = m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
m_h = m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
m_recording = false;
vCoder = std::make_shared<VideoCoder>(m_fps, _cfg);
// load first image
if (this->numFrames() > 0) {
this->nextFrame_impl();
}
m_current_frame_number = 0;
}
virtual bool nextFrame_impl() override
{
cv::Mat new_frame;
for (int i = 0; i < m_frame_stride; i++)
m_capture >> new_frame;
this->set_current_frame(new_frame);
if (m_recording) {
if (vCoder)
vCoder->add(new_frame);
}
return !new_frame.empty();
}
virtual bool setFrameNumber_impl(size_t frame_number) override
{
// new frame is next frame --> use next frame function
if (this->currentFrameNumber() + 1 == frame_number) {
return this->nextFrame_impl();
} else {
// adjust frame position ("0-based index of the frame to be
// decoded/captured next.")
m_capture.set(cv::CAP_PROP_POS_FRAMES,
static_cast<double>(frame_number));
return this->nextFrame_impl();
}
}
cv::VideoCapture m_capture;
size_t m_num_frames;
std::string m_fileName;
std::shared_ptr<VideoCoder> vCoder;
std::vector<boost::filesystem::path> m_batch;
double m_fps;
double m_w;
double m_h;
bool m_recording;
};
/*********************************************************/
class ImageStream3OpenCVCamera : public ImageStream
{
public:
/**
* @throw file_not_found when device does not exists
* @throw device_open_error when there is an error with the device
* @brief ImageStreamCamera
* @param device_id according to the VideoCapture class of OpenCV
*/
explicit ImageStream3OpenCVCamera(Config* cfg,
CameraConfiguration conf)
: ImageStream(0, cfg)
, m_capture(conf._selector.name)
, m_fps(m_capture.get(cv::CAP_PROP_FPS))
{
// Give the camera some extra time to get ready:
// Somehow opening it on first try sometimes does not succeed.
// Workaround:
// http://stackoverflow.com/questions/22019064/unable-to-read-frames-from-videocapture-from-secondary-webcam-with-opencv?rq=1
// So, stubbornly try it a few times until it works.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
qDebug() << "\nStarting to record on camera no. "
<< conf._selector.index;
m_w = conf._width == -1 ? _cfg->CameraWidth : conf._width;
m_h = conf._height == -1 ? _cfg->CameraHeight : conf._height;
m_fps = conf._fps == -1 ? _cfg->RecordFPS : conf._fps;
m_recording = false;
vCoder = std::make_shared<VideoCoder>(m_fps, _cfg);
int fails = 0;
while (!m_capture.isOpened() && fails < 5) {
m_capture.open(conf._selector.index);
std::this_thread::sleep_for(
std::chrono::milliseconds(1000));
fails++;
}
if (!m_capture.isOpened()) {
qWarning() << "Unable to open camera!";
throw device_open_error(":(");
}
if (m_w != -1)
m_capture.set(cv::CAP_PROP_FRAME_WIDTH, m_w);
if (m_h != -1)
m_capture.set(cv::CAP_PROP_FRAME_HEIGHT, m_h);
if (m_fps != -1)
m_capture.set(cv::CAP_PROP_FPS, m_fps);
m_w = m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
m_h = m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
m_fps = m_capture.get(cv::CAP_PROP_FPS);
qDebug() << "Cam open: " << m_capture.isOpened()
<< " w/h:" << m_w << "/" << m_h << " fps:" << m_fps;
// load first image
if (this->numFrames() > 0) {
this->nextFrame_impl();
}
}
virtual GuiParam::MediaType type() const override
{
return GuiParam::MediaType::Camera;
}
virtual size_t numFrames() const override
{
return -1; // TODO
}
virtual bool toggleRecord() override
{
if (!m_capture.isOpened()) {
return false;
}
m_recording = vCoder->toggle(m_w, m_h, m_fps);
return m_recording;
}
virtual double fps() const override
{
return m_fps;
}
virtual std::string currentFilename() const override
{
return "Camera"; // TODO be more specific!
}
private:
virtual bool nextFrame_impl() override
{
cv::Mat new_frame;
for (int i = 0; i < m_frame_stride; i++) {
m_capture >> new_frame;
}
this->set_current_frame(new_frame);
if (m_recording) {
if (vCoder)
vCoder->add(new_frame);
}
return !new_frame.empty();
}
virtual bool setFrameNumber_impl(size_t) override
{
// throw std::runtime_error("setFrameNumber not
// available for ImageStreamCamera");
this->nextFrame_impl();
return true;
}
std::shared_ptr<VideoCoder> vCoder;
cv::VideoCapture m_capture;
double m_fps;
double m_w;
double m_h;
bool m_recording;
};
#if HAS_PYLON
/*********************************************************/
class ImageStream3PylonCamera : public ImageStream
{
private:
Pylon::CGrabResultPtr m_grabbed;
public:
explicit ImageStream3PylonCamera(Config* cfg,
CameraConfiguration conf)
: ImageStream(0, cfg)
, m_camera(getPylonDevice(conf._selector.index),
Pylon::Cleanup_Delete)
{
m_fps = conf._fps == -1 ? _cfg->RecordFPS : conf._fps;
m_camera.Open();
if (!m_camera.IsOpen()) {
qWarning() << "Unable to open camera!";
throw device_open_error("Error loading camera");
}
m_imageSize = {[&]() -> int {
if (conf._width != -1)
return conf._width;
if (_cfg->CameraWidth != -1)
return _cfg->CameraWidth;
return GenApi::CIntegerPtr(
m_camera.GetNodeMap().GetNode(
"SensorWidth"))
->GetValue();
}(),
[&]() -> int {
if (conf._height != -1)
return conf._height;
if (_cfg->CameraHeight != -1)
return _cfg->CameraHeight;
return GenApi::CIntegerPtr(
m_camera.GetNodeMap().GetNode(
"SensorHeight"))
->GetValue();
}()};
GenApi::CBooleanPtr(m_camera.GetNodeMap().GetNode(
"AcquisitionFrameRateEnable"))
->SetValue(1);
if (m_fps == -1) {
m_fps = getFrameRate();
} else {
setFrameRate(m_fps);
auto const actual_fps = getFrameRate();
qInfo() << "Actual framerate:" << actual_fps;
if (std::abs(actual_fps - m_fps) > 0.1) {
throw device_open_error("Error setting framerate");
}
m_fps = actual_fps;
}
qDebug() << "\nStarting to record on camera "
<< m_camera.GetDeviceInfo().GetFriendlyName();
m_camera.StartGrabbing(Pylon::GrabStrategy_LatestImages,
Pylon::GrabLoop_ProvidedByUser);
nextFrame_impl();
m_recording = false;
m_encoder = std::make_unique<VideoCoder>(m_fps, _cfg);
}
GuiParam::MediaType type() const override
{
return GuiParam::MediaType::Camera;
}
std::size_t numFrames() const override
{
return std::numeric_limits<std::size_t>::max();
}
bool toggleRecord() override
{
if (!m_camera.IsOpen()) {
return false;
}
m_recording = m_encoder->toggle(m_imageSize.width,
m_imageSize.height,
m_fps);
return m_recording;
}
double fps() const override
{
return m_fps;
}
std::string currentFilename() const override
{
return std::string(m_camera.GetDeviceInfo().GetFriendlyName());
}
private:
bool nextFrame_impl() override
{
for (std::size_t i = 1; i < m_frame_stride; ++i) {
if (!m_camera.RetrieveResult(
2000,
m_grabbed,
Pylon::TimeoutHandling_Return)) {
return false;
}
}
if (!m_camera.RetrieveResult(2000,
m_grabbed,
Pylon::TimeoutHandling_Return)) {
return false;
}
auto view = toOpenCV(m_grabbed);
auto scaled = view.clone();
cv::resize(scaled, scaled, m_imageSize);
set_current_frame(scaled);
if (m_recording && m_encoder)
m_encoder->add(scaled);
return !scaled.empty();
}
bool setFrameNumber_impl(size_t) override
{
return false;
}
double getFrameRate()
{
if (m_camera.IsUsb()) {
return GenApi::CFloatPtr(m_camera.GetNodeMap().GetNode(
"AcquisitionFrameRate"))
->GetValue();
}
throw std::logic_error("Unsupported device class");
}
void setFrameRate(double value)
{
if (m_camera.IsUsb()) {
GenApi::CFloatPtr(
m_camera.GetNodeMap().GetNode("AcquisitionFrameRate"))
->SetValue(value);
} else {
throw std::logic_error("Unsupported camera type");
}
}
Pylon::PylonAutoInitTerm m_pylon;
Pylon::CInstantCamera m_camera;
double m_fps;
cv::Size m_imageSize;
bool m_recording;
std::unique_ptr<VideoCoder> m_encoder;
};
#endif
/*********************************************************/
std::shared_ptr<ImageStream> make_ImageStream3NoMedia()
{
return std::make_shared<ImageStream3NoMedia>();
}
std::shared_ptr<ImageStream> make_ImageStream3Pictures(
Config* cfg,
std::vector<boost::filesystem::path> filenames)
{
return std::make_shared<ImageStream3Pictures>(
cfg,
std::move(filenames));
}
std::shared_ptr<ImageStream> make_ImageStream3Video(
Config* cfg,
const std::vector<boost::filesystem::path>& files)
{
try {
return std::make_shared<ImageStream3Video>(cfg, files);
} catch (const video_open_error&) {
return make_ImageStream3NoMedia();
}
}
std::shared_ptr<ImageStream> make_ImageStream3Camera(
Config* cfg,
CameraConfiguration conf)
{
try {
switch (conf._selector.type) {
case CameraType::OpenCV:
return std::make_shared<ImageStream3OpenCVCamera>(cfg,
conf);
#if HAS_PYLON
case CameraType::Pylon:
return std::make_shared<ImageStream3PylonCamera>(cfg,
conf);
#endif
default:
throw std::logic_error(
"Missing image stream implementation");
}
} catch (const device_open_error&) {
return make_ImageStream3NoMedia();
}
}
}
}