video.cpp
3.77 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright 2012 The MITRE Corporation *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <opencv2/highgui/highgui.hpp>
#include <openbr/plugins/openbr_internal.h>
#include <openbr/core/qtutils.h>
namespace br
{
// Read a video frame by frame using cv::VideoCapture
class videoGallery : public Gallery
{
Q_OBJECT
public:
qint64 idx;
~videoGallery()
{
video.release();
}
static QMutex openLock;
virtual void deferredInit()
{
bool status = video.open(QtUtils::getAbsolutePath(file.name).toStdString());
if (!status)
qFatal("Failed to open file %s with path %s", qPrintable(file.name), qPrintable(QtUtils::getAbsolutePath(file.name)));
}
TemplateList readBlock(bool *done)
{
if (!video.isOpened()) {
// opening videos appears to not be thread safe on windows
QMutexLocker lock(&openLock);
deferredInit();
idx = 0;
}
Template output;
output.file = file;
output.m() = cv::Mat();
cv::Mat temp;
bool res = video.read(temp);
if (!res) {
// The video capture broke, return an empty list.
output.m() = cv::Mat();
video.release();
*done = true;
return TemplateList();
}
// This clone is critical, if we don't do it then the output matrix will
// be an alias of an internal buffer of the video source, leading to various
// problems later.
output.m() = temp.clone();
output.file.set("progress", idx);
idx++;
TemplateList rVal;
rVal.append(temp);
*done = false;
return rVal;
}
void write(const Template &t)
{
(void)t; qFatal("Not implemented");
}
protected:
cv::VideoCapture video;
};
BR_REGISTER(Gallery,videoGallery)
QMutex videoGallery::openLock;
class aviGallery : public videoGallery
{
Q_OBJECT
};
BR_REGISTER(Gallery, aviGallery)
class wmvGallery : public videoGallery
{
Q_OBJECT
};
BR_REGISTER(Gallery, wmvGallery)
// Mostly the same as videoGallery, but we open the VideoCapture with an integer index
// rather than file name/web address
class webcamGallery : public videoGallery
{
public:
Q_OBJECT
void deferredInit()
{
bool intOK = false;
int anInt = file.baseName().toInt(&intOK);
if (!intOK)
qFatal("Expected integer basename, got %s", qPrintable(file.baseName()));
bool rc = video.open(anInt);
if (!rc)
qFatal("Failed to open webcam with index: %s", qPrintable(file.baseName()));
}
};
BR_REGISTER(Gallery,webcamGallery)
} // namespace br
#include "gallery/video.moc"