mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 11:26:56 +08:00
Reorganized the detection interface to simplify code and reduce boilerplate
This commit is contained in:
@@ -64,9 +64,80 @@ namespace alpr
|
|||||||
|
|
||||||
vector<PlateRegion> Detector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
vector<PlateRegion> Detector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
||||||
{
|
{
|
||||||
// Must be implemented by subclass
|
|
||||||
std::vector<PlateRegion> rois;
|
Mat frame_gray;
|
||||||
return rois;
|
|
||||||
|
if (frame.channels() > 2)
|
||||||
|
{
|
||||||
|
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
frame.copyTo(frame_gray);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the detection mask if it has been specified by the user
|
||||||
|
if (detector_mask.mask_loaded)
|
||||||
|
frame_gray = detector_mask.apply_mask(frame_gray);
|
||||||
|
|
||||||
|
vector<PlateRegion> detectedRegions;
|
||||||
|
for (int i = 0; i < regionsOfInterest.size(); i++)
|
||||||
|
{
|
||||||
|
Rect roi = regionsOfInterest[i];
|
||||||
|
|
||||||
|
// Adjust the ROI to be inside the detection mask (if it exists)
|
||||||
|
if (detector_mask.mask_loaded)
|
||||||
|
roi = detector_mask.getRoiInsideMask(roi);
|
||||||
|
|
||||||
|
// Sanity check. If roi width or height is less than minimum possible plate size,
|
||||||
|
// then skip it
|
||||||
|
if ((roi.width < config->minPlateSizeWidthPx) ||
|
||||||
|
(roi.height < config->minPlateSizeHeightPx))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Mat cropped = frame_gray(roi);
|
||||||
|
|
||||||
|
int w = cropped.size().width;
|
||||||
|
int h = cropped.size().height;
|
||||||
|
int offset_x = roi.x;
|
||||||
|
int offset_y = roi.y;
|
||||||
|
float scale_factor = computeScaleFactor(w, h);
|
||||||
|
|
||||||
|
if (scale_factor != 1.0)
|
||||||
|
resize(cropped, cropped, Size(w * scale_factor, h * scale_factor));
|
||||||
|
|
||||||
|
|
||||||
|
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor;
|
||||||
|
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor;
|
||||||
|
Size minPlateSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor);
|
||||||
|
Size maxPlateSize(maxWidth, maxHeight);
|
||||||
|
|
||||||
|
vector<Rect> allRegions = find_plates(cropped, minPlateSize, maxPlateSize);
|
||||||
|
|
||||||
|
// Aggregate the Rect regions into a hierarchical representation
|
||||||
|
for( unsigned int i = 0; i < allRegions.size(); i++ )
|
||||||
|
{
|
||||||
|
allRegions[i].x = (allRegions[i].x / scale_factor);
|
||||||
|
allRegions[i].y = (allRegions[i].y / scale_factor);
|
||||||
|
allRegions[i].width = allRegions[i].width / scale_factor;
|
||||||
|
allRegions[i].height = allRegions[i].height / scale_factor;
|
||||||
|
|
||||||
|
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
||||||
|
allRegions[i] = expandRect(allRegions[i], 0, 0, w, h);
|
||||||
|
|
||||||
|
allRegions[i].x = allRegions[i].x + offset_x;
|
||||||
|
allRegions[i].y = allRegions[i].y + offset_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<PlateRegion> orderedRegions = aggregateRegions(allRegions);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (int j = 0; j < orderedRegions.size(); j++)
|
||||||
|
detectedRegions.push_back(orderedRegions[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return detectedRegions;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Detector::get_detector_file() {
|
std::string Detector::get_detector_file() {
|
||||||
|
@@ -45,9 +45,9 @@ namespace alpr
|
|||||||
|
|
||||||
bool isLoaded();
|
bool isLoaded();
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame);
|
std::vector<PlateRegion> detect(cv::Mat frame);
|
||||||
virtual std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
||||||
|
|
||||||
//virtual std::vector<cv::Rect> find_plates(cv::Mat frame)=0;
|
virtual std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)=0;
|
||||||
|
|
||||||
void setMask(cv::Mat mask);
|
void setMask(cv::Mat mask);
|
||||||
|
|
||||||
|
@@ -45,77 +45,24 @@ namespace alpr
|
|||||||
DetectorCPU::~DetectorCPU() {
|
DetectorCPU::~DetectorCPU() {
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCPU::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
|
||||||
|
|
||||||
|
|
||||||
|
vector<Rect> DetectorCPU::find_plates(Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
{
|
{
|
||||||
|
|
||||||
Mat frame_gray;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply the detection mask if it has been specified by the user
|
|
||||||
if (detector_mask.mask_loaded)
|
|
||||||
frame_gray = detector_mask.apply_mask(frame_gray);
|
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++)
|
|
||||||
{
|
|
||||||
Rect roi = regionsOfInterest[i];
|
|
||||||
|
|
||||||
// Adjust the ROI to be inside the detection mask (if it exists)
|
|
||||||
if (detector_mask.mask_loaded)
|
|
||||||
roi = detector_mask.getRoiInsideMask(roi);
|
|
||||||
|
|
||||||
// Sanity check. If roi width or height is less than minimum possible plate size,
|
|
||||||
// then skip it
|
|
||||||
if ((roi.width < config->minPlateSizeWidthPx) ||
|
|
||||||
(roi.height < config->minPlateSizeHeightPx))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Mat cropped = frame_gray(roi);
|
|
||||||
vector<PlateRegion> subRegions = doCascade(cropped, roi.x, roi.y);
|
|
||||||
|
|
||||||
for (int j = 0; j < subRegions.size(); j++)
|
|
||||||
detectedRegions.push_back(subRegions[j]);
|
|
||||||
}
|
|
||||||
return detectedRegions;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCPU::doCascade(Mat frame, int offset_x, int offset_y)
|
|
||||||
{
|
|
||||||
|
|
||||||
int w = frame.size().width;
|
|
||||||
int h = frame.size().height;
|
|
||||||
|
|
||||||
float scale_factor = computeScaleFactor(w, h);
|
|
||||||
|
|
||||||
vector<Rect> plates;
|
vector<Rect> plates;
|
||||||
|
|
||||||
equalizeHist( frame, frame );
|
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(frame, frame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
//-- Detect plates
|
//-- Detect plates
|
||||||
timespec startTime;
|
timespec startTime;
|
||||||
getTimeMonotonic(&startTime);
|
getTimeMonotonic(&startTime);
|
||||||
|
|
||||||
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor;
|
equalizeHist( frame, frame );
|
||||||
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor;
|
|
||||||
Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor);
|
|
||||||
Size maxSize(maxWidth, maxHeight);
|
|
||||||
|
|
||||||
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||||
CV_HAAR_DO_CANNY_PRUNING,
|
CV_HAAR_DO_CANNY_PRUNING,
|
||||||
//0|CV_HAAR_SCALE_IMAGE,
|
//0|CV_HAAR_SCALE_IMAGE,
|
||||||
minSize, maxSize );
|
min_plate_size, max_plate_size );
|
||||||
|
|
||||||
|
|
||||||
if (config->debugTiming)
|
if (config->debugTiming)
|
||||||
@@ -125,23 +72,7 @@ namespace alpr
|
|||||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
return plates;
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
|
||||||
plates[i] = expandRect(plates[i], 0, 0, w, h);
|
|
||||||
|
|
||||||
plates[i].x = plates[i].x + offset_x;
|
|
||||||
plates[i].y = plates[i].y + offset_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> orderedRegions = aggregateRegions(plates);
|
|
||||||
|
|
||||||
return orderedRegions;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -39,13 +39,12 @@ namespace alpr
|
|||||||
DetectorCPU(Config* config, PreWarp* prewarp);
|
DetectorCPU(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorCPU();
|
virtual ~DetectorCPU();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
cv::CascadeClassifier plate_cascade;
|
cv::CascadeClassifier plate_cascade;
|
||||||
|
|
||||||
std::vector<PlateRegion> doCascade(cv::Mat frame, int offset_x, int offset_y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
#include "detectorcuda.h"
|
#include "detectorcuda.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef COMPILE_GPU
|
#ifdef COMPILE_GPU
|
||||||
|
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
@@ -49,60 +48,22 @@ namespace alpr
|
|||||||
DetectorCUDA::~DetectorCUDA() {
|
DetectorCUDA::~DetectorCUDA() {
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCUDA::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
vector<Rect> DetectorCUDA::find_plates(Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
{
|
{
|
||||||
|
//-- Detect plates
|
||||||
Mat frame_gray;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++)
|
|
||||||
{
|
|
||||||
Mat cropped = frame_gray(regionsOfInterest[i]);
|
|
||||||
vector<PlateRegion> subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y);
|
|
||||||
|
|
||||||
for (int j = 0; j < subRegions.size(); j++)
|
|
||||||
detectedRegions.push_back(subRegions[j]);
|
|
||||||
}
|
|
||||||
return detectedRegions;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCUDA::doCascade(Mat frame, int offset_x, int offset_y)
|
|
||||||
{
|
|
||||||
|
|
||||||
int w = frame.size().width;
|
|
||||||
int h = frame.size().height;
|
|
||||||
|
|
||||||
float scale_factor = computeScaleFactor(w, h);
|
|
||||||
|
|
||||||
vector<Rect> plates;
|
vector<Rect> plates;
|
||||||
|
|
||||||
equalizeHist( frame, frame );
|
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(frame, frame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
//-- Detect plates
|
|
||||||
timespec startTime;
|
timespec startTime;
|
||||||
getTimeMonotonic(&startTime);
|
getTimeMonotonic(&startTime);
|
||||||
|
|
||||||
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor;
|
|
||||||
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor;
|
|
||||||
Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor);
|
|
||||||
|
|
||||||
gpu::GpuMat cudaFrame, plateregions_buffer;
|
gpu::GpuMat cudaFrame, plateregions_buffer;
|
||||||
Mat plateregions_downloaded;
|
Mat plateregions_downloaded;
|
||||||
|
|
||||||
cudaFrame.upload(frame);
|
cudaFrame.upload(frame);
|
||||||
int numdetected = cuda_cascade.detectMultiScale(cudaFrame, plateregions_buffer, (double) config->detection_iteration_increase, config->detectionStrictness, minSize);
|
int numdetected = cuda_cascade.detectMultiScale(cudaFrame, plateregions_buffer,
|
||||||
|
(double) config->detection_iteration_increase, config->detectionStrictness,
|
||||||
|
min_plate_size);
|
||||||
|
|
||||||
plateregions_buffer.colRange(0, numdetected).download(plateregions_downloaded);
|
plateregions_buffer.colRange(0, numdetected).download(plateregions_downloaded);
|
||||||
|
|
||||||
for (int i = 0; i < numdetected; ++i)
|
for (int i = 0; i < numdetected; ++i)
|
||||||
@@ -110,8 +71,6 @@ namespace alpr
|
|||||||
plates.push_back(plateregions_downloaded.ptr<cv::Rect>()[i]);
|
plates.push_back(plateregions_downloaded.ptr<cv::Rect>()[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (config->debugTiming)
|
if (config->debugTiming)
|
||||||
{
|
{
|
||||||
timespec endTime;
|
timespec endTime;
|
||||||
@@ -119,24 +78,7 @@ namespace alpr
|
|||||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
return plates;
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
|
||||||
plates[i] = expandRect(plates[i], 0, 0, w, h);
|
|
||||||
|
|
||||||
plates[i].x = plates[i].x + offset_x;
|
|
||||||
plates[i].y = plates[i].y + offset_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> orderedRegions = aggregateRegions(plates);
|
|
||||||
|
|
||||||
return orderedRegions;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -46,13 +46,12 @@ namespace alpr
|
|||||||
DetectorCUDA(Config* config, PreWarp* prewarp);
|
DetectorCUDA(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorCUDA();
|
virtual ~DetectorCUDA();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
cv::gpu::CascadeClassifier_GPU cuda_cascade;
|
cv::gpu::CascadeClassifier_GPU cuda_cascade;
|
||||||
|
|
||||||
std::vector<PlateRegion> doCascade(cv::Mat frame, int offset_x, int offset_y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -47,24 +47,15 @@ namespace alpr {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorMorph::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest) {
|
std::vector<cv::Rect> DetectorMorph::find_plates(cv::Mat frame_gray, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
|
|
||||||
Mat frame_gray,frame_gray_cp;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
{
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Mat frame_gray_cp(frame_gray.size(), frame_gray.type());
|
||||||
frame_gray.copyTo(frame_gray_cp);
|
frame_gray.copyTo(frame_gray_cp);
|
||||||
blur(frame_gray, frame_gray, Size(5, 5));
|
blur(frame_gray, frame_gray, Size(5, 5));
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
vector<Rect> plates;
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++) {
|
|
||||||
Mat img_open, img_result;
|
Mat img_open, img_result;
|
||||||
Mat element = getStructuringElement(MORPH_RECT, Size(30, 4));
|
Mat element = getStructuringElement(MORPH_RECT, Size(30, 4));
|
||||||
morphologyEx(frame_gray, img_open, CV_MOP_OPEN, element, cv::Point(-1, -1));
|
morphologyEx(frame_gray, img_open, CV_MOP_OPEN, element, cv::Point(-1, -1));
|
||||||
@@ -202,16 +193,14 @@ for (int i = 0; i < rects.size(); i++) {
|
|||||||
|
|
||||||
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
||||||
Rect bounding_rect = PlateRect.boundingRect();
|
Rect bounding_rect = PlateRect.boundingRect();
|
||||||
PlateReg.rect = expandRect(bounding_rect, 0, 0, frame.cols, frame.rows);
|
|
||||||
|
|
||||||
|
Rect rect_expanded = expandRect(bounding_rect, 0, 0, frame_gray.cols, frame_gray.rows);
|
||||||
|
|
||||||
detectedRegions.push_back(PlateReg);
|
plates.push_back(rect_expanded);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
return plates;
|
||||||
|
|
||||||
return detectedRegions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DetectorMorph::CheckSizes(RotatedRect& mr) {
|
bool DetectorMorph::CheckSizes(RotatedRect& mr) {
|
||||||
|
@@ -38,7 +38,7 @@ namespace alpr {
|
|||||||
DetectorMorph(Config* config, PreWarp* prewarp);
|
DetectorMorph(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorMorph();
|
virtual ~DetectorMorph();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool CheckSizes(cv::RotatedRect& mr);
|
bool CheckSizes(cv::RotatedRect& mr);
|
||||||
|
@@ -98,47 +98,10 @@ namespace alpr
|
|||||||
DetectorOCL::~DetectorOCL() {
|
DetectorOCL::~DetectorOCL() {
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorOCL::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
|
||||||
|
vector<Rect> DetectorOCL::find_plates(Mat orig_frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
{
|
{
|
||||||
|
|
||||||
Mat frame_gray;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++)
|
|
||||||
{
|
|
||||||
// Sanity check. If roi width or height is less than minimum possible plate size,
|
|
||||||
// then skip it
|
|
||||||
if ((regionsOfInterest[i].width < config->minPlateSizeWidthPx) ||
|
|
||||||
(regionsOfInterest[i].height < config->minPlateSizeHeightPx))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Mat cropped = frame_gray(regionsOfInterest[i]);
|
|
||||||
vector<PlateRegion> subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y);
|
|
||||||
|
|
||||||
for (int j = 0; j < subRegions.size(); j++)
|
|
||||||
detectedRegions.push_back(subRegions[j]);
|
|
||||||
}
|
|
||||||
return detectedRegions;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> DetectorOCL::doCascade(Mat orig_frame, int offset_x, int offset_y)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
int w = orig_frame.size().width;
|
|
||||||
int h = orig_frame.size().height;
|
|
||||||
|
|
||||||
float scale_factor = computeScaleFactor(w, h);
|
|
||||||
|
|
||||||
vector<Rect> plates;
|
vector<Rect> plates;
|
||||||
|
|
||||||
@@ -146,12 +109,6 @@ namespace alpr
|
|||||||
timespec startTime;
|
timespec startTime;
|
||||||
getTimeMonotonic(&startTime);
|
getTimeMonotonic(&startTime);
|
||||||
|
|
||||||
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor;
|
|
||||||
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor;
|
|
||||||
|
|
||||||
Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor);
|
|
||||||
Size maxSize(maxWidth, maxHeight);
|
|
||||||
|
|
||||||
// If we have an OpenCL core available, use it. Otherwise use CPU
|
// If we have an OpenCL core available, use it. Otherwise use CPU
|
||||||
if (ocl_detector_mutex_m.try_lock())
|
if (ocl_detector_mutex_m.try_lock())
|
||||||
{
|
{
|
||||||
@@ -160,12 +117,9 @@ namespace alpr
|
|||||||
|
|
||||||
equalizeHist( openclFrame, openclFrame );
|
equalizeHist( openclFrame, openclFrame );
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(openclFrame, openclFrame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
plate_cascade.detectMultiScale( openclFrame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
plate_cascade.detectMultiScale( openclFrame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||||
0,
|
0,
|
||||||
minSize, maxSize );
|
min_plate_size, max_plate_size );
|
||||||
|
|
||||||
ocl_detector_mutex_m.unlock();
|
ocl_detector_mutex_m.unlock();
|
||||||
}
|
}
|
||||||
@@ -173,18 +127,12 @@ namespace alpr
|
|||||||
{
|
{
|
||||||
equalizeHist( orig_frame, orig_frame );
|
equalizeHist( orig_frame, orig_frame );
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(orig_frame, orig_frame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
plate_cascade.detectMultiScale( orig_frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
plate_cascade.detectMultiScale( orig_frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||||
0,
|
0,
|
||||||
minSize, maxSize );
|
min_plate_size, max_plate_size );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (config->debugTiming)
|
if (config->debugTiming)
|
||||||
{
|
{
|
||||||
timespec endTime;
|
timespec endTime;
|
||||||
@@ -192,23 +140,8 @@ namespace alpr
|
|||||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
return plates;
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
|
||||||
plates[i] = expandRect(plates[i], 0, 0, w, h);
|
|
||||||
|
|
||||||
plates[i].x = plates[i].x + offset_x;
|
|
||||||
plates[i].y = plates[i].y + offset_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> orderedRegions = aggregateRegions(plates);
|
|
||||||
|
|
||||||
return orderedRegions;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -42,13 +42,12 @@ namespace alpr
|
|||||||
DetectorOCL(Config* config, PreWarp* prewarp);
|
DetectorOCL(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorOCL();
|
virtual ~DetectorOCL();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
cv::CascadeClassifier plate_cascade;
|
cv::CascadeClassifier plate_cascade;
|
||||||
|
|
||||||
std::vector<PlateRegion> doCascade(cv::Mat frame, int offset_x, int offset_y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user