-
Notifications
You must be signed in to change notification settings - Fork 2
/
liprec.cpp
164 lines (147 loc) · 5.01 KB
/
liprec.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
/***********************************************************************
This file is part of LiPRec, License Plate REcognition.
Copyright (C) 2012 Franco (nextime) Lanza <[email protected]>
LiPRec is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LiPRec 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with LiPRec. If not, see <http://www.gnu.org/licenses/>.
************************************************************************/
#include "liprec.h"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "optionparser.h"
using namespace liprec;
using namespace std;
using namespace cv;
enum optionIndex { OPT_UNKNOWN, OPT_HELP, OPT_DEBUG, OPT_GUI, OPT_PAUSE};
const option::Descriptor usage[] =
{
{OPT_UNKNOWN, 0,"", "" ,option::Arg::None, "USAGE: liprec [options] <video_file|image_file|video uri>\n\n"
"Options:" },
{OPT_HELP, 0,"h","help",option::Arg::None, " -h, --help \tPrint usage and exit." },
{OPT_DEBUG, 0,"d","debug",option::Arg::Optional, " -d[level], --debug[=level] \tSet debug level."},
{OPT_GUI, 0,"g","gui",option::Arg::None, " -g, --gui \tshow graphic UI." },
{OPT_PAUSE, 0,"p","",option::Arg::None, " -p \tpause video on plate detected\n"},
{OPT_UNKNOWN, 0,"", "" ,option::Arg::None, "\nExamples:\n"
" liprec -d file1.mjpeg\n"
" liprec http://<ip_addr>/img/video.h264\n"
" liprec -g file.jpg\n" },
{0,0,0,0,0,0}
};
int main(int argc, char* argv[])
{
int debug_level=0;
int use_gui=0;
int pause=0;
Mat frame;
LiPRec plateDetector;
argc-=(argc>0); argv+=(argc>0); // skip program name argv[0] if present
option::Stats stats(usage, argc, argv);
option::Option options[stats.options_max], buffer[stats.buffer_max];
option::Parser parse(usage, argc, argv, options, buffer);
if(parse.error())
{
cout << "Error parsing options\n";
return -1;
}
double imgnum=0;
#ifdef __DEBUG
debug_level=1;
#endif
#ifdef __SHOWIMAGES
debug_level=2;
#endif
//if( argc != 2) {
if(parse.nonOptionsCount()<1) {
cout << "You must specify a file to load\n\n";
option::printUsage(std::cout, usage);
return -1;
}
if (options[OPT_HELP]) {
option::printUsage(std::cout, usage);
return 0;
}
for (int i = 0; i < parse.optionsCount(); ++i) {
option::Option& opt = buffer[i];
switch(opt.index()) {
case OPT_HELP:
option::printUsage(std::cout, usage);
return 0;
case OPT_DEBUG:
debug_level=1;
if(opt.arg && atoi(opt.arg)<3)
{
debug_level=atoi(opt.arg);
if(debug_level > 0)
cout << "Debug level is " << debug_level << endl;
if(debug_level > 1)
use_gui=1;
}
break;
case OPT_GUI:
use_gui=1;
break;
case OPT_PAUSE:
pause=1;
break;
}
}
//VideoCapture cap(argv[1]);
VideoCapture cap(parse.nonOption(0));
if(!cap.isOpened()) {
cout << "Cannot open file " << argv[1] << endl;
return -1;
}
if(use_gui) {
cv::namedWindow("LiPRec", 0);
}
for(;;) {
//#ifdef __DEBUG
if(debug_level) {
imgnum++;
cout << "Working in frame # " << imgnum << endl;
}
if(cap.grab())
cap.retrieve(frame);
else
{
cout << "Video is over\n";
cv::waitKey(0);
break;
}
PlatesImage plates;
//plateDetector.optimizeImage(frame, frame);
plateDetector.detectPlates(frame, &plates);
if(debug_level) {
cout << "Plates vector size: " << plates.plates.size() << endl;
}
if(use_gui) {
cv::imshow("LiPRec", plates.image);
}
if(plates.plates.size() > 0) {
for(unsigned int i=0;i<plates.plates.size();i++) {
cout << "** Plates found: " << plates.plates[i].platetxt;
cout << " (confidence:" << plates.plates[i].confidence << ")" << endl;
}
if(pause) {
if(use_gui) {
cv::waitKey();
}
else {
std::cout << "Press enter to continue: ";
std::cin >> pause;
}
}
}
if(debug_level>1 || use_gui) {
if(cv::waitKey(30) >= 0) break;
}
}
return 0;
}