Wrapped OpenALPR library in "alpr" namespace. Resolves issue #60.

This commit is contained in:
Matt Hill
2014-10-27 20:12:57 -04:00
parent 83ed86c6b4
commit 85f52a6b8c
79 changed files with 7234 additions and 6968 deletions

View File

@@ -22,88 +22,93 @@
using namespace cv;
using namespace std;
Detector::Detector(Config* config)
namespace alpr
{
this->config = config;
this->scale_factor = 1.0f;
}
Detector::~Detector()
{
}
bool Detector::isLoaded()
{
return this->loaded;
}
vector<PlateRegion> Detector::detect(cv::Mat frame)
{
std::vector<cv::Rect> regionsOfInterest;
regionsOfInterest.push_back(Rect(0, 0, frame.cols, frame.rows));
return this->detect(frame, regionsOfInterest);
}
vector<PlateRegion> Detector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
{
// Must be implemented by subclass
std::vector<PlateRegion> rois;
return rois;
}
bool rectHasLargerArea(cv::Rect a, cv::Rect b) { return a.area() < b.area(); };
vector<PlateRegion> Detector::aggregateRegions(vector<Rect> regions)
{
// Combines overlapping regions into a parent->child order.
// The largest regions will be parents, and they will have children if they are within them.
// This way, when processing regions later, we can process the parents first, and only delve into the children
// If there was no plate match. Otherwise, we would process everything and that would be wasteful.
vector<PlateRegion> orderedRegions;
vector<PlateRegion> topLevelRegions;
// Sort the list of rect regions smallest to largest
std::sort(regions.begin(), regions.end(), rectHasLargerArea);
// Create new PlateRegions and attach the rectangles to each
for (uint i = 0; i < regions.size(); i++)
Detector::Detector(Config* config)
{
PlateRegion newRegion;
newRegion.rect = regions[i];
orderedRegions.push_back(newRegion);
this->config = config;
this->scale_factor = 1.0f;
}
for (uint i = 0; i < orderedRegions.size(); i++)
Detector::~Detector()
{
bool foundParent = false;
for (uint k = i + 1; k < orderedRegions.size(); k++)
}
bool Detector::isLoaded()
{
return this->loaded;
}
vector<PlateRegion> Detector::detect(cv::Mat frame)
{
std::vector<cv::Rect> regionsOfInterest;
regionsOfInterest.push_back(Rect(0, 0, frame.cols, frame.rows));
return this->detect(frame, regionsOfInterest);
}
vector<PlateRegion> Detector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
{
// Must be implemented by subclass
std::vector<PlateRegion> rois;
return rois;
}
bool rectHasLargerArea(cv::Rect a, cv::Rect b) { return a.area() < b.area(); };
vector<PlateRegion> Detector::aggregateRegions(vector<Rect> regions)
{
// Combines overlapping regions into a parent->child order.
// The largest regions will be parents, and they will have children if they are within them.
// This way, when processing regions later, we can process the parents first, and only delve into the children
// If there was no plate match. Otherwise, we would process everything and that would be wasteful.
vector<PlateRegion> orderedRegions;
vector<PlateRegion> topLevelRegions;
// Sort the list of rect regions smallest to largest
std::sort(regions.begin(), regions.end(), rectHasLargerArea);
// Create new PlateRegions and attach the rectangles to each
for (uint i = 0; i < regions.size(); i++)
{
Point center( orderedRegions[i].rect.x + (orderedRegions[i].rect.width / 2),
orderedRegions[i].rect.y + (orderedRegions[i].rect.height / 2));
// Check if the center of the smaller rectangle is inside the bigger rectangle.
// If so, add it to the children and continue on.
if (orderedRegions[k].rect.contains(center))
PlateRegion newRegion;
newRegion.rect = regions[i];
orderedRegions.push_back(newRegion);
}
for (uint i = 0; i < orderedRegions.size(); i++)
{
bool foundParent = false;
for (uint k = i + 1; k < orderedRegions.size(); k++)
{
orderedRegions[k].children.push_back(orderedRegions[i]);
foundParent = true;
break;
Point center( orderedRegions[i].rect.x + (orderedRegions[i].rect.width / 2),
orderedRegions[i].rect.y + (orderedRegions[i].rect.height / 2));
// Check if the center of the smaller rectangle is inside the bigger rectangle.
// If so, add it to the children and continue on.
if (orderedRegions[k].rect.contains(center))
{
orderedRegions[k].children.push_back(orderedRegions[i]);
foundParent = true;
break;
}
}
if (foundParent == false)
{
// We didn't find any parents for this rectangle. Add it to the top level regions
topLevelRegions.push_back(orderedRegions[i]);
}
}
if (foundParent == false)
{
// We didn't find any parents for this rectangle. Add it to the top level regions
topLevelRegions.push_back(orderedRegions[i]);
}
return topLevelRegions;
}
return topLevelRegions;
}
}

View File

@@ -28,34 +28,39 @@
#include "support/timing.h"
#include "constants.h"
struct PlateRegion
{
cv::Rect rect;
std::vector<PlateRegion> children;
};
class Detector
namespace alpr
{
public:
Detector(Config* config);
virtual ~Detector();
bool isLoaded();
std::vector<PlateRegion> detect(cv::Mat frame);
virtual std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
protected:
Config* config;
bool loaded;
float scale_factor;
std::vector<PlateRegion> aggregateRegions(std::vector<cv::Rect> regions);
struct PlateRegion
{
cv::Rect rect;
std::vector<PlateRegion> children;
};
};
class Detector
{
public:
Detector(Config* config);
virtual ~Detector();
bool isLoaded();
std::vector<PlateRegion> detect(cv::Mat frame);
virtual std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
protected:
Config* config;
bool loaded;
float scale_factor;
std::vector<PlateRegion> aggregateRegions(std::vector<cv::Rect> regions);
};
}
#endif // OPENALPR_REGIONDETECTOR_H

