mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 11:56:52 +08:00
Cleanup & reindent .cpp files
This commit is contained in:
244
src/main.cpp
244
src/main.cpp
@@ -17,90 +17,83 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "support/filesystem.h"
|
||||
#include "support/timing.h"
|
||||
#include "alpr.h"
|
||||
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
const std::string MAIN_WINDOW_NAME = "ALPR main window";
|
||||
|
||||
const bool SAVE_LAST_VIDEO_STILL = false;
|
||||
const std::string LAST_VIDEO_STILL_LOCATION = "/tmp/laststill.jpg";
|
||||
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "support/filesystem.h"
|
||||
#include "support/timing.h"
|
||||
#include "alpr.h"
|
||||
/** Function Headers */
|
||||
bool detectandshow(Alpr* alpr, cv::Mat frame, std::string region, bool writeJson);
|
||||
|
||||
bool measureProcessingTime = false;
|
||||
|
||||
int main( int argc, const char** argv )
|
||||
{
|
||||
std::string filename;
|
||||
std::string runtimePath = "";
|
||||
bool outputJson = false;
|
||||
int seektoms = 0;
|
||||
bool detectRegion = false;
|
||||
std::string templateRegion;
|
||||
std::string country;
|
||||
int topn;
|
||||
|
||||
const std::string MAIN_WINDOW_NAME = "ALPR main window";
|
||||
try
|
||||
{
|
||||
|
||||
TCLAP::CmdLine cmd("OpenAlpr Command Line Utility", ' ', OPENALPR_VERSION);
|
||||
|
||||
const bool SAVE_LAST_VIDEO_STILL = false;
|
||||
const std::string LAST_VIDEO_STILL_LOCATION = "/tmp/laststill.jpg";
|
||||
TCLAP::UnlabeledValueArg<std::string> fileArg( "image_file", "Image containing license plates", true, "", "image_file_path" );
|
||||
|
||||
TCLAP::ValueArg<std::string> countryCodeArg("c","country","Country code to identify (either us for USA or eu for Europe). Default=us",false, "us" ,"country_code");
|
||||
TCLAP::ValueArg<int> seekToMsArg("","seek","Seek to the specied millisecond in a video file. Default=0",false, 0 ,"integer_ms");
|
||||
TCLAP::ValueArg<std::string> runtimeDirArg("r","runtime_dir","Path to the OpenAlpr runtime data directory",false, "" ,"runtime_dir");
|
||||
TCLAP::ValueArg<std::string> templateRegionArg("t","template_region","Attempt to match the plate number against a region template (e.g., md for Maryland, ca for California)",false, "" ,"region code");
|
||||
TCLAP::ValueArg<int> topNArg("n","topn","Max number of possible plate numbers to return. Default=10",false, 10 ,"topN");
|
||||
|
||||
/** Function Headers */
|
||||
bool detectandshow(Alpr* alpr, cv::Mat frame, std::string region, bool writeJson);
|
||||
TCLAP::SwitchArg jsonSwitch("j","json","Output recognition results in JSON format. Default=off", cmd, false);
|
||||
TCLAP::SwitchArg detectRegionSwitch("d","detect_region","Attempt to detect the region of the plate image. Default=off", cmd, false);
|
||||
TCLAP::SwitchArg clockSwitch("","clock","Measure/print the total time to process image and all plates. Default=off", cmd, false);
|
||||
|
||||
cmd.add( fileArg );
|
||||
cmd.add( countryCodeArg );
|
||||
cmd.add( seekToMsArg );
|
||||
cmd.add( topNArg );
|
||||
cmd.add( runtimeDirArg );
|
||||
cmd.add( templateRegionArg );
|
||||
|
||||
bool measureProcessingTime = false;
|
||||
cmd.parse( argc, argv );
|
||||
|
||||
int main( int argc, const char** argv )
|
||||
{
|
||||
std::string filename;
|
||||
std::string runtimePath = "";
|
||||
bool outputJson = false;
|
||||
int seektoms = 0;
|
||||
bool detectRegion = false;
|
||||
std::string templateRegion;
|
||||
std::string country;
|
||||
int topn;
|
||||
filename = fileArg.getValue();
|
||||
|
||||
try {
|
||||
country = countryCodeArg.getValue();
|
||||
seektoms = seekToMsArg.getValue();
|
||||
outputJson = jsonSwitch.getValue();
|
||||
runtimePath = runtimeDirArg.getValue();
|
||||
detectRegion = detectRegionSwitch.getValue();
|
||||
templateRegion = templateRegionArg.getValue();
|
||||
topn = topNArg.getValue();
|
||||
measureProcessingTime = clockSwitch.getValue();
|
||||
|
||||
TCLAP::CmdLine cmd("OpenAlpr Command Line Utility", ' ', OPENALPR_VERSION);
|
||||
|
||||
TCLAP::UnlabeledValueArg<std::string> fileArg( "image_file", "Image containing license plates", true, "", "image_file_path" );
|
||||
|
||||
TCLAP::ValueArg<std::string> countryCodeArg("c","country","Country code to identify (either us for USA or eu for Europe). Default=us",false, "us" ,"country_code");
|
||||
TCLAP::ValueArg<int> seekToMsArg("","seek","Seek to the specied millisecond in a video file. Default=0",false, 0 ,"integer_ms");
|
||||
TCLAP::ValueArg<std::string> runtimeDirArg("r","runtime_dir","Path to the OpenAlpr runtime data directory",false, "" ,"runtime_dir");
|
||||
TCLAP::ValueArg<std::string> templateRegionArg("t","template_region","Attempt to match the plate number against a region template (e.g., md for Maryland, ca for California)",false, "" ,"region code");
|
||||
TCLAP::ValueArg<int> topNArg("n","topn","Max number of possible plate numbers to return. Default=10",false, 10 ,"topN");
|
||||
|
||||
TCLAP::SwitchArg jsonSwitch("j","json","Output recognition results in JSON format. Default=off", cmd, false);
|
||||
TCLAP::SwitchArg detectRegionSwitch("d","detect_region","Attempt to detect the region of the plate image. Default=off", cmd, false);
|
||||
TCLAP::SwitchArg clockSwitch("","clock","Measure/print the total time to process image and all plates. Default=off", cmd, false);
|
||||
|
||||
cmd.add( fileArg );
|
||||
cmd.add( countryCodeArg );
|
||||
cmd.add( seekToMsArg );
|
||||
cmd.add( topNArg );
|
||||
cmd.add( runtimeDirArg );
|
||||
cmd.add( templateRegionArg );
|
||||
|
||||
cmd.parse( argc, argv );
|
||||
|
||||
filename = fileArg.getValue();
|
||||
|
||||
country = countryCodeArg.getValue();
|
||||
seektoms = seekToMsArg.getValue();
|
||||
outputJson = jsonSwitch.getValue();
|
||||
runtimePath = runtimeDirArg.getValue();
|
||||
detectRegion = detectRegionSwitch.getValue();
|
||||
templateRegion = templateRegionArg.getValue();
|
||||
topn = topNArg.getValue();
|
||||
measureProcessingTime = clockSwitch.getValue();
|
||||
|
||||
} catch (TCLAP::ArgException &e) // catch any exceptions
|
||||
{
|
||||
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
cv::Mat frame;
|
||||
}
|
||||
catch (TCLAP::ArgException &e) // catch any exceptions
|
||||
{
|
||||
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
cv::Mat frame;
|
||||
|
||||
Alpr alpr(country, runtimePath);
|
||||
alpr.setTopN(topn);
|
||||
@@ -125,15 +118,15 @@
|
||||
cv::VideoCapture cap(0);
|
||||
if (!cap.isOpened())
|
||||
{
|
||||
std::cout << "Error opening webcam" << std::endl;
|
||||
return 1;
|
||||
std::cout << "Error opening webcam" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (cap.read(frame) == true)
|
||||
{
|
||||
detectandshow(&alpr, frame, "", outputJson);
|
||||
cv::waitKey(1);
|
||||
framenum++;
|
||||
detectandshow(&alpr, frame, "", outputJson);
|
||||
cv::waitKey(1);
|
||||
framenum++;
|
||||
}
|
||||
}
|
||||
else if (hasEnding(filename, ".avi") || hasEnding(filename, ".mp4") || hasEnding(filename, ".webm") || hasEnding(filename, ".flv"))
|
||||
@@ -148,16 +141,16 @@
|
||||
|
||||
while (cap.read(frame) == true)
|
||||
{
|
||||
if (SAVE_LAST_VIDEO_STILL == true)
|
||||
{
|
||||
cv::imwrite(LAST_VIDEO_STILL_LOCATION, frame);
|
||||
}
|
||||
std::cout << "Frame: " << framenum << std::endl;
|
||||
if (SAVE_LAST_VIDEO_STILL == true)
|
||||
{
|
||||
cv::imwrite(LAST_VIDEO_STILL_LOCATION, frame);
|
||||
}
|
||||
std::cout << "Frame: " << framenum << std::endl;
|
||||
|
||||
detectandshow( &alpr, frame, "", outputJson);
|
||||
//create a 1ms delay
|
||||
cv::waitKey(1);
|
||||
framenum++;
|
||||
detectandshow( &alpr, frame, "", outputJson);
|
||||
//create a 1ms delay
|
||||
cv::waitKey(1);
|
||||
framenum++;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -179,32 +172,31 @@
|
||||
std::cerr << "Image file not found: " << filename << std::endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else if (DirectoryExists(filename.c_str()))
|
||||
{
|
||||
std::vector<std::string> files = getFilesInDir(filename.c_str());
|
||||
std::vector<std::string> files = getFilesInDir(filename.c_str());
|
||||
|
||||
std::sort( files.begin(), files.end(), stringCompare );
|
||||
std::sort( files.begin(), files.end(), stringCompare );
|
||||
|
||||
for (int i = 0; i< files.size(); i++)
|
||||
for (int i = 0; i< files.size(); i++)
|
||||
{
|
||||
if (hasEnding(files[i], ".jpg") || hasEnding(files[i], ".png"))
|
||||
{
|
||||
if (hasEnding(files[i], ".jpg") || hasEnding(files[i], ".png"))
|
||||
{
|
||||
std::string fullpath = filename + "/" + files[i];
|
||||
std::cout << fullpath << std::endl;
|
||||
frame = cv::imread( fullpath.c_str() );
|
||||
if (detectandshow( &alpr, frame, "", outputJson))
|
||||
{
|
||||
//while ((char) cv::waitKey(50) != 'c') { }
|
||||
}
|
||||
else
|
||||
{
|
||||
//cv::waitKey(50);
|
||||
}
|
||||
}
|
||||
|
||||
std::string fullpath = filename + "/" + files[i];
|
||||
std::cout << fullpath << std::endl;
|
||||
frame = cv::imread( fullpath.c_str() );
|
||||
if (detectandshow( &alpr, frame, "", outputJson))
|
||||
{
|
||||
//while ((char) cv::waitKey(50) != 'c') { }
|
||||
}
|
||||
else
|
||||
{
|
||||
//cv::waitKey(50);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -212,53 +204,45 @@
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool detectandshow( Alpr* alpr, cv::Mat frame, std::string region, bool writeJson)
|
||||
{
|
||||
|
||||
bool detectandshow( Alpr* alpr, cv::Mat frame, std::string region, bool writeJson)
|
||||
{
|
||||
std::vector<uchar> buffer;
|
||||
cv::imencode(".bmp", frame, buffer );
|
||||
|
||||
timespec startTime;
|
||||
getTime(&startTime);
|
||||
|
||||
std::vector<uchar> buffer;
|
||||
cv::imencode(".bmp", frame, buffer );
|
||||
std::vector<AlprResult> results = alpr->recognize(buffer);
|
||||
|
||||
|
||||
timespec startTime;
|
||||
getTime(&startTime);
|
||||
|
||||
|
||||
std::vector<AlprResult> results = alpr->recognize(buffer);
|
||||
|
||||
|
||||
if (writeJson)
|
||||
{
|
||||
if (writeJson)
|
||||
{
|
||||
std::cout << alpr->toJson(results) << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < results.size(); i++)
|
||||
{
|
||||
std::cout << "plate" << i << ": " << results[i].result_count << " results -- Processing Time = " << results[i].processing_time_ms << "ms." << std::endl;
|
||||
|
||||
for (int k = 0; k < results[i].topNPlates.size(); k++)
|
||||
{
|
||||
std::cout << " - " << results[i].topNPlates[k].characters << "\t confidence: " << results[i].topNPlates[k].overall_confidence << "\t template_match: " << results[i].topNPlates[k].matches_template << std::endl;
|
||||
std::cout << " - " << results[i].topNPlates[k].characters << "\t confidence: " << results[i].topNPlates[k].overall_confidence << "\t template_match: " << results[i].topNPlates[k].matches_template << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
timespec endTime;
|
||||
getTime(&endTime);
|
||||
if (measureProcessingTime)
|
||||
std::cout << "Total Time to process image: " << diffclock(startTime, endTime) << "ms." << std::endl;
|
||||
timespec endTime;
|
||||
getTime(&endTime);
|
||||
if (measureProcessingTime)
|
||||
std::cout << "Total Time to process image: " << diffclock(startTime, endTime) << "ms." << std::endl;
|
||||
|
||||
if (results.size() > 0)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
if (results.size() > 0)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user