-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (158 loc) · 4.32 KB
/
main.cpp
File metadata and controls
180 lines (158 loc) · 4.32 KB
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
//============================================================================
// Name : LPD.cpp
// Author : Tuan Hoang
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C, Ansi-style
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <iomanip> // std::setfill, std::setw
#include "src/LPR.hpp"
#include "src/LetterClassifier.hpp"
#include "src/HelpFnc.hpp"
#include "src/timer.hpp"
#include "src/cvwin.hpp"
// Open CV
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
// Intel TBB
#include "tbb/tbb.h"
std::string letter_classifier_param_file;
std::string licensePlate_detection_config_file;
std::string video_file;
int SKIP_FRAME = 0;
bool PRINT_TIME = false;
int WAIT_TIME = 0;
int START_FRAME = 0;
int DEBUG_LEVEL = std::numeric_limits<int>::max();
inline void Help()
{
std::cout << "Usage: ./main " << std::endl;
std::cout << " -detection <license_plate_detection_config_file> " << std::endl;
std::cout << " -video <video_file> " << std::endl;
std::cout << " -skip <num_of_SKIP_FRAME> " << std::endl;
std::cout << " -time " << std::endl;
std::cout << " -debug <DEBUG_LEVEL> " << std::endl;
std::cout << " -wait <WAIT_TIME> " << std::endl;
std::cout << " -start <START_FRAME> " << std::endl;
}
inline void ArgumentParser(int argc, char** argv)
{
for( int i = 1; i < argc; i++ )
{
if( strcmp(argv[i],"-detection") == 0 )
{
i++;
licensePlate_detection_config_file = argv[i];
}
else if( strcmp(argv[i],"-video") == 0 )
{
i++;
video_file = argv[i];
}
else if( strcmp(argv[i],"-skip") == 0 )
{
i++;
SKIP_FRAME = std::atoi(argv[i]);
}
else if( strcmp(argv[i],"-time") == 0 )
{
PRINT_TIME = true;
}
else if( strcmp(argv[i],"-debug") == 0 )
{
i++;
DEBUG_LEVEL = std::atoi(argv[i]);
}
else if( strcmp(argv[i],"-wait") == 0 )
{
i++;
WAIT_TIME = std::atoi(argv[i]);
}
else if( strcmp(argv[i],"-start") == 0 )
{
i++;
START_FRAME = std::atoi(argv[i]);
}
}
}
int main(int argc, char** argv) {
if (argc < 2)
{
Help();
return 1;
}
// cvwin org_frame("Original");
timer licen_timer("Detect LP: ");
timer digit_timer("Get digits: ");
timer class_timer("Classify: ");
DMESG("Parsing argument ... ");
ArgumentParser(argc, argv);
DMESG("Initializing LPR ... ");
LPR lpr(licensePlate_detection_config_file);
DMESG("Opening video ... ");
cv::VideoCapture cap(video_file);
if (!cap.isOpened()) // if not success, exit program
{
std::cerr << "ERROR: Cannot open the video file" << std::endl;
return false;
}
int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
std::cout << "Video size: " << GREEN_TEXT << width << " x " << height << NORMAL_TEXT << std::endl;
// int fps = cap.get(CV_CAP_PROP_FPS);
cap.set(CV_CAP_PROP_POS_FRAMES, START_FRAME);
cv::Rect crop(0, height*3/10, width, height/2);
cv::Mat frame, detect_area, result;
while (true)
{
for ( int s = 0; s <= SKIP_FRAME; s++)
{
if (!cap.read(frame)) //if not success, break loop
{
std::cerr << "ERROR: Cannot read a frame from video stream" << std::endl;
return false;
}
}
// DMESG(YELLOW_TEXT << "----- NEW FRAME ----- " << NORMAL_TEXT);
detect_area = frame(crop);
// org_frame.display_frame(detect_area);
licen_timer.start();
lpr.DetectLP(detect_area);
licen_timer.stop();
if (PRINT_TIME)
licen_timer.printm();
digit_timer.start();
lpr.FindLicenseNumber(detect_area);
digit_timer.stop();
if (PRINT_TIME)
digit_timer.printm();
lpr.ShowLPs();
//
// for ( size_t i = 0; i < lpr.lp_candidates.size(); i++ )
// {
// SaveImage(lpr.lp_candidates[i].lp_img, "images/License_plates/lp_pos", lp_Cnt++);
// for ( size_t j = 0; j < lpr.lp_candidates[i].digit_boxes.size(); j++ )
// {
// cv::Mat digit = lpr.lp_candidates[i].lp_img(lpr.lp_candidates[i].digit_boxes[j]);
// SaveImage(digit, "images/Digits/digits_pos", digit_cnt++);
// }
// }
if (cv::waitKey(WAIT_TIME) == 27)
{
std::cout << RED_TEXT << "STOP!!! " << NORMAL_TEXT << std::endl;
break;
}
}
cap.release();
licen_timer.aprintm();
digit_timer.aprintm();
return EXIT_SUCCESS;
}