-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
370 lines (315 loc) · 10.5 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
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
/*
Automatic Number Plate Recognition (ANPR) utility
Copyright (C) 2009 Bob Mottram
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <sys/time.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <dirent.h>
#include <errno.h>
#include "anyoption.h"
#include "utils/Image.h"
#include "RawImage1.h"
#include "RawImage2.h"
#include "RawImage3.h"
#include "RawImage4.h"
#include "edgedetection/CannyEdgeDetector.h"
#include "cppunitlite/TestHarness.h"
#include "cppunitlite/TestResultStdErr.h"
#include "UnitTests.h"
#include "utils/bitmap.h"
#include "platedetection/ocr.h"
using namespace std;
/*!
* \brief returns true if the given file exists
* \param filename name of the file
*/
bool FileExists(char* filename)
{
bool flag = false;
fstream fin;
cout << "file exists: " << filename << endl;
fin.open(filename, ios::in);
if (fin.is_open())
flag = true;
fin.close();
return(flag);
}
/*!
* \brief returns true if the given file exists
* \param filename name of the file
*/
bool FileExists(std::string filename)
{
ifstream inf;
bool flag = false;
inf.open(filename.c_str());
if (inf.good()) flag = true;
inf.close();
return(flag);
}
/*!
* \brief returns a list of all filenames within a directory
* \param dir directory
* \param filenames list of filenames
*/
void GetFilesInDirectory(std::string dir, std::vector<std::string> &filenames)
{
filenames.clear();
DIR *dp;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) == NULL)
{
cout << "Error " << errno << " opening " << dir << endl;
return;
}
while ((dirp = readdir(dp)) != NULL)
{
std::string filename = dirp->d_name;
if (filename.size() > 3)
{
if ((filename.substr(filename.size()-3,3) == "bmp") ||
(filename.substr(filename.size()-3,3) == "BMP"))
{
//cout << "filename: " << filename << endl;
filenames.push_back(filename);
}
}
}
std::sort(filenames.begin(), filenames.end());
closedir(dp);
}
/*!
* \brief attempt to detect a number plate
* \param filename filename
* \param minimum_volume_percent minimum volume of the license plate as a percent of the image volume
* \param maximum_volume_percent maximum volume of the license plate as a percent of the image volume
* \param debug save extra debugging data
* \return license plate text
*/
std::string Detect(
std::string filename,
int minimum_volume_percent,
int maximum_volume_percent,
bool debug)
{
std::string license_plate_text = "";
if (FileExists(filename))
{
// create a bitmap object
Bitmap *bmp = new Bitmap();
if (bmp->FromFile(filename))
{
if ((bmp->Width < 10000) && (bmp->Width > 0) &&
(bmp->Height < 10000) && (bmp->Height > 0))
{
std::vector<unsigned char*> debug_images;
int debug_image_width = 0;
int debug_image_height = 0;
unsigned char* img_mono = bmp->Data;
if (bmp->bytes_per_pixel > 1)
{
// convert from colour to mono
img_mono = new unsigned char[bmp->Width * bmp->Height];
int n = (bmp->Width * bmp->Height) - 1;
for (int i = (bmp->Width * bmp->Height*3) - 3; i >= 0; i -= 3, n--)
img_mono[n] = (unsigned char)(((int)bmp->Data[i] + (int)bmp->Data[i + 1] + (int)bmp->Data[i + 2]) / 3);
}
// TODO: detect plate
license_plate_text = "";
// save the debug images
std::string debug_filename;
for (unsigned int i = 0; i < debug_images.size(); i++)
{
// make a file name
debug_filename = "";
std::stringstream s_debug_filename;
s_debug_filename << "debug_" << i << ".ppm";
s_debug_filename >> debug_filename;
// create a bitmap object
Bitmap *bmp_debug = new Bitmap(debug_images[i], debug_image_width, debug_image_height, 3);
// save the bitmap object
printf("Saving debug image %s\n", debug_filename.c_str());
bmp_debug->SavePPM(debug_filename.c_str());
delete bmp_debug;
}
if (license_plate_text != "")
{
cout << license_plate_text << endl;
}
if (img_mono != bmp->Data) delete[] img_mono;
delete bmp;
}
else
{
cout << "Error reading image file" << endl;
}
}
}
else
{
cout << "File not found" << endl;
}
return(license_plate_text);
}
/*!
* \brief run all unit tests
*/
void RunUnitTests()
{
TestResultStdErr result;
TestRegistry::runAllTests(result);
}
void Run( int argc, char* argv[], float VERSION )
{
AnyOption *opt = new AnyOption();
// help
opt->addUsage( "" );
opt->addUsage( "Usage: " );
opt->addUsage( "" );
opt->addUsage( " -h --help Prints this help " );
opt->addUsage( " -f --filename img1.bmp Image file to be analysed " );
opt->addUsage( " -d --dir Directory containing images to be analysed " );
opt->addUsage( " --minvol <value> Minimum volume of the license plate as a % of the image " );
opt->addUsage( " --maxvol <value> Maximum volume of the license plate as a % of the image " );
opt->addUsage( " --test Run unit tests " );
opt->addUsage( " --debug Save debugging info " );
opt->addUsage( " -c --chars Save characters " );
opt->addUsage( " -m --model <filename> Use the given character model " );
opt->addUsage( " -l --learn <directory> Learn character model " );
opt->addUsage( " -v --version Shows the version number " );
opt->addUsage( "" );
opt->setFlag( "help", 'h' ); // a flag (takes no argument), supporting long and short form
opt->setOption( "filename", 'f' ); // an option (takes an argument), filename to search
opt->setOption( "dir", 'd' ); // an option (takes an argument), directory to search
opt->setOption( "minvol" ); // minimum volume of the license plate as a percent of the image volume
opt->setOption( "maxvol" ); // maximum volume of the license plate as a percent of the image volume
opt->setFlag( "test", 't' ); // a flag (takes no argument) used to run unit tests
opt->setFlag( "debug" ); // a flag (takes no argument) used to save debugging images
opt->setFlag( "chars", 'c' );
opt->setOption( "model", 'm' );
opt->setOption( "learn", 'l' );
opt->setFlag( "version", 'v' );
opt->processCommandArgs(argc, argv);
if( ! opt->hasOptions())
{
// print usage if no options
opt->printUsage();
delete opt;
return;
}
if( opt->getFlag( "version" ) || opt->getFlag( 'v' ) )
{
printf("Version %f\n", VERSION);
return;
}
bool save_characters = false;
if( opt->getFlag( "chars" ) || opt->getFlag( 'c' ) )
{
save_characters = true;
}
if( opt->getFlag( "help" ) || opt->getFlag( 'h' ) )
opt->printUsage();
if( opt->getFlag( "test" ) || opt->getFlag( 't' ) )
RunUnitTests();
bool debug = false;
if( opt->getFlag( "debug" ) )
debug = true;
int minimum_volume_percent = 10;
if( opt->getValue( "minvol" ) != NULL )
{
minimum_volume_percent = atoi(opt->getValue("minvol"));
if (minimum_volume_percent < 5) minimum_volume_percent = 5;
}
int maximum_volume_percent = 100;
if( opt->getValue( "maxvol" ) != NULL )
{
maximum_volume_percent = atoi(opt->getValue("maxvol"));
if (maximum_volume_percent > 100) maximum_volume_percent = 100;
}
int model_image_width = 20;
int model_image_height = 20;
float* average_model = new float[model_image_width * model_image_height];
std::vector<float*> models;
if( opt->getValue( "model" ) != NULL || opt->getValue( 'm' ) != NULL )
{
std::string filename = opt->getValue("model");
ocr::LoadCharacterModels(filename, model_image_width, model_image_height, models, average_model);
}
if( opt->getValue( "learn" ) != NULL || opt->getValue( 'l' ) != NULL )
{
std::string directory = opt->getValue("learn");
std::vector<std::string> numbers;
ocr::CreateCharacterModels(directory, model_image_width, model_image_height, models);
if ((int)models.size() == 36)
{
ocr::CreateCharacterEigenModels(model_image_width,model_image_height,models, average_model);
ocr::SaveCharacterModels("model.dat", model_image_width, model_image_height, models, average_model);
cout << "Model saved: " << endl;
}
else
{
cout << "Incorrect number of models: " << (int)models.size() << endl;
}
}
if( opt->getValue( 'f' ) != NULL || opt->getValue( "filename" ) != NULL )
{
std::string filename = opt->getValue('f');
//cout << "filename: " << filename << endl;
std::vector<std::string> numbers;
std::vector<polygon2D*> plates;
int character_index = 0;
anpr::ReadFile(
filename,
plates,
numbers,
save_characters,
character_index,
model_image_width,
model_image_height,
models,
average_model,
"number_plates.ppm",
"filtered_image.ppm");
for (int i = 0; i < (int)plates.size(); i++)
{
delete plates[i];
plates[i] = NULL;
}
}
if( opt->getValue( "dir" ) != NULL || opt->getValue( 'd' ) != NULL )
{
std::string directory = opt->getValue("dir");
std::vector<std::string> numbers;
anpr::ReadDirectory(directory, numbers, save_characters, model_image_width, model_image_height, models, average_model);
}
for (int i = 0; i < (int)models.size(); i++)
{
delete[] models[i];
models[i] = NULL;
}
delete[] average_model;
delete opt;
}
int main( int argc, char* argv[] )
{
const float VERSION = 0.01f;
Run(argc, argv, VERSION);
// Uncomment this if you want to run valgrind on the unit tests
//RunUnitTests();
return EXIT_SUCCESS;
}