Added DetectorCPU class and factory to instantiate it. Updated openalpr to use new class

This commit is contained in:
Matt Hill
2014-08-21 07:11:11 -04:00
parent 74bbdc0c5b
commit 740adabf0f
10 changed files with 224 additions and 122 deletions

View File

@@ -30,6 +30,7 @@
#include "endtoendtest.h"
#include "detection/detectorfactory.h"
#include "support/filesystem.h"
using namespace std;
@@ -146,7 +147,7 @@ int main( int argc, const char** argv )
else if (benchmarkName.compare("detection") == 0)
{
Config config(country);
RegionDetector plateDetector(&config);
Detector* plateDetector = createDetector(&config);
for (int i = 0; i< files.size(); i++)
{
@@ -155,12 +156,14 @@ int main( int argc, const char** argv )
string fullpath = inDir + "/" + files[i];
frame = imread( fullpath.c_str() );
vector<PlateRegion> regions = plateDetector.detect(frame);
vector<PlateRegion> regions = plateDetector->detect(frame);
imshow("Current LP", frame);
waitKey(5);
}
}
delete plateDetector;
}
else if (benchmarkName.compare("speed") == 0)
{
@@ -176,7 +179,7 @@ int main( int argc, const char** argv )
alpr.config->debugOff();
alpr.setDetectRegion(true);
RegionDetector plateDetector(&config);
Detector* plateDetector = createDetector(&config);
StateIdentifier stateIdentifier(&config);
OCR ocr(&config);
@@ -207,7 +210,7 @@ int main( int argc, const char** argv )
endToEndTimes.push_back(endToEndTime);
getTime(&startTime);
vector<PlateRegion> regions = plateDetector.detect(frame);
vector<PlateRegion> regions = plateDetector->detect(frame);
getTime(&endTime);
double regionDetectionTime = diffclock(startTime, endTime);

View File

@@ -24,7 +24,6 @@
#include <stdio.h>
#include <sys/stat.h>
#include "regiondetector.h"
#include "licenseplatecandidate.h"
#include "stateidentifier.h"
#include "utility.h"

View File

@@ -24,7 +24,6 @@
#include <stdio.h>
#include <sys/stat.h>
#include "regiondetector.h"
#include "licenseplatecandidate.h"
#include "stateidentifier.h"
#include "utility.h"

View File

@@ -6,7 +6,8 @@ set(lpr_source_files
alpr.cpp
alpr_impl.cpp
config.cpp
regiondetector.cpp
detection/detector.cpp
detection/detectorcpu.cpp
licenseplatecandidate.cpp
utility.cpp
stateidentifier.cpp

View File

@@ -37,7 +37,7 @@ AlprImpl::AlprImpl(const std::string country, const std::string configFile, cons
return;
}
plateDetector = new RegionDetector(config);
plateDetector = createDetector(config);
stateIdentifier = new StateIdentifier(config);
ocr = new OCR(config);
setNumThreads(0);

View File

@@ -27,7 +27,9 @@
#include "alpr.h"
#include "config.h"
#include "regiondetector.h"
#include "detection/detector.h"
#include "detection/detectorfactory.h"
#include "licenseplatecandidate.h"
#include "stateidentifier.h"
#include "segmentation/charactersegmenter.h"
@@ -86,7 +88,7 @@ class AlprImpl
private:
RegionDetector* plateDetector;
Detector* plateDetector;
StateIdentifier* stateIdentifier;
OCR* ocr;

View File

@@ -17,128 +17,46 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "regiondetector.h"
#include "detector.h"
using namespace cv;
using namespace std;
RegionDetector::RegionDetector(Config* config)
Detector::Detector(Config* config)
{
this->config = config;
this->scale_factor = 1.0f;
// Load either the regular or OpenCL version of the cascade classifier
this->plate_cascade = new CascadeClassifier();
if( this->plate_cascade->load( config->getCascadeRuntimeDir() + config->country + ".xml" ) )
{
this->loaded = true;
}
else
{
this->loaded = false;
printf("--(!)Error loading classifier\n");
}
}
RegionDetector::~RegionDetector()
Detector::~Detector()
{
delete this->plate_cascade;
}
bool RegionDetector::isLoaded()
bool Detector::isLoaded()
{
return this->loaded;
}
vector<PlateRegion> RegionDetector::detect(cv::Mat frame)
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> RegionDetector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
vector<PlateRegion> Detector::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;
// Must be implemented by subclass
std::vector<PlateRegion> rois;
return rois;
}
vector<PlateRegion> RegionDetector::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( int 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;
}
bool rectHasLargerArea(cv::Rect a, cv::Rect b) { return a.area() < b.area(); };
vector<PlateRegion> RegionDetector::aggregateRegions(vector<Rect> regions)
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.
@@ -185,25 +103,7 @@ vector<PlateRegion> RegionDetector::aggregateRegions(vector<Rect> regions)
}
}
/*
cv::Mat debugFrame = Mat::zeros( 700, 1300, CV_8UC3);
for (int i = 0; i < regions.size(); i++)
{
// Draw all level
cv::rectangle(debugFrame, regions[i], Scalar(0, 0, 255), 2);
}
for (int i = 0; i < topLevelRegions.size(); i++)
{
// Draw top level
cv::rectangle(debugFrame, topLevelRegions[i].rect, Scalar(255, 0, 0), 2);
}
drawAndWait(&debugFrame);
*/
return topLevelRegions;
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (c) 2013 New Designs Unlimited, LLC
* Opensource Automated License Plate Recognition [http://www.openalpr.com]
*
* This file is part of OpenAlpr.
*
* OpenAlpr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "detectorcpu.h"
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)
{
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( int 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

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2013 New Designs Unlimited, LLC
* Opensource Automated License Plate Recognition [http://www.openalpr.com]
*
* This file is part of OpenAlpr.
*
* OpenAlpr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPENALPR_DETECTORCPU_H
#define OPENALPR_DETECTORCPU_H
#include <stdio.h>
#include <iostream>
#include <vector>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"
#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;
std::vector<PlateRegion> doCascade(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
};
#endif /* OPENALPR_DETECTORCPU_H */

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2013 New Designs Unlimited, LLC
* Opensource Automated License Plate Recognition [http://www.openalpr.com]
*
* This file is part of OpenAlpr.
*
* OpenAlpr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPENALPR_DETECTORFACTORY_H
#define OPENALPR_DETECTORFACTORY_H
#include "detectorcpu.h"
Detector* createDetector(Config* config)
{
return new DetectorCPU(config);
}
#endif /* DETECTORFACTORY_H */