View File

@@ -22,97 +22,102 @@
using namespace cv;
using namespace std;
DetectorCPU::DetectorCPU(Config* config) : Detector(config) {
if( this->plate_cascade.load( config->getCascadeRuntimeDir() + config->country + ".xml" ) )
{
this->loaded = true;
}
else
{
this->loaded = false;
printf("--(!)Error loading classifier\n");
}
}
DetectorCPU::~DetectorCPU() {
}
vector<PlateRegion> DetectorCPU::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
namespace alpr
{
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
vector<PlateRegion> detectedRegions = doCascade(frame_gray, regionsOfInterest);
DetectorCPU::DetectorCPU(Config* config) : Detector(config) {
return detectedRegions;
}
vector<PlateRegion> DetectorCPU::doCascade(Mat frame, std::vector<cv::Rect> regionsOfInterest)
{
if (frame.cols > config->maxDetectionInputWidth)
{
// The frame is too wide
this->scale_factor = ((float) config->maxDetectionInputWidth) / ((float) frame.cols);
if (config->debugGeneral)
std::cout << "Input detection image is too wide. Resizing with scale: " << this->scale_factor << endl;
}
else if (frame.rows > config->maxDetectionInputHeight)
{
// The frame is too tall
this->scale_factor = ((float) config->maxDetectionInputHeight) / ((float) frame.rows);
if (config->debugGeneral)
std::cout << "Input detection image is too tall. Resizing with scale: " << this->scale_factor << endl;
}
int w = frame.size().width;
int h = frame.size().height;
vector<Rect> plates;
equalizeHist( frame, frame );
resize(frame, frame, Size(w * this->scale_factor, h * this->scale_factor));
//-- Detect plates
timespec startTime;
getTime(&startTime);
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * this->scale_factor;
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * this->scale_factor;
Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor);
Size maxSize(maxWidth, maxHeight);
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
0,
//0|CV_HAAR_SCALE_IMAGE,
minSize, maxSize );
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
if( this->plate_cascade.load( config->getCascadeRuntimeDir() + config->country + ".xml" ) )
{
this->loaded = true;
}
else
{
this->loaded = false;
printf("--(!)Error loading classifier\n");
}
}
for( uint i = 0; i < plates.size(); i++ )
{
plates[i].x = plates[i].x / scale_factor;
plates[i].y = plates[i].y / scale_factor;
plates[i].width = plates[i].width / scale_factor;
plates[i].height = plates[i].height / scale_factor;
DetectorCPU::~DetectorCPU() {
}
vector<PlateRegion> orderedRegions = aggregateRegions(plates);
return orderedRegions;
vector<PlateRegion> DetectorCPU::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
{
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
vector<PlateRegion> detectedRegions = doCascade(frame_gray, regionsOfInterest);
return detectedRegions;
}
vector<PlateRegion> DetectorCPU::doCascade(Mat frame, std::vector<cv::Rect> regionsOfInterest)
{
if (frame.cols > config->maxDetectionInputWidth)
{
// The frame is too wide
this->scale_factor = ((float) config->maxDetectionInputWidth) / ((float) frame.cols);
if (config->debugGeneral)
std::cout << "Input detection image is too wide. Resizing with scale: " << this->scale_factor << endl;
}
else if (frame.rows > config->maxDetectionInputHeight)
{
// The frame is too tall
this->scale_factor = ((float) config->maxDetectionInputHeight) / ((float) frame.rows);
if (config->debugGeneral)
std::cout << "Input detection image is too tall. Resizing with scale: " << this->scale_factor << endl;
}
int w = frame.size().width;
int h = frame.size().height;
vector<Rect> plates;
equalizeHist( frame, frame );
resize(frame, frame, Size(w * this->scale_factor, h * this->scale_factor));
//-- Detect plates
timespec startTime;
getTime(&startTime);
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * this->scale_factor;
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * this->scale_factor;
Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor);
Size maxSize(maxWidth, maxHeight);
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
0,
//0|CV_HAAR_SCALE_IMAGE,
minSize, maxSize );
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
for( uint i = 0; i < plates.size(); i++ )
{
plates[i].x = plates[i].x / scale_factor;
plates[i].y = plates[i].y / scale_factor;
plates[i].width = plates[i].width / scale_factor;
plates[i].height = plates[i].height / scale_factor;
}
vector<PlateRegion> orderedRegions = aggregateRegions(plates);
return orderedRegions;
}
}

View File

@@ -31,19 +31,24 @@
#include "detector.h"
class DetectorCPU : public Detector {
public:
DetectorCPU(Config* config);
virtual ~DetectorCPU();
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
private:
cv::CascadeClassifier plate_cascade;
namespace alpr
{
std::vector<PlateRegion> doCascade(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
};
class DetectorCPU : public Detector {
public:
DetectorCPU(Config* config);
virtual ~DetectorCPU();
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
private:
cv::CascadeClassifier plate_cascade;
std::vector<PlateRegion> doCascade(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
};
}
#endif /* OPENALPR_DETECTORCPU_H */

View File

@@ -1,7 +1,11 @@
#include "detectorfactory.h"
Detector* createDetector(Config* config)
namespace alpr
{
return new DetectorCPU(config);
}
Detector* createDetector(Config* config)
{
return new DetectorCPU(config);
}
}

View File

@@ -23,7 +23,11 @@
#include "detectorcpu.h"
#include "config.h"
Detector* createDetector(Config* config);
namespace alpr
{
Detector* createDetector(Config* config);
}
#endif /* OPENALPR_DETECTORFACTORY_H */