-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
367 lines (281 loc) · 11 KB
/
functions.cpp
File metadata and controls
367 lines (281 loc) · 11 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* functions.cpp
*
* Created on: Oct 29, 2014
* Author: romba
*/
#include "functions.hpp"
vision::vision(){
create_directories("./classifiers");
bowTrainer = (new BOWKMeansTrainer(CLUSTERS));
descriptorMatcher = (new BruteForceMatcher<L2<float> >);
//Ptr<DescriptorMatcher> matcher(new FlannBasedMatcher);
SIFT sf;
sf.set("edgeThreshold",edgeThreshold);
featureDetector = (new SiftFeatureDetector(sf));
descriptorExtractor = (new SiftDescriptorExtractor(sf));
//cout << featureDetector->getDouble("edgeThreshold");
bowDescriptorExtractor = (new BOWImgDescriptorExtractor(descriptorExtractor,descriptorMatcher));
};
vision::~vision(){
};
//=================================================================================================
// Build the Visual Vocabulary
//=================================================================================================
int vision::buildVocabulary(){
Mat input , descriptor, features_unclustered;
vector<KeyPoint> keypoints;
for(multimap<string,Mat>::iterator it = training_set.begin();it!=training_set.end();it++){
input = (*it).second;
keypoints = getKeyPoints(input);
keypoints_vector.push_back(keypoints);
descriptor = getDescriptors(input,keypoints);
features_unclustered.push_back(descriptor);
//cout << imagefile->d_name << " ";
}
cout << "Total Descriptors : " << features_unclustered.rows << endl;
FileStorage fs(TRAINING_DESCRIPTORS_FILE.c_str(),FileStorage::WRITE);
fs << "training_descriptors" << features_unclustered;
fs.release();
cout << "Training Descriptors => " << "/training_descriptors.yml" << endl;
bowTrainer->add(features_unclustered);
cout << "Cluster BOW Features" << endl;
vocabulary = bowTrainer->cluster();
FileStorage fs1(VOCABULARY_FILE.c_str(),FileStorage::WRITE);
fs1 << "Vocabulary" << vocabulary;
fs1.release();
cout << "Vocabulary => " << "/vocabulary.yml" << endl;
//fs2.release();
return 0;
}
//=================================================================================================
// Train the SVMs
//=================================================================================================
int vision::trainSVM(){
Mat hist, image; //vocabulary -> moved to private global
vector<KeyPoint> keypoints;
if(initVocabulary()!=0) return -1;
if(keypoints_vector.size()==0){
cout << "Making Keypoints"<<endl;
for(multimap<string,Mat>::iterator it = training_set.begin();it!=training_set.end();it++){
Mat input = (*it).second;
keypoints = getKeyPoints(input);
keypoints_vector.push_back(keypoints);
}
}
bowDescriptorExtractor->setVocabulary(vocabulary);
map<string,Mat> classes_training_data;
classes_training_data.clear();
vector < vector <KeyPoint> >::iterator itr = keypoints_vector.begin();
for(multimap<string,Mat>::iterator it=training_set.begin();it!=training_set.end();it++){
bowDescriptorExtractor->compute((*it).second,(*itr),hist);
string _class = (*it).first;
if(classes_training_data.count(_class) == 0){
classes_training_data[_class].create(0,hist.cols,hist.type());
}
classes_training_data[_class].push_back(hist);
itr++;
//cout << classes_training_data[_class].rows << endl;
//cout << hist.cols << hist.type() << endl;
}
CvSVMParams svmparams;
svmparams.svm_type = CvSVM::C_SVC;
svmparams.kernel_type = CvSVM::LINEAR;
svmparams.degree = 0.0;
svmparams.gamma = 0.0;
svmparams.coef0 = 0.0;
svmparams.C = 1000;
svmparams.nu = 0;
svmparams.p = 0;
svmparams.term_crit = cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,10000, 0.000001);
svmparams.class_weights = NULL;
//sfor(map<string,Mat>::iterator it = classes_training_data.begin();it != classes_training_data.end();it++){
for(vector<string>::iterator it = classes.begin();it!=classes.end();it++){
//string class_ = (*it).first;
string class_ = (*it);
cout << "training.class : " << class_ << " .. " << endl;
Mat samples(0,hist.cols,hist.type());
Mat labels(0,1,CV_32S);
samples.push_back(classes_training_data[class_]);
Mat class_label = Mat::ones(classes_training_data[class_].rows,1,CV_32S);
labels.push_back(class_label);
//for(map<string,Mat>::iterator it1 = classes_training_data.begin();it1!=classes_training_data.end();++it1){
for(vector<string>::iterator it1=classes.begin();it1!=classes.end();it1++){
//string not_class = (*it1).first;
string not_class = (*it1);
if(not_class.compare(class_) == 0) continue;
samples.push_back(classes_training_data[not_class]);
class_label = Mat::zeros(classes_training_data[not_class].rows,1,CV_32S);
//class_label *= -1;
labels.push_back(class_label);
//cout << samples.rows << " " << labels.rows<< endl;
}
//cout << "going to train" << endl;
//Mat samples_32f;
//.convertTo(samples_32f,CV_32F);
//classes_classifiers[class_].train(samples_32f,labels,Mat(),Mat(),svmparams);
//classes_classifiers[class_].train(samples,labels,Mat(),Mat(),svmparams);
classes_classifiers[class_].train_auto(samples,labels,Mat(),Mat(),svmparams,10);
classes_classifiers[class_].save(String("./classifiers/"+ class_+ ".yml").c_str());
//cout << classes_classifiers.count(class_) << endl;
}
Mat testimage;
testimage = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
keypoints = getKeyPoints(testimage);
bowDescriptorExtractor->compute(testimage,keypoints,hist);
//cout << hist.cols << endl;
for(map<string,CvSVM>::iterator it=classes_classifiers.begin();it!=classes_classifiers.end();++it){
float res = (*it).second.predict(hist,true);
cout << "class: " << (*it).first << " --> " << res << endl;
}
return 0;
}
//=================================================================================================
// Predict for a Image
//=================================================================================================
int vision::testImage(Mat testimage){
Mat hist;
Vector<pair<string,float>> result;
if(initVocabulary()!=0) return -1;
bowDescriptorExtractor->setVocabulary(vocabulary);
// testimage = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
vector<KeyPoint> keypoints;
keypoints = getKeyPoints(testimage);
bowDescriptorExtractor->compute(testimage,keypoints,hist);
//cout << hist.cols << endl;
for(map<string,CvSVM>::iterator it=classes_classifiers.begin();it!=classes_classifiers.end();++it){
float res = (*it).second.predict(hist,true);
cout << "class: " << (*it).first << " --> " << res << endl;
}
return 0;
}
//=================================================================================================
// Helpers
//=================================================================================================
Mat vision::getDescriptors(Mat image,vector<KeyPoint> keypoints){
Mat descriptors; // descriptors of the current image
//descriptorExtractor = (new SiftDescriptorExtractor(0,5,0.4,10,1.6));
descriptorExtractor->compute(image,keypoints,descriptors); // extract
return descriptors; // return the descriptors for the input image
}
vector<KeyPoint> vision::getKeyPoints(Mat image){
vector<KeyPoint> keypoints; // SIFT keypoints of the current image
//featureDetector = (new SiftFeatureDetector()); // feature Detector
featureDetector->detect(image,keypoints);
return keypoints;
}
void vision::drawKeyPoints(Mat image, vector<KeyPoint> keypoints){
Mat outimage;
drawKeypoints(image,keypoints,outimage,Scalar(255,0,0));
imshow("Keypoints",outimage);
//waitKey(0);
}
void vision::showImage(Mat image){
imshow("Image",image);
//waitKey(0);
}
int vision::initVocabulary(){
FileStorage fs(VOCABULARY_FILE.c_str(),FileStorage::READ);
fs["Vocabulary"] >> vocabulary;
fs.release();
if(vocabulary.size >0)
return 0;
else
return -1;
}
int vision::initVocabulary(String filename){
FileStorage fs(filename.c_str(),FileStorage::READ);
fs["Vocabulary"] >> vocabulary;
fs.release();
if(vocabulary.size >0)
return 0;
else
return -1;
}
//=================================================================================================
// Load Training Image Set
//=================================================================================================
int vision::loadTrainingSet(){
num_of_samples = 0;
string class_;
string training_path = current_path().string() + "/images/";
path dirr(training_path);
//cout << dirr << endl;
for(recursive_directory_iterator end, dir(dirr);dir!=end;dir++){
if(dir.level()==0){
class_ = path(*dir).filename().string();
classes.push_back(class_);
//cout << class_ << endl;
}
else
if(dir.level()==1){
string filename = path(*dir).string();
cout << filename << endl;
pair<string,Mat> tmp(class_,imread(filename,CV_LOAD_IMAGE_GRAYSCALE));
training_set.insert(tmp);
num_of_samples++;
}
}
FileStorage fs1("trainingsetinfo.yml",FileStorage::WRITE);
fs1 << "num_of_samples" << num_of_samples;
fs1 << "classes" << classes;
num_of_classes = classes.size();
cout << "Total Number of Classes : " << num_of_classes << endl;
fs1 << "num_of_classes" << num_of_classes;
fs1.release();
return 0;
}
void vision::openCamera(VideoCapture cap){ // index - video device - 0,1,2... == video0, video1
SIFT sf;
sf.set("edgeThreshold",edgeThreshold);
featureDetector = (new SiftFeatureDetector(sf));
descriptorExtractor = (new SiftDescriptorExtractor(sf));
//cout << featureDetector->getDouble("edgeThreshold");
bowDescriptorExtractor = (new BOWImgDescriptorExtractor(descriptorExtractor,descriptorMatcher));
cout << "Camera Starting" << endl;
namedWindow("cap");
if(initVocabulary()!=0) return ;
while(char(waitKey(1)) != 'q') {
float dec = 1000;
string class_;
Mat frame, frame_g;
cap >> frame;
imshow("Image", frame);
cvtColor(frame, frame_g, CV_BGR2GRAY);
vector <KeyPoint> keypoints;
Mat hist;
keypoints = getKeyPoints(frame_g);
bowDescriptorExtractor->setVocabulary(vocabulary);
bowDescriptorExtractor->compute(frame_g,keypoints,hist);
cout << hist.cols<<endl;
if(hist.cols==0) continue;
for(map<string,CvSVM>::iterator it=classes_classifiers.begin();it!=classes_classifiers.end();++it){
float res = (*it).second.predict(hist,true);
cout << "class: " << (*it).first << " --> " << res << endl;
if(res<dec){
dec = res;
class_ = (*it).first;
}
}
Mat im = (training_set.find(class_))->second;
imshow("Detected",im);
}
destroyAllWindows();
}
//=================================================================================================
// Load Prebuilt SVMs
//=================================================================================================
int vision::initClassifiers(){
String cpath = current_path().string() + "/classifiers/";
path p(cpath);
classes_classifiers.clear();
for(recursive_directory_iterator end, file(p);file!=end;file++){
String class_ = path(*file).filename().string();
cout << "Loading.. " << class_ << endl;
class_ = class_.substr(0,class_.length()-4);
classes_classifiers[class_].load(path(*file).string().c_str());
}
return 0;
}
int vision::testImages(){
}