diff --git a/runtime_data/openalpr.conf b/runtime_data/openalpr.conf index 094c907..dd9a616 100644 --- a/runtime_data/openalpr.conf +++ b/runtime_data/openalpr.conf @@ -7,6 +7,7 @@ state_id_img_size_percent = 2.0 max_plate_width_percent = 100 max_plate_height_percent = 100 +opencl_enabled = 0 ocr_min_font_point = 6 diff --git a/src/openalpr/alpr_impl.cpp b/src/openalpr/alpr_impl.cpp index 621a1ae..383d5fd 100644 --- a/src/openalpr/alpr_impl.cpp +++ b/src/openalpr/alpr_impl.cpp @@ -31,6 +31,34 @@ AlprImpl::AlprImpl(const std::string country, const std::string runtimeDir) this->topN = DEFAULT_TOPN; this->defaultRegion = ""; + if (config->opencl_enabled) + { + + cv::ocl::PlatformsInfo platinfo; + cv::ocl::getOpenCLPlatforms(platinfo); + + for (int i = 0; i < platinfo.size(); i++) + { + std::cout << platinfo[i]->platformName << std::endl; + } + + cv::ocl::DevicesInfo devices; + cv::ocl::getOpenCLDevices(devices, cv::ocl::CVCL_DEVICE_TYPE_CPU); + + for (int i = 0; i < devices.size(); i++) + std:: cout << devices[i]->deviceName << std::endl; + + if (devices.size() > 0) + { + cv::ocl::setDevice(devices[0]); + + cout << "Using OpenCL Device: " << devices[0]->deviceName << endl; + } + else + { + cout << "OpenCL initialization failed. Runtime drivers may not be installed." << endl; + } + } } AlprImpl::~AlprImpl() { diff --git a/src/openalpr/alpr_impl.h b/src/openalpr/alpr_impl.h index 31f1e4f..9960901 100644 --- a/src/openalpr/alpr_impl.h +++ b/src/openalpr/alpr_impl.h @@ -33,6 +33,8 @@ #include "cjson.h" #include +#include "opencv2/ocl/ocl.hpp" + #define DEFAULT_TOPN 25 diff --git a/src/openalpr/config.cpp b/src/openalpr/config.cpp index 50a1720..282719e 100644 --- a/src/openalpr/config.cpp +++ b/src/openalpr/config.cpp @@ -70,6 +70,8 @@ Config::~Config() void Config::loadValues(string country) { + + opencl_enabled = getBoolean("common", "opencl_enabled", false); maxPlateWidthPercent = getFloat("common", "max_plate_width_percent", 100); maxPlateHeightPercent = getFloat("common", "max_plate_height_percent", 100); diff --git a/src/openalpr/config.h b/src/openalpr/config.h index b18dc07..34a5229 100644 --- a/src/openalpr/config.h +++ b/src/openalpr/config.h @@ -43,6 +43,8 @@ class Config string country; + bool opencl_enabled; + float maxPlateWidthPercent; float maxPlateHeightPercent; diff --git a/src/openalpr/regiondetector.cpp b/src/openalpr/regiondetector.cpp index cd64969..9839da8 100644 --- a/src/openalpr/regiondetector.cpp +++ b/src/openalpr/regiondetector.cpp @@ -28,23 +28,34 @@ RegionDetector::RegionDetector(Config* config) // Don't scale. Can change this in the future (i.e., maximum resolution preference, or some such). this->scale_factor = 1.0f; + // Load either the regular or OpenCL version of the cascade classifier + if (config->opencl_enabled) + { + this->plate_cascade = new ocl::OclCascadeClassifier(); + } + else + { + this->plate_cascade = new CascadeClassifier(); + } - // Load the cascade classifier - if( this->plate_cascade.load( config->getCascadeRuntimeDir() + config->country + ".xml" ) ) - { + + if( this->plate_cascade->load( config->getCascadeRuntimeDir() + config->country + ".xml" ) ) + { this->loaded = true; } else { - printf("--(!)Error loading classifier\n"); this->loaded = false; + printf("--(!)Error loading classifier\n"); } + + } RegionDetector::~RegionDetector() { - + delete this->plate_cascade; } @@ -86,11 +97,19 @@ vector RegionDetector::doCascade(Mat frame) Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor); Size maxSize(w * config->maxPlateWidthPercent * this->scale_factor, h * config->maxPlateHeightPercent * this->scale_factor); - - plate_cascade.detectMultiScale( frame, plates, 1.1, 3, - 0, - //0|CV_HAAR_SCALE_IMAGE, - minSize, maxSize ); + if (config->opencl_enabled) + { + ocl::oclMat openclFrame(frame); + ((ocl::OclCascadeClassifier*) plate_cascade)->detectMultiScale(openclFrame, plates, 1.1, 3, 0, minSize, maxSize); + } + else + { + + plate_cascade->detectMultiScale( frame, plates, 1.1, 3, + 0, + //0|CV_HAAR_SCALE_IMAGE, + minSize, maxSize ); + } if (config->debugTiming) diff --git a/src/openalpr/regiondetector.h b/src/openalpr/regiondetector.h index 0d53a0d..a02a54a 100644 --- a/src/openalpr/regiondetector.h +++ b/src/openalpr/regiondetector.h @@ -30,7 +30,8 @@ #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/core/core.hpp" #include "opencv2/ml/ml.hpp" - #include "opencv2/highgui/highgui.hpp" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/ocl/ocl.hpp" #include "utility.h" #include "support/timing.h" @@ -54,8 +55,8 @@ class RegionDetector Config* config; float scale_factor; - CascadeClassifier plate_cascade; - CvSVM* svmClassifier; + CascadeClassifier* plate_cascade; + bool loaded; vector doCascade(Mat frame);