-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainUI.cpp
296 lines (259 loc) · 10.2 KB
/
MainUI.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
// Copyright 2014, University of Freiburg, Machine Learning Lab
// Authors: Daniel Leinfelder, Manuel Bühler, Christina Hernández Wunsch, Tobias Strickfaden
#include "MainUI.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QTextStream>
#include <QTextBrowser>
#include <QDateTime>
#include <QEventLoop>
#include <iostream>
#include "./Detection.h"
#include "./RecognitionOpenCV.h"
#include "./readWriteObjectFile.h"
#include "./FaceObject.h"
#include "benchmark.h"
#include "plotdialog.h"
#include "bbdialog.h"
using namespace std;
using namespace cv;
#define BENCHMARK_OVERLAP_THRESHOLD 0.5
QStringList benchmarkTargets; // list of targets for benchmarking
string sTimestamp; // timestamp with chosen method of the last detection/recognition run
struct PlotStruct
{
QVector<double> pr, rc;
QString name;
};
vector<PlotStruct> vPlots;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
connect(ui->pushDetect, SIGNAL(clicked()), this, SLOT(detect()));
connect(ui->pushRecognize, SIGNAL(clicked()), this, SLOT(recognize()));
connect(ui->pushOpenFolder, SIGNAL(clicked()), this, SLOT(openFolder()));
connect(ui->pushSaveLog, SIGNAL(clicked()), this, SLOT(saveLog()));
connect(ui->pushClearA, SIGNAL(clicked()), this, SLOT(clearLog()));
connect(ui->pushClearB, SIGNAL(clicked()), this, SLOT(clearRuns()));
connect(ui->pushLoadAllRuns, SIGNAL(clicked()), this, SLOT(loadAllRuns()));
connect(ui->pushLoadRun, SIGNAL(clicked()), this, SLOT(loadRun()));
connect(ui->pushCompareDetection, SIGNAL(clicked()), this, SLOT(compareDetection()));
connect(ui->pushCompareRecognition, SIGNAL(clicked()), this, SLOT(compareRecognition()));
connect(ui->pushShowPlot, SIGNAL(clicked()), this, SLOT(showPlot()));
connect(ui->pushShowBBs, SIGNAL(clicked()), this, SLOT(showBBs()));
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::detect() {
string sFileName, sFullPath;
vector<FaceObject> faceObjects;
// parse arguments
string sClassifier = ui->dropDownDetect->currentText().toUtf8().data();
string sPath = ui->inputPath->text().toUtf8().constData();
// create subfolder with a timestamp and chosen classifiern
QString path = ui->inputPath->text();
QDir dir = path;
QString fullPath = dir.absolutePath();
if (!QDir(path + "/metaface").exists()) {
QDir().mkdir("metaface");
}
QDateTime dateTime;
QString date = dateTime.currentDateTime().toString();
sTimestamp = dateTime.currentDateTime().toString().toUtf8().constData();
sTimestamp = sTimestamp + "_" + sClassifier;
QDir dirr = QDir::root();
QString timestamp = QString::fromStdString(sTimestamp);
dirr.mkpath(fullPath + "/metaface/" + timestamp +"/");
// iterate through image folder and run detection on each image
dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::NoSymLinks);
// add QDirIterator::Subdirectories as argument if you want to iterate trough subfolders
QDirIterator it(dir);
while(it.hasNext()) {
it.next();
sFileName = it.fileName().toUtf8().constData();
faceObjects = detectFaces(sPath, sFileName, sClassifier);
writeObjectFile(faceObjects, sPath + "/metaface/" + sTimestamp + "/" + sFileName + ".txt");
}
ui->outputText->append("Detection done!");
}
void MainWindow::recognize() {
// check if detection has been run
if (sTimestamp.empty()) {
ui->outputText->append("Need to run Detection first!");
return;
}
// parse arguments
string sClassifier = ui->dropDownRecognize->currentText().toUtf8().data();
string sPath = ui->inputPath->text().toUtf8().constData();
string sFullPath = sPath + "/metaface/" + sTimestamp;
int sizeTrainingSet = ui->spinBoxTrainingSet->value();
// iterate through the previous detected Faces
std::vector<std::vector<FaceObject> > faceObjects;
QString path = sFullPath.c_str();
QDir dir = path;
dir.setFilter(QDir::Files | QDir::NoDot | QDir::NoDotDot | QDir::NoSymLinks);
QDirIterator it(dir);
while(it.hasNext()) {
it.next();
vector<FaceObject> objects = readObjectFile(it.filePath().toUtf8().constData());
faceObjects.push_back(objects);
}
// edit faces for usage and save them to the faceObjects
int counter = 0;
Size size(100,100);
vector<Mat> trainingImages;
vector<int> trainingLabels;
for (size_t i = 0; i < faceObjects.size(); i++) {
for (size_t j = 0; j < faceObjects[i].size(); j++) {
string file = sPath + "/" + faceObjects[i][j].fileName;
Mat image = imread(file);
if (!image.empty()) {
// crop the face
Rect crop(faceObjects[i][j].x, faceObjects[i][j].y, faceObjects[i][j].width, faceObjects[i][j].height);
image = image(crop);
// convert to grayscale
cvtColor(image, image, COLOR_BGR2GRAY);
equalizeHist(image, image);
// resize to 100x100
cv::resize(image, image, size);
faceObjects[i][j].image = image;
// open dialog to tag the training set
if (counter < sizeTrainingSet) {
TagTrainingSetDialog tagDialog;
tagDialog.setImage(image);
tagDialog.exec();
int tag = tagDialog.getTag();
faceObjects[i][j].objectID = tag;
trainingImages.push_back(image);
trainingLabels.push_back(tag);
counter++;
}
}
}
}
// start the chosen recognition method
if (sClassifier == "Eigenfaces OpenCV") {
recognizeEigenfacesOpenCV(faceObjects, trainingImages, trainingLabels);
}
else if (sClassifier == "Fisherfaces OpenCV") {
recognizeFisherfacesOpenCV(faceObjects, trainingImages, trainingLabels);
}
else if (sClassifier == "LBP Histograms OpenCV") {
recognizeLBPHistogramsOpenCV(faceObjects, trainingImages, trainingLabels);
}
// get a new timestamp, create a subfolder in metaface/ and save the results
path = ui->inputPath->text();
dir = path;
QDateTime dateTime;
QString date = dateTime.currentDateTime().toString();
QDir dirr = QDir::root();
sTimestamp = dateTime.currentDateTime().toString().toUtf8().constData();
sTimestamp = sTimestamp + "_" + sClassifier;
QString timestamp = QString::fromStdString(sTimestamp);
path = dir.absolutePath() + "/metaface/" + timestamp;
cout << path.toUtf8().constData() << endl;
dirr.mkpath(path);
writeObjectFileVector(faceObjects, path.toUtf8().constData());
ui->outputText->append("Recognition Done!");
}
void MainWindow::openFolder() {
QString currentDir = ui->inputPath->text();
if(!QDir(currentDir).exists()) currentDir = "/home";
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
currentDir,
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
ui->inputPath->setText(dir);
}
void MainWindow::saveLog() {
QString name = QFileDialog::getSaveFileName(this, "Save file", "", ".txt");
QFile file(name + ".txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << ui->outputText->toPlainText();
file.close();
}
}
void MainWindow::clearLog() {
ui->outputText->clear();
}
void MainWindow::clearRuns() {
benchmarkTargets.clear();
ui->outputRuns->clear();
}
void MainWindow::loadAllRuns() {
QString path = ui->inputPath->text() + "/metaface/";
benchmarkTargets.clear();
ui->outputRuns->clear();
QDirIterator directories(path, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while(directories.hasNext()) {
directories.next();
QStringList tmp = directories.filePath().split("/");
//benchmarkTargets << tmp.value(tmp.length()-1);
benchmarkTargets << directories.filePath();
ui->outputRuns->append(tmp.value(tmp.length()-1));
}
}
void MainWindow::loadRun() {
QString currentDir = ui->inputPath->text();
if(!QDir(currentDir).exists()) currentDir = "/home";
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
currentDir,
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
QStringList tmp = dir.split("/");
//benchmarkTargets << tmp.value(tmp.length()-1);
benchmarkTargets << dir;
ui->outputRuns->append(tmp.value(tmp.length()-1));
benchmarkTargets.removeDuplicates();
sTimestamp = tmp.value(tmp.length()-1).toStdString();
}
void MainWindow::compareDetection() {
ui->outputText->append("begin compareDetection()");
QString gTruth = ui->inputPath->text() + "/.metaface";
vPlots.clear();
for (int i = 0; i < benchmarkTargets.size(); ++i) {
QString work = benchmarkTargets.at(i);
ui->outputText->append(work);
//benchmarkResult bRes = benchmark(work.toAscii().data(), gTruth.toAscii().data(), 0.5, false);
benchmarkResult bRes = benchmarkDetection(work.toAscii().data(), gTruth.toAscii().data(), BENCHMARK_OVERLAP_THRESHOLD);
PlotStruct plot_struct;
plot_struct.pr.push_back(bRes.precision);
plot_struct.rc.push_back(bRes.recall);
plot_struct.name = bRes.logFileName.c_str();
vPlots.push_back(plot_struct);
ui->outputText->append(bRes.toString().c_str());
}
ui->outputText->append("end compareDetection()");
}
void MainWindow::compareRecognition() {
ui->outputText->append("begin compareRecognition()");
QString gTruth = ui->inputPath->text() + "/.metaface";
for (int i = 0; i < benchmarkTargets.size(); ++i) {
QString work = benchmarkTargets.at(i);
ui->outputText->append(work);
benchmarkResult bRes = benchmarkRecognition(work.toAscii().data(), gTruth.toAscii().data(), BENCHMARK_OVERLAP_THRESHOLD);
ui->outputText->append(bRes.toString().c_str());
}
ui->outputText->append("end compareRecognition()");
}
void MainWindow::showPlot() {
PlotDialog plotDialog(this);
for(size_t i=0; i<vPlots.size(); i++)
{
plotDialog.addGraph(vPlots[i].pr, vPlots[i].rc, vPlots[i].name);
}
plotDialog.plotGraphs();
plotDialog.exec();
}
void MainWindow::showBBs()
{
BBdialog bbdialog(ui->inputPath->text(), benchmarkTargets, this);
bbdialog.exec();
}