-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·163 lines (144 loc) · 5.05 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
/*****************************************************************************
* Number Plate Recognition using SVM and Neural Networks
******************************************************************************
* by Ronnie Leon Ochieng, 26th March 2024
******************************************************************************/
// Main entry code OpenCV
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
#include <vector>
#include "DetectRegions.h"
#include "OCR.h"
#include "Plate.h"
using namespace std;
using namespace cv;
#define CV_TERMCRIT_ITER 1
string getFilename(string s) {
char sep = '/';
char sepExt='.';
// #ifdef _WIN32
// sep = '\\';
// #endif
size_t i = s.rfind(sep, s.length( ));
if (i != string::npos) {
string fn= (s.substr(i+1, s.length( ) - i));
size_t j = fn.rfind(sepExt, fn.length( ));
if (i != string::npos) {
return fn.substr(0,j);
}else{
return fn;
}
}else{
return "";
}
}
int main ( int argc, char** argv )
{
cout << "OpenCV Automatic Number Plate Recognition\n";
char* filename;
Mat input_image;
//Check if user specify image to process
if(argc >= 2 )
{
filename= argv[1];
//load image in gray level
input_image=imread(filename,1);
}else{
printf("Use:\n\t%s image\n",argv[0]);
return 0;
}
string filename_whithoutExt=getFilename(filename);
cout << "working with file: "<< filename_whithoutExt << "\n";
//Detect posibles plate regions
DetectRegions detectRegions;
detectRegions.setFilename(filename_whithoutExt);
detectRegions.saveRegions=false;
detectRegions.showSteps=false;
vector<Plate> posible_regions= detectRegions.run( input_image );
//SVM for each plate region to get valid car plates
//Read file storage.
FileStorage fs;
fs.open("SVM.xml", FileStorage::READ);
Mat SVM_TrainingData;
Mat SVM_Classes;
fs["TrainingData"] >> SVM_TrainingData;
fs["classes"] >> SVM_Classes;
//Set SVM params
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::LINEAR);
svm->setDegree(0);
svm->setGamma(1);
svm->setCoef0(0);
svm->setC(1);
svm->setNu(0);
svm->setP(0);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 1000, 0.01));
// Train the SVM
svm->train(SVM_TrainingData, cv::ml::ROW_SAMPLE, SVM_Classes);
// cv::ml::SVM::SvmParams SVM_params;
// SVM_params.svmType = cv::ml::SVM::C_SVC;
// SVM_params.kernelType = cv::ml::SVM::LINEAR; //CvSVM::LINEAR;
// SVM_params.degree = 0;
// SVM_params.gamma = 1;
// SVM_params.coef0 = 0;
// SVM_params.C = 1;
// SVM_params.nu = 0;
// SVM_params.p = 0;
// SVM_params.term_crit = cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 1000, 0.01);
//Train SVM
// CvSVM svmClassifier(SVM_TrainingData, SVM_Classes, Mat(), Mat(), SVM_params);
//For each possible plate, classify with svm if it's a plate or no
vector<Plate> plates;
for(int i=0; i< posible_regions.size(); i++)
{
Mat img=posible_regions[i].plateImg;
Mat p= img.reshape(1, 1);
p.convertTo(p, CV_32F); // Ensure the matrix is of type CV_32F
// Check if p has the correct number of features
if (p.cols != svm->getVarCount()) {
std::cerr << "Error: Number of features in p does not match the number of features the SVM was trained on." << std::endl;
} else {
int response = (int)svm->predict(p);
if(response == 1)
plates.push_back(posible_regions[i]);
}
}
cout << "Num plates detected: " << plates.size() << "\n";
//For each plate detected, recognize it with OCR
OCR ocr("OCR.xml");
ocr.saveSegments=true;
ocr.DEBUG=false;
ocr.filename=filename_whithoutExt;
for(int i=0; i< plates.size(); i++){
Plate plate=plates[i];
string plateNumber=ocr.run(&plate);
string licensePlate=plate.str();
cout << "================================================\n";
cout << "License plate number: "<< licensePlate << "\n";
cout << "================================================\n";
rectangle(input_image, plate.position, Scalar(0,0,200));
putText(input_image, licensePlate, Point(plate.position.x, plate.position.y), CV_FONT_HERSHEY_SIMPLEX, 1, Scalar(0,0,200),2);
if(false){
imshow("Plate Detected seg", plate.plateImg);
cvWaitKey(0);
}
}
imshow("Plate Detected", input_image);
for(;;)
{
int c;
c = cvWaitKey(10);
if( (char) c == 27)
break;
}
return 0;
}