-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathminutiaechecker.cpp
executable file
·200 lines (171 loc) · 8.98 KB
/
minutiaechecker.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
#include "minutiaechecker.h"
MinutiaeChecker::MinutiaeChecker()
{
this->setObjectName("checker");
this->fileDeploy = "./core/config/Caffe/extraction_deploy.prototxt";
this->fileCaffeModel = "./core/config/Caffe/extraction.caffemodel";
this->fileImageMean = "./core/config/Caffe/extraction_imagemean.binaryproto";
this->fileLabelNames = "./core/config/Caffe/extraction_labels.txt";
this->caffeNetworkTh = new CaffeNetwork();
this->caffeNetworkTh->moveToThread(this->caffeNetworkTh);
this->caffeNetworkTh->start();
connect(this, SIGNAL(loadModelSignal(const QString, const QString, const QString, const QString)), this->caffeNetworkTh, SLOT(loadModel(const QString, const QString, const QString, const QString)));
//connect(this->caffeNetworkTh, SIGNAL(finished()), this, SLOT(finished()));
connect(this->caffeNetworkTh, SIGNAL(started()), this, SLOT(loadCaffeModel()));
}
MinutiaeChecker::~MinutiaeChecker()
{
if (this->caffeNetworkTh) this->caffeNetworkTh->exit();
}
void MinutiaeChecker::run()
{
exec();
}
void MinutiaeChecker::loadCaffeModel()
{
emit loadModelSignal(this->fileDeploy, this->fileCaffeModel, this->fileImageMean, this->fileLabelNames);
}
void MinutiaeChecker::predictOne(const QImage &fpQImage, const QPoint &xy, int blockSize, bool variableBlockSize)
{
if (this->caffeNetworkTh->getNetworkLoaded()) {
QVector<QPair<QString, float> > predictedData;
cv::Mat fpImage;
int borderSize = blockSize;
if (variableBlockSize) borderSize += VARBLOCKITER * VARBLOCKSTEP;
//make border to allow predict the endpoints
cv::copyMakeBorder(QMatConverter::QImage2Mat(fpQImage, CV_8UC1), fpImage, borderSize, borderSize, borderSize, borderSize, cv::BORDER_CONSTANT, cv::Scalar(255,255,255));
cv::Mat block = fpImage(cv::Rect(xy.x()+borderSize - blockSize/2, xy.y()+borderSize - blockSize/2, blockSize, blockSize));
std::vector<Prediction> prediction = this->caffeNetworkTh->classify(block);
if (variableBlockSize) {
QVector<std::vector<Prediction>> predictions;
for (int i = 0; i < VARBLOCKITER; i++) {
block = fpImage(cv::Rect(xy.x()+borderSize - (blockSize + (i+1)*VARBLOCKSTEP)/2, xy.y()+borderSize - (blockSize + (i+1)*VARBLOCKSTEP)/2, (blockSize + (i+1)*VARBLOCKSTEP), (blockSize + (i+1)*VARBLOCKSTEP)));
predictions.push_back(this->caffeNetworkTh->classify(block));
}
float bestBlock[3] = {0,0,0};
for (std::vector<Prediction> prediction : predictions) {
if (prediction[0].first[0] == 'E' || prediction[0].first[0] == 'e') {
bestBlock[0] += prediction[0].second;
if (prediction[1].first[0] == 'B' || prediction[1].first[0] == 'b') {
bestBlock[1] += prediction[1].second;
bestBlock[2] += prediction[2].second;
}
else {
bestBlock[2] += prediction[1].second;
bestBlock[1] += prediction[2].second;
}
}
else if (prediction[0].first[0] == 'B' || prediction[0].first[0] == 'b') {
bestBlock[1] += prediction[0].second;
if (prediction[1].first[0] == 'E' || prediction[1].first[0] == 'e') {
bestBlock[0] += prediction[1].second;
bestBlock[2] += prediction[2].second;
}
else {
bestBlock[2] += prediction[1].second;
bestBlock[0] += prediction[2].second;
}
}
else {
bestBlock[2] += prediction[0].second;
if (prediction[1].first[0] == 'E' || prediction[1].first[0] == 'e') {
bestBlock[0] += prediction[1].second;
bestBlock[1] += prediction[2].second;
}
else {
bestBlock[1] += prediction[1].second;
bestBlock[0] += prediction[2].second;
}
}
}
if (bestBlock[0] > bestBlock[1] && bestBlock[0] > bestBlock[2]) {
predictedData.push_back(qMakePair(QString("Ending"), bestBlock[0]/VARBLOCKITER));
if (bestBlock[1] > bestBlock[2]) {
predictedData.push_back(qMakePair(QString("Bifurcation"), bestBlock[1]/VARBLOCKITER));
predictedData.push_back(qMakePair(QString("Nothing"), bestBlock[2]/VARBLOCKITER));
}
else {
predictedData.push_back(qMakePair(QString("Nothing"), bestBlock[2]/VARBLOCKITER));
predictedData.push_back(qMakePair(QString("Bifurcation"), bestBlock[1]/VARBLOCKITER));
}
}
else if (bestBlock[1] > bestBlock[0] && bestBlock[1] > bestBlock[2]) {
predictedData.push_back(qMakePair(QString("Bifurcation"), bestBlock[1]/VARBLOCKITER));
if (bestBlock[0] > bestBlock[2]) {
predictedData.push_back(qMakePair(QString("Ending"), bestBlock[0]/VARBLOCKITER));
predictedData.push_back(qMakePair(QString("Nothing"), bestBlock[2]/VARBLOCKITER));
}
else {
predictedData.push_back(qMakePair(QString("Nothing"), bestBlock[2]/VARBLOCKITER));
predictedData.push_back(qMakePair(QString("Ending"), bestBlock[0]/VARBLOCKITER));
}
}
else {
predictedData.push_back(qMakePair(QString("Nothing"), bestBlock[2]/VARBLOCKITER));
if (bestBlock[0] > bestBlock[1]) {
predictedData.push_back(qMakePair(QString("Ending"), bestBlock[0]/VARBLOCKITER));
predictedData.push_back(qMakePair(QString("Bifurcation"), bestBlock[1]/VARBLOCKITER));
}
else {
predictedData.push_back(qMakePair(QString("Bifurcation"), bestBlock[1]/VARBLOCKITER));
predictedData.push_back(qMakePair(QString("Ending"), bestBlock[0]/VARBLOCKITER));
}
}
}
else for (int i = 0; i < prediction.size(); i++) predictedData.push_back(qMakePair(QString::fromStdString(prediction[i].first), prediction[i].second));
emit sendPredictedMinutiaSignal(xy, predictedData);
}
}
void MinutiaeChecker::predictHeatmap(const QImage &fpQImage, int blockSize, bool variableBlockSize)
{
/*if (*this->mask.cpuOnly) Caffe::set_mode(Caffe::CPU);
else */Caffe::set_mode(Caffe::GPU);
if (this->caffeNetworkTh->getNetworkLoaded()) {
cv::Mat fpImage;
cv::Mat heatMap(fpQImage.height(), fpQImage.width(), CV_32FC1);
cv::Mat block;
int borderSize = blockSize;
if (variableBlockSize) borderSize += VARBLOCKITER * VARBLOCKSTEP;
//make border to allow predict the endpoints
cv::copyMakeBorder(QMatConverter::QImage2Mat(fpQImage, CV_8UC1), fpImage, borderSize, borderSize, borderSize, borderSize, cv::BORDER_CONSTANT, cv::Scalar(255,255,255));
std::vector<cv::Mat> blocks;
std::vector<std::vector<Prediction>> predictions;
std::vector<Prediction> prediction;
for (int x = 0 + borderSize; x < fpQImage.width() + borderSize; x++) {
for (int y = 0 + borderSize; y < fpQImage.height() + borderSize; y++) {
block = fpImage(cv::Rect(x - blockSize/2, y - blockSize/2, blockSize, blockSize));
blocks.push_back(block);
}
//Use Batch
predictions = this->caffeNetworkTh->classifyBatch(blocks, 3);
blocks.clear();
int cnt = 0;
for (int y = 0 + borderSize; y < fpQImage.height() + borderSize; y++) {
prediction = predictions[cnt++];
if (prediction[0].first[0] == 'N' || prediction[0].first[0] == 'n') heatMap.at<float>(y-borderSize, x-borderSize) = prediction[0].second;
else if (prediction[0].first[0] == 'E' || prediction[0].first[0] == 'e') heatMap.at<float>(y-borderSize, x-borderSize) = prediction[0].second + 1;
else heatMap.at<float>(y-borderSize, x-borderSize) = prediction[0].second + 2;
}
}
emit heatmapReadySignal(Helper::Mat2QImage(heatMap, QImage::Format_Grayscale8));
}
else {
//Net not loaded
}
}
QString MinutiaeChecker::getImgInputPath() const
{
return imgInputPath;
}
void MinutiaeChecker::setImgInputPath(const QString &value)
{
imgInputPath = value;
}
QString MinutiaeChecker::getModelInputFile() const
{
return modelInputFile;
}
void MinutiaeChecker::setModelInputFile(const QString &value)
{
modelInputFile = value;
}