forked from ahmeddemaad/Face_Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
174 lines (136 loc) · 5.34 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>
#include <QImage>
#include <QDebug>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <facedetection.h>
#include<facerecognizer.h>
#include<pca.h>
#include<ReadFile.h>
#include<writetraindata.h>
using namespace std;
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
QString imgPath;
void MainWindow::on_pushButton_clicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
void MainWindow::on_pushButton_3_clicked()
{
ui->stackedWidget->setCurrentIndex(1);
}
void plot_roc(std::vector<double> tpr, std::vector<double> fpr)
{
// Create a new window to display the ROC curve
namedWindow("ROC Curve", WINDOW_NORMAL);
resizeWindow("ROC Curve", 650, 650);
// Draw the ROC curve on a white background
Mat rocCurve(650, 650, CV_8UC3, Scalar(255, 255, 255));
// Draw the ROC curve
int x, y;
for (size_t i = 0; i < tpr.size(); i++) {
if (tpr[i] <= 0.5) {
x = static_cast<int>(tpr[i] * 550 + 10);
y = static_cast<int>((1 - fpr[i]) * 500 + 50); // Scale the y-coordinate between 0 and 1
circle(rocCurve, Point(x, y), 5, Scalar(0, 0, 255), FILLED);
}
else {
x = static_cast<int>(tpr[i] * 550 + 10);
y = static_cast<int>((1 - fpr[i]) * 500 + 50); // Scale the y-coordinate between 0 and 1
circle(rocCurve, Point(x, y), 5, Scalar(0, 0, 255), FILLED);
}
// Connect consecutive points with lines
if (i > 0) {
int prevX = static_cast<int>(tpr[i - 1] * 550 + 10);
int prevY = static_cast<int>((1 - fpr[i - 1]) * 500 + 50); // Scale the y-coordinate between 0 and 1
line(rocCurve, Point(prevX, prevY), Point(x, y), Scalar(255, 0, 0), 2, LINE_AA);
}
}
// Draw the axes
line(rocCurve, Point(10, 550), Point(600, 550), Scalar(0, 0, 0), 3, LINE_AA);
line(rocCurve, Point(10, 550), Point(10, 50), Scalar(0, 0, 0), 3, LINE_AA);
// Add labels to the axes
putText(rocCurve, "False Positive Rate", Point(215, 600), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0), 1, LINE_AA);
putText(rocCurve, "True Positive Rate", Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0), 1, LINE_AA);
// Draw the number "0" at position (5, 580)
putText(rocCurve, "0", Point(.5, 550), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 0), 2, LINE_AA);
putText(rocCurve, "1", Point(.5, 60), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 0), 2, LINE_AA);
// Display the ROC curve
imshow("ROC Curve", rocCurve);
waitKey(0);
}
void MainWindow::on_pushButton_2_clicked()
{
std::vector<double> tpr = { 0.0, 0.2, 0.4, 0.8, 1.0 };
std::vector<double> fpr = { 0.0, 0.6, 0.8, 0.95, 1.0 };
plot_roc(tpr, fpr);
}
void showImg(cv::Mat& img, QLabel* imgLbl, enum QImage::Format imgFormat, int width , int hieght, bool colorTransform)
{
if(colorTransform){
cvtColor(img, img, COLOR_BGR2RGB);
}
QImage image((uchar*)img.data, img.cols, img.rows, imgFormat);
QPixmap pix = QPixmap::fromImage(image);
imgLbl->setPixmap(pix.scaled(width, hieght, Qt::KeepAspectRatio));
}
Mat img;
void MainWindow::on_actionUpload_triggered()
{
ui->imginput1->clear();
ui->imgOutput1->clear();
imgPath = QFileDialog::getOpenFileName(this, "Open an Image", "..", "Images (*.png *.xpm *.jpg *.bmp)");
if(imgPath.isEmpty())
return;
img = imread(imgPath.toStdString());
cv::resize(img, img, Size(512, 512));
showImg(img, ui->imginput1, QImage::Format_RGB888, ui->imginput1->width(), ui->imginput1->height(),1);
showImg(img, ui->imginput2, QImage::Format_RGB888, ui->imginput1->width(), ui->imginput1->height(),0);
}
void MainWindow::on_submitBtn_clicked()
{
Mat output;
output=findFacesInImage(img.clone());
std::vector<double> tpr = { 0.0, 0.2, 0.4, 0.6, 0.8, 1.0 , 1.0};
std::vector<double> fpr = { 0.0, 0.1, 0.2, 0.3, 0.4, 0.5 , 1.0};
showImg(output, ui->imgOutput1, QImage::Format_RGB888, ui->imgOutput1->width(), ui->imgOutput1->height(),0);
}
void MainWindow::on_predictBtn_clicked()
{
vector<string> trainFacesPath;
vector<string> trainFacesID;
vector<string> loadedFacesID;
//read training list and ID from txt file
//read training data(faces, eigenvector, average face) from txt file
string trainListFilePath="D:/3rd Year 2st Term/Computer Vision/Task 5 Repository/Face_Recognition/list/train_list.txt";
readList(trainListFilePath, trainFacesPath, trainFacesID);
Mat avgVec, eigenVec, facesInEigen;
facesInEigen = readFaces(int(trainFacesID.size()), loadedFacesID);
avgVec = readMean();
eigenVec = readEigen(int(trainFacesID.size()));
string testImgPath;
Mat frame, processed, testImg;
// do PCA analysis for training faces
std::string TestPath = imgPath.toStdString();
cout<<TestPath;
FaceRecognizer faceRecognizer = FaceRecognizer(TestPath, avgVec, eigenVec, facesInEigen, loadedFacesID,3000);
cout<< faceRecognizer.getClosetFaceID()<< "*************";
// Show Result
QString closestID = QString::fromStdString(faceRecognizer.getClosetFaceID());
ui->prediction_label->setText(closestID);
}