Added setCountry function to main API.

This allows the country to be changed without reloading the library and runtime_data
This commit is contained in:
Matt Hill
2016-02-14 15:58:04 -05:00
parent bc6e49b546
commit eb2954e49f
6 changed files with 83 additions and 31 deletions

View File

@@ -87,6 +87,10 @@ namespace alpr
return AlprImpl::fromJson(json); return AlprImpl::fromJson(json);
} }
void Alpr::setCountry(std::string country) {
impl->setCountry(country);
}
void Alpr::setDetectRegion(bool detectRegion) void Alpr::setDetectRegion(bool detectRegion)
{ {

View File

@@ -137,6 +137,9 @@ namespace alpr
Alpr(const std::string country, const std::string configFile = "", const std::string runtimeDir = ""); Alpr(const std::string country, const std::string configFile = "", const std::string runtimeDir = "");
virtual ~Alpr(); virtual ~Alpr();
// Set the country used for plate recognition
void setCountry(std::string country);
void setDetectRegion(bool detectRegion); void setDetectRegion(bool detectRegion);
void setTopN(int topN); void setTopN(int topN);
void setDefaultRegion(std::string region); void setDefaultRegion(std::string region);

View File

@@ -45,19 +45,7 @@ namespace alpr
return; return;
} }
for (unsigned int i = 0; i < config->loaded_countries.size(); i++) loadRecognizers();
{
config->setCountry(config->loaded_countries[i]);
AlprRecognizers recognizer;
recognizer.plateDetector = createDetector(config);
recognizer.ocr = new OCR(config);
recognizer.stateDetector = new StateDetector(this->config->country, this->config->config_file_path, this->config->runtimeBaseDir);
recognizers[config->country] = recognizer;
}
setNumThreads(0); setNumThreads(0);
@@ -666,6 +654,11 @@ namespace alpr
return allResults; return allResults;
} }
void AlprImpl::setCountry(std::string country) {
config->load_countries(country);
loadRecognizers();
}
void AlprImpl::setDetectRegion(bool detectRegion) void AlprImpl::setDetectRegion(bool detectRegion)
{ {
@@ -691,6 +684,28 @@ namespace alpr
return ss.str(); return ss.str();
} }
void AlprImpl::loadRecognizers() {
for (unsigned int i = 0; i < config->loaded_countries.size(); i++)
{
config->setCountry(config->loaded_countries[i]);
if (recognizers.find(config->country) == recognizers.end())
{
// Country training data has not already been loaded. Load it.
AlprRecognizers recognizer;
recognizer.plateDetector = createDetector(config);
recognizer.ocr = new OCR(config);
recognizer.stateDetector = new StateDetector(this->config->country, this->config->config_file_path, this->config->runtimeBaseDir);
recognizers[config->country] = recognizer;
}
}
}
cv::Mat AlprImpl::getCharacterTransformMatrix(PipelineData* pipeline_data ) { cv::Mat AlprImpl::getCharacterTransformMatrix(PipelineData* pipeline_data ) {
std::vector<Point2f> crop_corners; std::vector<Point2f> crop_corners;
crop_corners.push_back(Point2f(0,0)); crop_corners.push_back(Point2f(0,0));

View File

@@ -92,6 +92,7 @@ namespace alpr
AlprFullDetails analyzeSingleCountry(cv::Mat colorImg, cv::Mat grayImg, std::vector<cv::Rect> regionsOfInterest); AlprFullDetails analyzeSingleCountry(cv::Mat colorImg, cv::Mat grayImg, std::vector<cv::Rect> regionsOfInterest);
void setCountry(std::string country);
void setDetectRegion(bool detectRegion); void setDetectRegion(bool detectRegion);
void setTopN(int topn); void setTopN(int topn);
void setDefaultRegion(std::string region); void setDefaultRegion(std::string region);
@@ -116,6 +117,8 @@ namespace alpr
bool detectRegion; bool detectRegion;
std::string defaultRegion; std::string defaultRegion;
void loadRecognizers();
cv::Mat getCharacterTransformMatrix(PipelineData* pipeline_data ); cv::Mat getCharacterTransformMatrix(PipelineData* pipeline_data );
std::vector<AlprCoordinate> getCharacterPoints(cv::Rect char_rect, cv::Mat transmtx); std::vector<AlprCoordinate> getCharacterPoints(cv::Rect char_rect, cv::Mat transmtx);
std::vector<cv::Rect> convertRects(std::vector<AlprRegionOfInterest> regionsOfInterest); std::vector<cv::Rect> convertRects(std::vector<AlprRegionOfInterest> regionsOfInterest);

View File

@@ -111,36 +111,45 @@ namespace alpr
return; return;
} }
this->loaded_countries = this->parse_country_string(country); bool countries_loaded = load_countries(country);
if (this->loaded_countries.size() == 0)
{
std::cerr << "--(!) Country not specified." << endl;
return;
}
for (unsigned int i = 0; i < loaded_countries.size(); i++)
{
bool country_loaded = setCountry(this->loaded_countries[i]);
if (!country_loaded)
{
return;
}
}
setCountry(this->loaded_countries[0]);
if (this->debugGeneral) if (this->debugGeneral)
{ {
std::cout << debug_message << endl; std::cout << debug_message << endl;
} }
this->loaded = true; this->loaded = countries_loaded;
} }
Config::~Config() Config::~Config()
{ {
} }
bool Config::load_countries(const std::string countries) {
this->loaded_countries = this->parse_country_string(countries);
if (this->loaded_countries.size() == 0)
{
std::cerr << "--(!) Country not specified." << endl;
return false;
}
for (unsigned int i = 0; i < loaded_countries.size(); i++)
{
bool country_loaded = setCountry(this->loaded_countries[i]);
if (!country_loaded)
{
return false;
}
}
setCountry(this->loaded_countries[0]);
return true;
}
void Config::loadCommonValues(string configFile) void Config::loadCommonValues(string configFile)
{ {
@@ -344,10 +353,22 @@ namespace alpr
return parsed_countries; return parsed_countries;
} }
bool Config::country_is_loaded(std::string country) {
for (uint32_t i = 0; i < loaded_countries.size(); i++)
{
if (loaded_countries[i] == country)
return true;
}
return false;
}
bool Config::setCountry(std::string country) bool Config::setCountry(std::string country)
{ {
this->country = country; this->country = country;
std::string country_config_file = this->runtimeBaseDir + "/config/" + country + ".conf"; std::string country_config_file = this->runtimeBaseDir + "/config/" + country + ".conf";
if (fileExists(country_config_file.c_str()) == false) if (fileExists(country_config_file.c_str()) == false)
{ {
@@ -363,6 +384,9 @@ namespace alpr
return false; return false;
} }
if (!country_is_loaded(country))
this->loaded_countries.push_back(country);
return true; return true;
} }

View File

@@ -40,6 +40,8 @@ namespace alpr
Config(const std::string country, const std::string config_file = "", const std::string runtime_dir = ""); Config(const std::string country, const std::string config_file = "", const std::string runtime_dir = "");
virtual ~Config(); virtual ~Config();
bool load_countries(const std::string countries);
bool loaded; bool loaded;
std::string config_file_path; std::string config_file_path;
@@ -153,6 +155,7 @@ namespace alpr
float stateIdImagePercent; float stateIdImagePercent;
std::vector<std::string> parse_country_string(std::string countries); std::vector<std::string> parse_country_string(std::string countries);
bool country_is_loaded(std::string country);
void loadCommonValues(std::string configFile); void loadCommonValues(std::string configFile);
void loadCountryValues(std::string configFile, std::string country); void loadCountryValues(std::string configFile, std::string country);