-
Notifications
You must be signed in to change notification settings - Fork 0
/
svmclassification.cpp
181 lines (131 loc) · 4.88 KB
/
svmclassification.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
#include "svmclassification.hpp"
/***********************************/
//CONSTRUCTORS
SVMClassification::SVMClassification()
{
}
/***********************************/
//SETTERS
void SVMClassification::setNbBins(int inNbBins)
{
this->nbBins=inNbBins;
}
void SVMClassification::setPercentage(float inPercentage)
{
this->percentage=inPercentage;
}
void SVMClassification::setRootPath(string path)
{
this->rootPath = path;
}
/***********************************/
//GETTERS
/***********************************/
//FUNCTIONS
void SVMClassification::training()
{
histograms.release();//Erase arrays
labels.release();
accessData();//Extract the path and class of the dataset and store in a vector
/*********************************************/
//Training data
for (int i = 0; i <trainingData.size(); i++) {
image = imread(trainingData[i].first);//Extract image from file
label = trainingData[i].second;//Extract the label
cout << "image path (TRAIN) : " << trainingData[i].first << "\n";
cout << "class number (TRAIN) :" << label << "\n";
//Compute histogram
myHisto.setImage(image);
myHisto.setNbBins(nbBins);
myHisto.process();
histo=myHisto.getHisto();
histo = histo.t();//Transpose the histo
//Save the histogram
histograms.push_back(histo);//Save the histo in an array
labels.push_back(label);//Save the labels in an array
}
cout << "Number of data : "<< histograms.size[0]<< "\n";
cout << "Computation of the SV" << "\n";
trainSVM(histograms, labels);
}
void SVMClassification::process()//Test the SVM with test data set and save the result in a text file
{
/****************************************************/
//Test data
int estimatedLabel = 0;
stringstream ssResults;
int nbClass = 3;
nbLabel=0;
nbRightLabel=0;
nbLabelClass.clear();
nbRightLabelClass.clear();
for (int i = 0; i < nbClass; i++) {
nbLabelClass.push_back(0);
nbRightLabelClass.push_back(0);
}
for (int i = 0; i < testData.size(); i++) {
image = imread(testData[i].first);//Extract image from file
label = testData[i].second;//Extract the label
cout << "image path (TEST) : " << testData[i].first << "\n";
cout << "class number (TEST) :" << label << "\n";
//Compute histogram
myHisto.setImage(image);
myHisto.setNbBins(nbBins);
myHisto.process();
histo=myHisto.getHisto();
histo = histo.t();//Transpose histogram's values
//Compare the right value with the estimatedLabel
estimatedLabel = svm.predict(histo);
std::cout << "My prediction : " << estimatedLabel << "\n";
nbLabel+=1;
nbLabelClass[label]+=1;
if (estimatedLabel == label) {
nbRightLabel+=1;
nbRightLabelClass[label]+=1;
}
}
cout << "Number of data (TEST): "<< nbLabel << "\n";
cout << "Number of class found correctly :" << nbRightLabel << "\n";
cout << "Corrdior : " << nbLabelClass[0] << " - " << nbRightLabelClass[0] << "\n" ;
cout << "Office : " << nbLabelClass[1] << " - " << nbRightLabelClass[1] << "\n";
cout << "Toilet : " << nbLabelClass[2] << " - " << nbRightLabelClass[2] << "\n";
accuracy=((float)nbRightLabel/(float)nbLabel)*100;
cout << BLU "Accuracy : " << accuracy << "%\n" RESET;
ssResults << nbBins << "\t\t" << percentage << "\t\t\t" << nbLabel << "\t\t" << nbRightLabel << "\t\t" << accuracy << "\t\t" << nbLabelClass[0] << " - " << nbRightLabelClass[0] << "\t\t\t" << nbLabelClass[1] << " - " << nbRightLabelClass[1] << "\t\t\t" << nbLabelClass[2] << " - " << nbRightLabelClass[2] << '\n';
saveResults(name, ssResults.str());
}
void SVMClassification::accessData()//Access to the image in the folder and stock in a vector
{
Dataset myData;
myData.setRootPath(rootPath);
myData.createDataPaths(percentage);//1 = 100% of the training data
trainingData = myData.getTrainingPathsLabels();
testData = myData.getTestPathsLabels();
}
void SVMClassification::trainSVM(Mat histograms,Mat labels)
{
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::POLY;
params.term_crit = TermCriteria(CV_TERMCRIT_ITER, (int)10000, 1e-6);
params.p = 1;
params.degree=3;
svm.train(histograms,labels, Mat(), Mat(), params);
}
void SVMClassification::createFile(string inName)
{
name = inName;
//Creat a new vierge file
fstream file;
file.open(inName.c_str(),ios::out);
file << "Nb bins" << "\t\t" << "% of training data" << "\t" << "Nb Data" << "\t\t" << "Nb right" << '\t' << "Accuracy" << '\t' << "Corridor (Tot - Good)" << "\t" <<"Office (Tot - Good)" << "\t" << "Toilet (Tot - Good)" <<'\n';
file.close();
}
void SVMClassification::saveResults(string inName, string inData)
{
//Append data to a file
ofstream file;
file.open(inName.c_str(),ios::app);
file << inData;
file.close();
}