-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
206 lines (158 loc) · 9.24 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
#include "main.h"
#include "Postprocess.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
bool blnKNNTrainingSuccessful = loadKNNDataAndTrainKNN(); // attempt KNN training
if (!blnKNNTrainingSuccessful) { // if KNN training was not successful
// show error message
std::cout << std::endl << std::endl << "error: error: KNN traning was not successful" << std::endl << std::endl;
return (0); // and exit program
}
if (argc < 3 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
displayHelp();
return (0);
}
if (strcmp(argv[1], "-p") == 0 || strcmp(argv[1], "--photo") == 0) {
recognizePhotos(argc, argv);
return (0);
}
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--video") == 0) {
recognizeVideos(argc, argv);
return (0);
}
return (0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void drawRedRectangleAroundPlate(cv::Mat &imgOriginalScene, PossiblePlate &licPlate) {
cv::Point2f p2fRectPoints[4];
licPlate.rrLocationOfPlateInScene.points(p2fRectPoints); // get 4 vertices of rotated rect
for (int i = 0; i < 4; i++) { // draw 4 red lines
if (licPlate.patternVerified) {
cv::line(imgOriginalScene, p2fRectPoints[i], p2fRectPoints[(i + 1) % 4], SCALAR_GREEN, 2);
} else {
cv::line(imgOriginalScene, p2fRectPoints[i], p2fRectPoints[(i + 1) % 4], SCALAR_RED, 2);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void writeLicensePlateCharsOnImage(cv::Mat &imgOriginalScene, PossiblePlate &licPlate) {
cv::Point ptCenterOfTextArea; // this will be the center of the area the text will be written to
cv::Point ptLowerLeftTextOrigin; // this will be the bottom left of the area that the text will be written to
int intFontFace = CV_FONT_HERSHEY_SIMPLEX; // choose a plain jane font
double dblFontScale = (double) licPlate.imgPlate.rows / 40.0; // base font scale on height of plate area
int intFontThickness = (int) std::round(dblFontScale * 1.5); // base font thickness on font scale
int intBaseline = 0;
cv::Size textSize = cv::getTextSize(licPlate.strChars, intFontFace, dblFontScale, intFontThickness,
&intBaseline); // call getTextSize
ptCenterOfTextArea.x = (int) licPlate.rrLocationOfPlateInScene.center.x; // the horizontal location of the text area is the same as the plate
if (licPlate.rrLocationOfPlateInScene.center.y <
(imgOriginalScene.rows * 0.75)) { // if the license plate is in the upper 3/4 of the image
// write the chars in below the plate
ptCenterOfTextArea.y = (int) std::round(licPlate.rrLocationOfPlateInScene.center.y) +
(int) std::round((double) licPlate.imgPlate.rows * 1.1);
} else { // else if the license plate is in the lower 1/4 of the image
// write the chars in above the plate
ptCenterOfTextArea.y = (int) std::round(licPlate.rrLocationOfPlateInScene.center.y) -
(int) std::round((double) licPlate.imgPlate.rows * 1.1);
}
ptLowerLeftTextOrigin.x = (int) (ptCenterOfTextArea.x - (textSize.width /
2)); // calculate the lower left origin of the text area
ptLowerLeftTextOrigin.y = (int) (ptCenterOfTextArea.y + (textSize.height /
2)); // based on the text area center, width, and height
// write the text on the image
if (licPlate.patternVerified) {
cv::putText(imgOriginalScene, licPlate.strChars, ptLowerLeftTextOrigin, intFontFace, dblFontScale, SCALAR_GREEN,
intFontThickness);
} else {
cv::putText(imgOriginalScene, licPlate.strChars, ptLowerLeftTextOrigin, intFontFace, dblFontScale,
SCALAR_YELLOW, intFontThickness);
}
}
void displayHelp() {
printf("USAGE:\n");
printf("\tpoo-proiect [-p <photo_file_path>] [-v <video_file_path>] [-h]\n");
printf("\n");
printf("\n");
printf("Where:\n");
printf("\n");
printf("\t-p <photo_file_path> OR --photo <photo_file_path>\n");
printf("\t\tA list of file path(s) for the photo(s) that we're going to process.\n");
printf("\n");
printf("\t-v <photo_file_path> OR --video <video_file_path>\n");
printf("\t\tFull file path for the video that we're going to process.\n");
printf("\n");
printf("\t-h OR --help\n");
printf("\t\tDisplays usage information and exits.\n");
}
void recognizeFrame(cv::Mat imgOriginalScene, std::string &name) {
if (imgOriginalScene.empty()) { // if unable to open image
std::cout << "error: image not read from file\n\n"; // show error message on command line
cv::waitKey(0); // may have to modify this line if not using Windows
return; // and exit program
}
std::vector<PossiblePlate> vectorOfPossiblePlates = detectPlatesInScene(imgOriginalScene); // detect plates
vectorOfPossiblePlates = detectCharsInPlates(
vectorOfPossiblePlates); // detect chars in plates
cv::imshow(name + "imgOriginalScene", imgOriginalScene); // show scene image
if (vectorOfPossiblePlates.empty()) { // if no plates were found
std::cout << std::endl << "no license plates were detected"
<< std::endl; // inform user no plates were found
} else { // else
// if we get in here vector of possible plates has at leat one plate
// sort the vector of possible plates in DESCENDING order (most number of chars to least number of chars)
std::sort(vectorOfPossiblePlates.begin(), vectorOfPossiblePlates.end(),
PossiblePlate::sortDescendingByNumberOfChars);
// suppose the plate with the most recognized chars (the first plate in sorted by string length descending order) is the actual plate
PossiblePlate licPlate = vectorOfPossiblePlates.front();
cv::imshow(name + "_imgPlate", licPlate.imgPlate); // show crop of plate and threshold of plate
cv::imshow(name + "_imgThresh", licPlate.imgThresh);
if (licPlate.strChars.length() ==
0) { // if no chars were found in the plate
std::cout << std::endl << "no characters were detected" << std::endl << std::endl; // show message
return; // and exit program
}
testRegex(licPlate);
drawRedRectangleAroundPlate(imgOriginalScene, licPlate); // draw red rectangle around plate
std::cout << std::endl << "license plate read from image = " << licPlate.strChars
<< std::endl; // write license plate text to std out
std::cout << std::endl << "-----------------------------------------" << std::endl;
writeLicensePlateCharsOnImage(imgOriginalScene, licPlate); // write license plate text on the image
cv::imshow(name + "_imgOriginalScene", imgOriginalScene); // re-show scene image
//cv::imwrite("imgOriginalScene.png", imgOriginalScene); // write image out to file
}
}
void recognizePhotos(int argc, char **argv) {
cv::Mat imgOriginalScene; // input image
std::string currentFrameName;
for (int i = 2; i < argc; ++i) {
currentFrameName = argv[i];
imgOriginalScene = cv::imread(currentFrameName); // open image
currentFrameName = currentFrameName.substr(currentFrameName.rfind('/') + 1); //parse file name
recognizeFrame(imgOriginalScene, currentFrameName);
cv::waitKey(0);
}
}
void recognizeVideos(int argc, char **argv) {
std::string currentFrameName;
for (int i = 2; i < argc; ++i) {
currentFrameName = argv[i];
cv::VideoCapture cap(argv[i]);
currentFrameName = currentFrameName.substr(currentFrameName.rfind('/') + 1); //parse file name
if (cap.isOpened() == 0) {
std::cout << "The video file cannot be opened." << std::endl;
continue;
}
int idx = 0;
cv::Mat frame;
do {
cap >> frame;
recognizeFrame(frame, currentFrameName);
cv::waitKey(1);
printf("Frame %d\n", idx);
idx++;
if (frame.empty()) {
break;
}
} while (true);
}
}