-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUse_Device_Camera
142 lines (119 loc) · 4.39 KB
/
Use_Device_Camera
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
/*****************************************************************************
* 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";
Mat input_image;
// Open the camera device
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "Failed to open the camera device" << endl;
return -1;
}
while (true) {
// Capture a frame from the camera
cap >> input_image;
// Check if the frame is empty
if (input_image.empty()) {
cout << "Failed to capture a frame" << endl;
break;
}
// Detect possible plate regions
DetectRegions detectRegions;
detectRegions.saveRegions = false;
detectRegions.showSteps = false;
vector<Plate> possible_regions = detectRegions.run(input_image);
// SVM for each plate region to get valid car plates
FileStorage fs;
fs.open("SVM.xml", FileStorage::READ);
Mat SVM_TrainingData;
Mat SVM_Classes;
fs["TrainingData"] >> SVM_TrainingData;
fs["classes"] >> SVM_Classes;
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));
svm->train(SVM_TrainingData, cv::ml::ROW_SAMPLE, SVM_Classes);
// For each possible plate, classify with SVM if it's a plate or not
vector<Plate> plates;
for (int i = 0; i < possible_regions.size(); i++) {
Mat img = possible_regions[i].plateImg;
Mat p = img.reshape(1, 1);
p.convertTo(p, CV_32F);
if (p.cols != svm->getVarCount()) {
cerr << "Error: Number of features in p does not match the number of features the SVM was trained on." << endl;
} else {
int response = (int)svm->predict(p);
if (response == 1)
plates.push_back(possible_regions[i]);
}
}
// For each plate detected, recognize it with OCR
OCR ocr("OCR.xml");
ocr.saveSegments = true;
ocr.DEBUG = false;
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);
}
// Display the processed frame
imshow("Automatic Number Plate Recognition", input_image);
// Break the loop if the 'Esc' key is pressed
if (waitKey(1) == 27)
break;
}
return 0;
}