-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
120 lines (119 loc) · 2.13 KB
/
Copy pathtest.cpp
File metadata and controls
120 lines (119 loc) · 2.13 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
#include "src2/categorizer.h"
#include<stdio.h>
int main(int argc,char* argv[]) {
// Number of clusters for building BOW vocabulary from SURF features
int clusters = 100;
try{
string read=string(argv[1]);
Mat input=imread(read);
categorizer c(clusters,0);
c.read_vocab();
//cout<<c.categorize(input);
string category=c.categorize(input);
//cout<<category<<endl;
//stringstream ss; // Insert the string into a stream
vector<string> tokens,preds; // Create vector to hold our words
ifstream infile;
infile.open("prediction_values.txt");
for (string line; getline(infile, line);) {
String sl="";
//cout << line << endl;
int i,k=0,found=0;
for(i=0;i<line.length();i++)
{
//cout<<line.length()<<endl;
if (line[i]!=' ')
sl=sl+line[i];
else
{
//cout<<sl<<endl;
if(k==0 && sl==category)
{
found=1;
}
if(k==1 && found==1)
tokens.push_back(sl);
if(k==2 && found==1)
{
k=0;
preds.push_back(sl);
found=0;
}
sl="";
k++;
}
}
}
string pred_value;
infile.close();
vector<string> copy(preds);
sort(copy.begin(),copy.end());
ifstream myfile;
myfile.open ("prediction.txt");
getline(myfile,pred_value);
myfile.close();
int pos=0;
float min=99,val=0.0;
for(int i=0;i<copy.size();i++)
{
val=abs(atof(copy[i].c_str())-atof(pred_value.c_str()));
if(val<=min)
{
min=val;
pos = i;
}
}
string top3[3];
if(pos==0)
{
top3[0]=copy[pos];
top3[1]=copy[pos+1];
top3[2]=copy[pos+2];
}
else if(pos==copy.size()-1)
{
top3[0]=copy[pos];
top3[1]=copy[pos-1];
top3[2]=copy[pos-2];
}
else
{
min = abs(atof(copy[pos-1].c_str())-atof(pred_value.c_str()));
val = abs(atof(copy[pos+1].c_str())-atof(pred_value.c_str()));
top3[0]=copy[pos];
if(min<val)
{
top3[1]=copy[pos-1];
top3[2]=copy[pos+1];
}
else
{
top3[1]=copy[pos+1];
top3[2]=copy[pos-1];
}
}
string top3_images[3]="";
for(int i=0;i<preds.size();i++)
{
if(top3[0]==preds[i])
top3_images[0]=tokens[i];
if(top3[1]==preds[i])
top3_images[1]=tokens[i];
if(top3[2]==preds[i])
top3_images[2]=tokens[i];
}
for(int i=0;i<3;i++)
cout<<top3_images[i]<<endl;
}
catch(std::runtime_error& b)
{
cout<<"error- Runtime Error";
exit(0);
}
catch(cv::Exception& a)
{
cout<<"error- openCV Exception";
exit(0);
}
return 0;
}