-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlauwebcamerawidget.cpp
299 lines (262 loc) · 11.5 KB
/
lauwebcamerawidget.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
#include "lauwebcamerawidget.h"
#include "lautiredetectorglfilter.h"
#include "laurandomizepixelsglwidget.h"
#include "lausobeledgedetectorglwidget.h"
#include "lauharriscornerdetectorglwidget.h"
#include "lauhistogramequalizationglwidget.h"
#include "laufacialfeaturedetectorglwidget.h"
#include <QCameraInfo>
#include <QMessageBox>
#include <QFileDialog>
#include <QSettings>
QUrl LAUWebCameraWidget::localURL = QUrl::fromLocalFile(QString("%1/videofile.mp4").arg(QStandardPaths::writableLocation(QStandardPaths::TempLocation)));
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUWebCameraWidget::LAUWebCameraWidget(QCamera::CaptureMode capture, QWidget *parent) : QWidget(parent), mode(capture), thread(NULL), camera(NULL), recorder(NULL), imageCapture(NULL), surface(NULL)
{
// SEE IF THERE IS A LEFTOVER VIDEO FILE FROM A PREVIOUS RUN OF THE SOFTWARE
//saveVideoFile();
this->setLayout(new QVBoxLayout());
this->layout()->setContentsMargins(6, 6, 6, 6);
// ASK THE USER WHAT FILTER THEY WANT TO IMPLEMENT
QStringList items;
items << QString("Facial Features");
items << QString("Harris Corners");
items << QString("Randomized Pixels");
items << QString("Histogram Equalize");
items << QString("Tire Detector");
items << QString("Raw Video");
items << QString("Sobel Edges");
bool ok = false;
QString string = QInputDialog::getItem(nullptr, QString("Web Camera Widget"), QString("Select video filter"), items, 3, false, &ok);
if (ok) {
if (string == QString("Raw Video")) {
label = new LAUVideoGLWidget();
} else if (string == QString("Facial Features")) {
label = new LAUFacialFeatureDetectorGLWidget();
} else if (string == QString("Tire Detector")) {
label = new LAUTireDetectorGLWidget();
} else if (string == QString("Harris Corners")) {
label = new LAUHarrisCornerDetectorGLWidget();
} else if (string == QString("Histogram Equalize")){
label = new LAUHistogramEqualizationGLWidget();
} else if (string == QString("Randomized Pixels")) {
label = new LAURandomizePixelsGLWidget();
} else if (string == QString("Sobel Edges")) {
label = new LAUSobelEdgeDetectorGLWidget();
}
} else {
label = new LAUVideoGLWidget();
}
#ifdef Q_OS_WIN
label->setVideoRecorder(&recorder);
#endif
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->layout()->addWidget(label);
QStringList strings;
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
for (int n = 0; n < cameras.count(); n++) {
strings << cameras.at(n).description();
}
if (strings.count() > 1) {
bool okay = false;
QString string = QInputDialog::getItem(this, QString("Select Camera"), QString("Select input device"), strings, 0, false, &okay);
if (okay) {
int n = strings.indexOf(string);
camera = new QCamera(cameras.at(n));
}
} else if (strings.count() == 1) {
camera = new QCamera(cameras.first());
} else {
QMessageBox::warning(this, QString("LAUWebCam"), QString("No camera available."));
}
if (camera) {
surface = new LAUVideoSurface();
surface->setLabel(label);
QCameraViewfinderSettings set = camera->viewfinderSettings();
//QList<QSize> resolutions = camera->supportedViewfinderResolutions(set);
//QList<QVideoFrame::PixelFormat> formats = camera->supportedViewfinderPixelFormats(set);
//QList<QCamera::FrameRateRange> ranges = camera->supportedViewfinderFrameRateRanges(set);
set.setResolution(LAUWEBCAMERAWIDGETWIDTH, LAUWEBCAMERAWIDGETHEIGHT);
set.setMaximumFrameRate(LAUWEBCAMERAWIDGETFPS);
set.setMinimumFrameRate(LAUWEBCAMERAWIDGETFPS);
//set.setPixelFormat(QVideoFrame::Format_ARGB32);
camera->setViewfinderSettings(set);
camera->setViewfinder(surface);
camera->setCaptureMode(mode);
if (mode == QCamera::CaptureStillImage) {
imageCapture = new QCameraImageCapture(camera);
imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(onImageAvailable(int, QImage)));
}
// CREATE A NEW THREAD TO HOST THE CAMERA
thread = new QThread();
camera->moveToThread(thread);
thread->start();
}
label->setMinimumSize(qMin(LAUWEBCAMERAWIDGETWIDTH, 640), qMin(LAUWEBCAMERAWIDGETHEIGHT, 480));
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUWebCameraWidget::~LAUWebCameraWidget()
{
if (thread) {
thread->quit();
while (thread->isRunning()) {
qApp->processEvents();
}
delete thread;
}
if (surface) {
surface->stop();
delete surface;
}
if (camera) {
camera->stop();
delete camera;
}
// DELETE TEMPORARY VIDEO RECORDING FILE IF IT EXISTS
if (QFile::exists(localURL.toLocalFile())) {
QFile::remove(localURL.toLocalFile());
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUWebCameraWidget::onCapture()
{
if (imageCapture) {
// WAIT HERE UNTIL CAMERA IS READY TO CAPTURE
while (imageCapture->isReadyForCapture() == false) {
qApp->processEvents();
}
imageCapture->capture();
if (imageCapture->error() != QCameraImageCapture::NoError) {
qDebug() << imageCapture->errorString();
}
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUWebCameraWidget::onTriggerVideo(bool state)
{
qDebug() << "Trigger video recording:" << state;
if (recorder) {
#ifndef Q_OS_WIN
// GET OUTPUT LOCATION
localURL = recorder->outputLocation();
// STOP RECORDING AND DELETE THE RECORDER
recorder->stop();
#endif
// DELETE THE RECORDER
#ifdef Q_OS_WIN
delete recorder;
#else
recorder->deleteLater();
#endif
recorder = nullptr;
// LET THE USER SAVE THE VIDEO FILE TO DISK
saveVideoFile();
} else {
#ifdef Q_OS_WIN
recorder = new cv::VideoWriter();
if (recorder->open(localURL.toString().toStdString(), cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10.0, cv::Size(LAUWEBCAMERAWIDGETWIDTH, LAUWEBCAMERAWIDGETHEIGHT), true)) {
qDebug() << "Recording to file:" << localURL.toString();
}
#else
// CREATE NEW RECORDER
recorder = new QMediaRecorder(camera);
// SET AUDIO PARAMETERS
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
recorder->setAudioSettings(audioSettings);
// SET THE SINK
recorder->setOutputLocation(localURL);
recorder->record();
#endif
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUWebCameraWidget::onImageAvailable(int id, QImage image)
{
Q_UNUSED(id);
QLabel *label = new QLabel();
label->setPixmap(QPixmap::fromImage(image));
label->show();
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUWebCameraWidget::grabImage()
{
if (label) {
QImage image = label->grabImage();
if (image.isNull() == false) {
QSettings settings;
QString directory = settings.value("LAUWebCameraWidget::lastUsedDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
QString filename = QFileDialog::getSaveFileName(0, QString("Save image to disk (*.tif)"), directory, QString("*.tif *.tiff"));
if (filename.isEmpty() == false) {
settings.setValue("LAUWebCameraWidget::lastUsedDirectory", QFileInfo(filename).absolutePath());
if (filename.toLower().endsWith(".tif") == false && filename.toLower().endsWith(".tiff")) {
filename.append(".tif");
}
} else {
return;
}
image.save(filename, "TIFF");
}
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUWebCameraWidget::saveVideoFile()
{
// MAKE SURE TEMPORARY VIDEO FILE EXISTS
if (QFile::exists(localURL.toLocalFile()) == false) {
return (false);
}
// GET THE LAST USED DIRECTORY FROM SETTINGS
QSettings settings;
QString directory = settings.value("LAUWebCameraWidget::lastUsedDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
if (QDir().exists(directory) == false) {
directory = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
QString filename;
for (int counter = 0; counter < 1000; counter++) {
if (counter < 10) {
filename = QString("%1/intubation00%2.mp4").arg(directory).arg(counter);
} else if (counter < 100) {
filename = QString("%1/intubation0%2.mp4").arg(directory).arg(counter);
} else {
filename = QString("%1/intubation%2.mp4").arg(directory).arg(counter);
}
if (QFile::exists(filename) == false) {
break;
}
}
while (1) {
// COPY TO A USER SPECIFIED FILE
filename = QFileDialog::getSaveFileName(nullptr, QString("Save video to disk (*.mp4)"), filename, QString("*.mp4"));
if (filename.isEmpty() == false) {
if (filename.toLower().endsWith(".mp4") == false) {
filename.append(".mp4");
}
settings.setValue("LAUWebCameraWidget::lastUsedDirectory", QFileInfo(filename).absolutePath());
// RENAME THE TEMPORARY RECORDING TO A PERMANENT FILE
return (QFile::rename(localURL.toLocalFile(), filename));
}
// GIVE THE USER ANOTHER CHANCE
if (QMessageBox::warning(this, QString("Webcam Recorder"), QString("You are about to discard the recording and lose the data forever.\n\nDo you want to do this?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
if (QMessageBox::warning(this, QString("Webcam Recorder"), QString("Are you sure?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
return (false);
}
}
}
}