gallerytoolbar.cpp
5.44 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
#include <QDateTime>
#include <QDesktopServices>
#include <QDir>
#include <QFileDialog>
#include <QIcon>
#include <QMessageBox>
#include <QSharedPointer>
#include <QSize>
#include <QtConcurrentRun>
#include <opencv2/highgui/highgui.hpp>
#include <assert.h>
#include <openbr_plugin.h>
#include "gallerytoolbar.h"
#include "utility.h"
/**** GALLERY ****/
/*** STATIC ***/
QMutex br::GalleryToolBar::galleryLock;
/*** PUBLIC ***/
br::GalleryToolBar::GalleryToolBar(QWidget *parent)
: QToolBar("Gallery", parent)
{
lGallery.setAlignment(Qt::AlignCenter);
lGallery.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
tbOpenFile.setIcon(QIcon(":/glyphicons/png/glyphicons_138_picture@2x.png"));
tbOpenFile.setToolTip("Load Photo");
tbOpenFolder.setIcon(QIcon(":/glyphicons/png/glyphicons_144_folder_open@2x.png"));
tbOpenFolder.setToolTip("Load Photo Folder");
tbWebcam.setCheckable(true);
tbWebcam.setIcon(QIcon(":/glyphicons/png/glyphicons_301_webcam@2x.png"));
tbWebcam.setToolTip("Load Webcam");
tbBack.setEnabled(false);
tbBack.setIcon(QIcon(":/glyphicons/png/glyphicons_221_unshare@2x.png"));
tbBack.setToolTip("Back");
tbMean.setIcon(QIcon(":/glyphicons/png/glyphicons_003_user@2x.png"));
tbMean.setToolTip("Mean Image");
addWidget(&tbOpenFile);
addWidget(&tbOpenFolder);
addWidget(&tbWebcam);
addSeparator();
addWidget(&lGallery);
addSeparator();
addWidget(&tbBack);
addWidget(&tbMean);
setIconSize(QSize(20,20));
connect(&tbOpenFile, SIGNAL(clicked()), this, SLOT(openFile()));
connect(&tbOpenFolder, SIGNAL(clicked()), this, SLOT(openFolder()));
connect(&tbBack, SIGNAL(clicked()), this, SLOT(home()));
connect(&tbMean, SIGNAL(clicked()), this, SLOT(mean()));
connect(&enrollmentWatcher, SIGNAL(finished()), this, SLOT(enrollmentFinished()));
connect(&timer, SIGNAL(timeout()), this, SLOT(checkWebcam()));
timer.start(500);
}
/*** PUBLIC SLOTS ***/
void br::GalleryToolBar::enroll(const br::File &input)
{
if (input.isNull()) return;
enrollmentWatcher.setFuture(QtConcurrent::run(this, &GalleryToolBar::_enroll, input));
}
void br::GalleryToolBar::enroll(const QImage &input)
{
QString tempFileName = br::Context::scratchPath() + "/tmp/" + QDateTime::currentDateTime().toString("yyyy-MM-ddThh:mm:ss:zzz") + ".png";
input.save(tempFileName);
enroll(tempFileName);
}
void br::GalleryToolBar::select(const br::File &file)
{
tbBack.setEnabled(true);
emit newFiles(br::FileList() << file);
emit newGallery(file);
}
/*** PRIVATE ***/
void br::GalleryToolBar::_enroll(const br::File &input)
{
galleryLock.lock();
this->input = input;
if (input.suffix() == "gal") gallery = input.name + ".mem";
else gallery = QString("%1/galleries/%2.gal[cache]").arg(br_scratch_path(), qPrintable(input.baseName()+input.hash()));
files = br::Enroll(input.flat(), gallery.flat());
galleryLock.unlock();
}
void br::GalleryToolBar::_checkWebcam()
{
static QSharedPointer<cv::VideoCapture> videoCapture;
if (videoCapture.isNull()) {
videoCapture = QSharedPointer<cv::VideoCapture>(new cv::VideoCapture(0));
cv::Mat m;
while (!m.data) videoCapture->read(m); // First frames can be empty
}
if (galleryLock.tryLock()) {
cv::Mat m;
videoCapture->read(m);
galleryLock.unlock();
enroll(toQImage(m));
}
}
/*** PRIVATE SLOTS ***/
void br::GalleryToolBar::checkWebcam()
{
// Check webcam
if (!tbWebcam.isChecked()) return;
QtConcurrent::run(this, &GalleryToolBar::_checkWebcam);
}
void br::GalleryToolBar::enrollmentFinished()
{
if (files.isEmpty()) {
if (!input.getBool("forceEnrollment") && !tbWebcam.isChecked()) {
QMessageBox msgBox;
msgBox.setText("Quality test failed.");
msgBox.setInformativeText("Enroll anyway?");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
if (ret == QMessageBox::Ok) {
br::File file = input;
file.setBool("forceEnrollment");
enroll(file);
}
}
return;
}
lGallery.setText(input.baseName() + (files.size() != 1 ? " (" + QString::number(files.size()) + ")" : ""));
tbBack.setEnabled(false);
emit newFiles(files);
emit newGallery(gallery);
}
void br::GalleryToolBar::home()
{
tbBack.setEnabled(false);
emit newGallery(gallery);
}
void br::GalleryToolBar::mean()
{
const QString file = QString("%1/mean/%2.png").arg(br_scratch_path(), input.baseName()+input.hash());
br_set_property("CENTER_TRAIN_B", qPrintable(file));
br::File trainingFile = input;
trainingFile.setBool("forceEnrollment");
br_train(qPrintable(trainingFile.flat()), "[algorithm=MedianFace]");
enroll(file);
}
void br::GalleryToolBar::openFile()
{
enroll(QFileDialog::getOpenFileName(this, "Select Photo", QDesktopServices::storageLocation(QDesktopServices::PicturesLocation)));
}
void br::GalleryToolBar::openFolder()
{
enroll(QFileDialog::getExistingDirectory(this, "Select Photo Directory", QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
}
#include "moc_gallerytoolbar.cpp"