-
Notifications
You must be signed in to change notification settings - Fork 2
/
5_recognition.cpp
379 lines (234 loc) · 7.9 KB
/
5_recognition.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
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
368
369
370
371
372
373
374
375
376
377
378
379
// TrainNetwork.cpp : Defines the entry point for the console application.
#include <stdlib.h>
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <string.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
using namespace cv;
#define IMG_NEW_DIM 40
#define RESIZED_IMG_DIM 4800
#define NUM_OF_CLASSES 13
#define HIDDEN 300
/******************************************************************************/
#define DATA_SET_SIZE 8729 //Number of samples in dataset
#define ATTRIBUTES 388 //Number of pixels per sample.16X16
#define TRAINING_SAMPLES 6000 //Number of samples in test dataset
#define TEST_SAMPLES 2729 //Number of samples in test dataset
#define CLASSES 13 //Number of distinct labels.
#define RAW_DATA_SET_4800 "dataset4800.txt"
#define SHUFFLE_DATA_SET_FILE_4800 "dataSetShuffle4800.yml"
#define SHUFFLE_DATA_SET_4800 "dataSetShuffle4800"
#define PCA_FILE "pca480.yml"
#define NEURAL_NET_FILE "neuralNet.xml"
#define NEURAL_NET "neuralNet"
/* _______author_______
@author : TEUDJIO MBATIVOU Junior (Aspiring Data Scientist)
@mail : [email protected]
@linkedin : ma.linkedin.com/pub/junior-teudjio/8a/25b/3a1
*/
/* _______project tutor______
@tutor : ABDELHAK Ezzine ( Professor at ENSA Tanger)
@mail : [email protected]
*/
/* _______DataSet Citation_______
@Ref to the dataSet : http://benchmark.ini.rub.de/?section=gtsrb&subsection=dataset
J. Stallkamp, M. Schlipsing, J. Salmen, and C. Igel. The German Traffic Sign Recognition Benchmark: A multi-class classification competition.
In Proceedings of the IEEE International Joint Conference on Neural Networks, pages 1453–1460. 2011.
@inproceedings{Stallkamp-IJCNN-2011,
author = {Johannes Stallkamp and Marc Schlipsing and Jan Salmen and Christian Igel},
booktitle = {IEEE International Joint Conference on Neural Networks},
title = {The {G}erman {T}raffic {S}ign {R}ecognition {B}enchmark: A multi-class classification competition},
year = {2011},
pages = {1453--1460}
}
*/
/* ________code utility_______
this code is used to compute traffic sign images recognition
*/
// global variable to store de probabilities for each class on each sample
cv::Mat classificationResult(1, CLASSES, CV_64F);
// global variable for the raw dataset of size 4800=40*40*3
cv::Mat dataset4800(DATA_SET_SIZE,ATTRIBUTES,CV_64F);
void convert(cv::Mat &img,double pixelArray[])
{
int k=0;
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
img.at<Vec3b>(i, j)[0] = (int)pixelArray[k];
k++;
}
}
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
img.at<Vec3b>(i, j)[1] = (int)pixelArray[k];
k++;
}
}
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
img.at<Vec3b>(i, j)[2] = (int)pixelArray[k];
k++;
}
}
}
void convertToVect(cv::Mat &img, Mat & img_vect)
{
int k=0;
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
img_vect.at<double>(0,k) = img.at<Vec3b>(i, j)[0] ;
k++;
}
}
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
img_vect.at<double>(0,k) = img.at<Vec3b>(i, j)[1] ;
k++;
}
}
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
img_vect.at<double>(0,k) = img.at<Vec3b>(i, j)[2] ;
k++;
}
}
}
void MatShuffle(Mat & dataset)
{
vector< Mat > vv;
Mat res;
for( int i=0; i<dataset.rows; i++)
{
vv.push_back( dataset.row(i).clone());
}
random_shuffle(vv.begin(), vv.end());
for(int i=0; i<vv.size(); i++)
{
res.push_back( vv[i] ) ;
}
dataset = res.clone();
cout << " shuffle over " << endl;
}
void read_dataset(char *filename, cv::Mat &data, cv::Mat &labels, cv::Mat &labs)
{
Mat dataSet, classes;
FileStorage fs(filename,FileStorage::READ);
fs[SHUFFLE_DATA_SET_4800] >> dataSet ; fs.release();
// randomly shuffle the matrix rows
MatShuffle(dataSet);
data = dataSet( Range::all(),Range(0,RESIZED_IMG_DIM) ).clone();
labs = dataSet.col(RESIZED_IMG_DIM).clone();
classes= labs;
for(int i=0 ; i < classes.rows ; i++)
{
for( int j = 0; j < classes.cols ; j++)
{
switch( (int)classes.at<double>(i,j) )
{
case 13 : labels.at<double>(i,0) = 1; break;
case 14 : labels.at<double>(i,1) = 1; break;
case 15 : labels.at<double>(i,2) = 1; break;
case 17 : labels.at<double>(i,3) = 1; break;
case 19 : labels.at<double>(i,4) = 1; break;
case 20 : labels.at<double>(i,5) = 1; break;
case 21 : labels.at<double>(i,6) = 1; break;
case 27 : labels.at<double>(i,7) = 1; break;
case 33 : labels.at<double>(i,8) = 1; break;
case 34 : labels.at<double>(i,9) = 1; break;
case 35 : labels.at<double>(i,10) = 1; break;
case 36 : labels.at<double>(i,11) = 1; break;
case 37 : labels.at<double>(i,12) = 1; break;
default: break;
}
}
}
}
double predict(Mat & sample,CvANN_MLP& nnetwork)
{
nnetwork.predict(sample, classificationResult);
/*The classification result matrix holds weightage of each class.
we take the class with the highest weightage as the resultant class */
// find the class with maximum weightage.
int maxIndex = 0;
double value=0.0;
double maxValue=classificationResult.at<double>(0,0);
for(int index=1;index<CLASSES;index++)
{ value = classificationResult.at<double>(0,index);
if(value>maxValue)
{ maxValue = value;
maxIndex=index;
}
}
return maxIndex + 1;
}
int loadPCA(const string &file_name,cv::PCA& pca_)
{
FileStorage fs(file_name,FileStorage::READ);
fs["mean"] >> pca_.mean ;
fs["e_vectors"] >> pca_.eigenvectors ;
fs["e_values"] >> pca_.eigenvalues ;
fs.release();
}
/******************************************************************************/
int main( int argc, char** argv )
{
PCA pca;
loadPCA(PCA_FILE, pca);
string img_path;
cout << "path to road sign image to recognize : ";
cin >> img_path;
while(img_path.c_str())
{
// get the image to recognize
Mat test_img = imread(img_path);
imshow("img",test_img);
waitKey(0);
Mat test_img_vect(1,RESIZED_IMG_DIM,CV_64FC1) ;
// resizing img to the standard size of 40*40*3
Mat resizedImg(IMG_NEW_DIM,IMG_NEW_DIM,CV_8UC3) ;
resize(test_img , resizedImg , resizedImg.size() );
// convert the image 3D matrix to a vector
convertToVect( resizedImg, test_img_vect);
// apply PCA to that image to reduce it dimension ( 4800 ---> 388 )
Mat test_img_388 = pca.project(test_img_vect);
// define the structure for the neural network (MLP)
// The neural network has 3 layers.
// - one input node per attribute in a sample so 388 input nodes
// - 500 hidden nodes
// - 13 output node, one for each class.
cv::Mat layers(3,1,CV_32S);
layers.at<int>(0,0) = ATTRIBUTES;//input layer
layers.at<int>(1,0)= HIDDEN;//hidden layer
layers.at<int>(2,0) =CLASSES;//output layer
//create the neural network.
//for more details check http://docs.opencv.org/modules/ml/doc/neural_networks.html
CvANN_MLP nnetwork(layers, CvANN_MLP::SIGMOID_SYM,1,1);
// retrieve the neural network modeled previously
// nnetwork.load("param.xml", "DigitOCR");
nnetwork.load(NEURAL_NET_FILE, NEURAL_NET);
int pred;
pred = predict(test_img_388, nnetwork);
cout << " predicted value : " << pred<< endl << endl << endl;
cout << " path to a new image: " ;
cin >> img_path ;
}
return 0;
}