-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.cpp
141 lines (125 loc) · 4.44 KB
/
main.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
/* Read a single facial image and recognize it.
Xiang Xiang (eglxiang@gmail.com), March 2016, MIT license.
*/
#define CPU_ONLY
//#define SIN_INPUT
#define CORR_METRIC
#include "Classifier.h"
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <math.h>
using namespace caffe;
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
#ifdef SIN_INPUT
// load image
if (argc != 4)
{
cerr<<"Run a simple test sample using pretrained VGG face model. " << endl
<< "Usage: " << argv[0]
<< "deploy.prototxt network.caffemodel image" << endl;
// downloaded from http://www.robots.ox.ac.uk/~vgg/software/vgg_face/src/vgg_face_caffe.tar.gz
return -1;
}
string model_file = argv[1];
string trained_file = argv[2];
Classifier classifier(model_file, trained_file);
string file = argv[3];
cout << "------------ Feature extraction for "<< file<< "------------" << endl;
Mat img = imread(file, -1);
if (! img.data) {
cout<<"Could not open or find the image"<<endl;
return -1;
}
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", img);
//int height = img.rows;
//int width = img.cols;
// subtract the average face
//Mat avg(height,width,CV_8UC3,Scalar(93.5940,104.7624,129.1863));
//Mat image = img - avg;
namedWindow("Display subtracted face", WINDOW_AUTOSIZE);
imshow("Display subtracted face", img);
// foward in the network
vector<float> output = classifier.Predict(image);
int count = 0;
/* cout<<"Prediction:"<<endl;
for (vector<float>::const_iterator i = output.begin(); i != output.end(); ++i) {
count = count + 1;
cout << *i << ' ';
}
cout << endl << count << endl;
cout<<"Prediction done!"<<endl;*/
cvWaitKey(0);
return 0;
#else
if (argc != 5) {
cerr << "Run a simple test sample using pre-trained VGG face model. " << endl
<< "Usage: " << argv[0]
<< "deploy.prototxt network.caffemodel imageA imageB" << endl;
return -1;
}
string model_file = argv[1];
string trained_file = argv[2];
Classifier classifier(model_file, trained_file);
string datapath = "/home/eglxiang/databases/FaceRecognition/YouTubeFaces/aligned_images_DB/";
string fileA = argv[3];
string fileApath = datapath + fileA;
string fileB = argv[4];
string fileBpath = datapath + fileB;
cerr << "--------------------- Feature extraction for "<<fileA<<"-----------"<<endl;
Mat imgA = imread(fileApath, -1);
if (! imgA.data) {
cout << "Could not open or find the image " << fileA << endl;
}
namedWindow("Display window 1", WINDOW_AUTOSIZE);
imshow("Display window 1", imgA);
int heightA = imgA.rows;
int widthA = imgA.cols;
Mat imgB = imread(fileBpath, -1);
if (! imgB.data) {
cout << "Could not open or find the image " << fileB << endl;
}
namedWindow("Display window 2", WINDOW_AUTOSIZE);
imshow("Display window 2", imgB);
int heightB = imgB.rows;
int widthB = imgB.cols;
// subtract the average face
Mat avgA(heightA,widthA,CV_8UC3,Scalar(93.5940,104.7624,129.1863));
Mat imageA = imgA - avgA;
Mat avgB(heightB,widthB,CV_8UC3,Scalar(93.5940,104.7624,129.1863));
Mat imageB = imgB - avgB;
// foward in the network
vector<float> outputA = classifier.Predict(imageA);
vector<float> outputB = classifier.Predict(imageB);
/*for (vector<float>::const_iterator i = outputA.begin(); i != outputA.end(); ++i) {
cout << *i << ' ';
}
cout << endl << endl << endl;
for (vector<float>::const_iterator j = outputB.begin(); j != outputB.end(); ++j) {
cout << *j << ' ';
}*/
#ifdef CORR_METRIC
// compute cosine similarity
float in_prod = 0;
for (int i=0; i<outputA.size(); i++)
in_prod += outputA[i]*outputB[i];
double sim = in_prod/(norm(outputA,NORM_L2)*norm(outputB,NORM_L2));
cout << endl << "Similarity: "<< sim*100.0 << "%"<<endl;
#else
float sub = 0;
float sub_norm_sq = 0;
for (int i=0; i<outputA.size(); i++)
sub = outputA[i] - outputB[i];
sub_norm_sq += sub*sub;
double sub_norm = sqrt(sub_norm_sq);
cout << endl << "Difference: "<< sub_norm*100.0 << "%"<< endl;
#endif // CORR_METRIC
cvWaitKey(0);
#endif // TWO_INPUT
}