-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoengine.cpp
125 lines (109 loc) · 2.9 KB
/
videoengine.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
#include <opencv2/opencv.hpp>
#include <QImage>
#include <QMutexLocker>
#include <QDir>
#include <QDebug>
#include <string>
#include "videoengine.h"
#include "colorkeyerhsv.h"
// http://stackoverflow.com/questions/7801522/integrating-opencv-with-larger-programs
VideoEngine::VideoEngine()
: stopped(false)
, processor(0)
, usingCamera(false)
{
}
VideoEngine::~VideoEngine(){
stop();
wait();
}
const VideoFormat& VideoEngine::videoFormat() const{
return _videoFormat;
}
void VideoEngine::setProcessor(VideoProcessor *processor){
this->processor = processor;
}
void VideoEngine::openFile(const QString& file){
try {
bool success = videoCapture.open(file.toStdString());
if (!success){
qDebug() << "Error: cannot open File: " << file;
}
else{
_videoFormat.setFormat(videoCapture);
qDebug() << "Successfully opened file: " << file;
qDebug() << "Format: " << _videoFormat;
}
} catch (cv::Exception e) {
qDebug() << e.err.c_str();
}
}
void VideoEngine::openCamera(int device, int channel){
cameraChannel = channel;
try {
bool success = videoCapture.open(device);
if (!success){
qDebug() << "Error: cannot open camera ";
}
else{
_videoFormat.setFormat(videoCapture);
qDebug() << "Successfully opened from camera";
qDebug() << "Format: " << _videoFormat;
}
} catch (cv::Exception e) {
qDebug() << e.err.c_str();
}
}
void VideoEngine::stop()
{
QMutexLocker locker(&mutex);
stopped = true;
}
void VideoEngine::run()
{
float fps = _videoFormat.framesPerSecond();
if (fps == 0){
fps = 60;
}
int milliSeconds = 1000/fps;
int frameNumber = 0;
while(!stopped)
{
cv::Mat cvFrame;
if (false == videoCapture.grab()){
qDebug() << "grab() failed";
break;
}
if (false == videoCapture.retrieve(cvFrame, cameraChannel)){
qDebug() << "retreive failed";
break;
}
// retrieve Mat::type()
frameNumber++;
if (frameNumber == 1){
_videoFormat.setType(cvFrame.type());
if (processor != 0){
processor->startProcessing(_videoFormat);
}
}
// queue the image to the gui
emit sendInputImage(cvMatToQImage(cvFrame));
// Process Video Frame
if (processor != 0){
cvFrame = processor->process(cvFrame);
}
emit sendProcessedImage(cvMatToQImage(cvFrame));
// check if stopped
QMutexLocker locker(&mutex);
if (stopped) {
break;
}
if (usingCamera == false){
msleep(milliSeconds);
}
}
videoCapture.release();
}
int VideoEngine::framePosition(){
return videoCapture.get(CV_CAP_PROP_POS_FRAMES);
}