mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-07 00:02:52 +08:00
moved existing state detection code into separate library
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015 OpenALPR Technology, Inc.
|
||||
* Open source 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 "stateidentifier.h"
|
||||
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
namespace alpr
|
||||
{
|
||||
|
||||
StateIdentifier::StateIdentifier(Config* config)
|
||||
{
|
||||
this->config = config;
|
||||
|
||||
featureMatcher = new FeatureMatcher(config);
|
||||
|
||||
if (featureMatcher->isLoaded() == false)
|
||||
{
|
||||
cout << "Can not create detector or descriptor extractor or descriptor matcher of given types" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
featureMatcher->loadRecognitionSet(config->country);
|
||||
}
|
||||
|
||||
StateIdentifier::~StateIdentifier()
|
||||
{
|
||||
delete featureMatcher;
|
||||
}
|
||||
|
||||
|
||||
// Attempts to recognize the plate. Returns a confidence level. Updates the region code and confidence
|
||||
// If region is found, returns true.
|
||||
bool StateIdentifier::recognize(PipelineData* pipeline_data)
|
||||
{
|
||||
timespec startTime;
|
||||
getTimeMonotonic(&startTime);
|
||||
|
||||
Mat plateImg = Mat(pipeline_data->grayImg, pipeline_data->regionOfInterest);
|
||||
|
||||
resize(plateImg, plateImg, getSizeMaintainingAspect(plateImg, config->stateIdImageWidthPx, config->stateIdimageHeightPx));
|
||||
|
||||
|
||||
Mat debugImg(plateImg.size(), plateImg.type());
|
||||
plateImg.copyTo(debugImg);
|
||||
vector<int> matchesArray(featureMatcher->numTrainingElements());
|
||||
|
||||
RecognitionResult result = featureMatcher->recognize(plateImg, true, &debugImg, true, matchesArray );
|
||||
|
||||
if (this->config->debugStateId)
|
||||
{
|
||||
displayImage(config, "State Identifier1", plateImg);
|
||||
displayImage(config, "State Identifier", debugImg);
|
||||
cout << result.haswinner << " : " << result.confidence << " : " << result.winner << endl;
|
||||
}
|
||||
|
||||
if (config->debugTiming)
|
||||
{
|
||||
timespec endTime;
|
||||
getTimeMonotonic(&endTime);
|
||||
cout << "State Identification Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||
}
|
||||
|
||||
if (result.haswinner == false)
|
||||
return 0;
|
||||
|
||||
pipeline_data->region_code = result.winner;
|
||||
pipeline_data->region_confidence = result.confidence;
|
||||
|
||||
if (result.confidence >= 10)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
23
src/statedetection/CMakeLists.txt
Normal file
23
src/statedetection/CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
|
||||
|
||||
set(statedetector_source_files
|
||||
state_detector.cpp
|
||||
featurematcher.cpp
|
||||
state_detector_impl.cpp
|
||||
)
|
||||
|
||||
|
||||
add_library(statedetection SHARED ${statedetector_source_files} )
|
||||
|
||||
set_target_properties(statedetection PROPERTIES SOVERSION ${OPENALPR_MAJOR_VERSION})
|
||||
|
||||
TARGET_LINK_LIBRARIES(statedetection
|
||||
${OpenCV_LIBS}
|
||||
)
|
||||
|
||||
|
||||
install (FILES state_detector.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
|
||||
install (TARGETS statedetection DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
|
||||
|
||||
|
52
src/statedetection/state_detector.cpp
Normal file
52
src/statedetection/state_detector.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2015 OpenALPR Technology, Inc.
|
||||
* Open source 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 "state_detector.h"
|
||||
#include "state_detector_impl.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace alpr {
|
||||
|
||||
StateDetector::StateDetector(const std::string country, const std::string runtimeDir) {
|
||||
impl = new StateDetectorImpl(country, runtimeDir);
|
||||
}
|
||||
|
||||
StateDetector::~StateDetector() {
|
||||
delete impl;
|
||||
}
|
||||
|
||||
bool StateDetector::isLoaded() {
|
||||
return impl->isLoaded();
|
||||
}
|
||||
|
||||
void StateDetector::setTopN(int topN) {
|
||||
impl->setTopN(topN);
|
||||
}
|
||||
|
||||
vector<StateCandidate> StateDetector::detect(vector<char> imageBytes) {
|
||||
return impl->detect(imageBytes);
|
||||
}
|
||||
|
||||
vector<StateCandidate> StateDetector::detect(unsigned char *pixelData, int bytesPerPixel, int imgWidth,
|
||||
int imgHeight) {
|
||||
return impl->detect(pixelData, bytesPerPixel, imgWidth, imgHeight);
|
||||
}
|
||||
|
||||
}
|
55
src/statedetection/state_detector.h
Normal file
55
src/statedetection/state_detector.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2015 OpenALPR Technology, Inc.
|
||||
* Open source 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_STATE_DETECTOR_H
|
||||
#define OPENALPR_STATE_DETECTOR_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace alpr {
|
||||
|
||||
struct StateCandidate
|
||||
{
|
||||
std::string state_code;
|
||||
float confidence;
|
||||
};
|
||||
|
||||
class StateDetectorImpl;
|
||||
class StateDetector {
|
||||
|
||||
public:
|
||||
StateDetector(const std::string country, const std::string runtimeDir);
|
||||
virtual ~StateDetector();
|
||||
|
||||
bool isLoaded();
|
||||
|
||||
// Maximum number of candidates to return
|
||||
void setTopN(int topN);
|
||||
|
||||
// Given an image of a license plate, provide the likely state candidates
|
||||
std::vector<StateCandidate> detect(std::vector<char> imageBytes);
|
||||
std::vector<StateCandidate> detect(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight);
|
||||
|
||||
StateDetectorImpl* impl;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENALPR_STATE_DETECTOR_H
|
83
src/statedetection/state_detector_impl.cpp
Normal file
83
src/statedetection/state_detector_impl.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2015 OpenALPR Technology, Inc.
|
||||
* Open source 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 "state_detector_impl.h"
|
||||
|
||||
namespace alpr
|
||||
{
|
||||
StateDetectorImpl::StateDetectorImpl(const std::string country, const std::string runtimeDir)
|
||||
{
|
||||
|
||||
|
||||
if (featureMatcher.isLoaded() == false)
|
||||
{
|
||||
std::cout << "Can not create detector or descriptor extractor or descriptor matcher of given types" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
featureMatcher.loadRecognitionSet(runtimeDir, country);
|
||||
}
|
||||
|
||||
StateDetectorImpl::~StateDetectorImpl() { }
|
||||
|
||||
bool StateDetectorImpl::isLoaded() {
|
||||
return false;
|
||||
}
|
||||
|
||||
void StateDetectorImpl::setTopN(int topN) {
|
||||
}
|
||||
|
||||
std::vector<StateCandidate> StateDetectorImpl::detect(std::vector<char> imageBytes) {
|
||||
cv::Mat img = cv::imdecode(cv::Mat(imageBytes), 1);
|
||||
|
||||
return this->detect(img);
|
||||
}
|
||||
|
||||
std::vector<StateCandidate> StateDetectorImpl::detect(unsigned char *pixelData, int bytesPerPixel, int imgWidth,
|
||||
int imgHeight) {
|
||||
int arraySize = imgWidth * imgHeight * bytesPerPixel;
|
||||
cv::Mat imgData = cv::Mat(arraySize, 1, CV_8U, pixelData);
|
||||
cv::Mat img = imgData.reshape(bytesPerPixel, imgHeight);
|
||||
|
||||
return this->detect(img);
|
||||
|
||||
}
|
||||
|
||||
std::vector<StateCandidate> StateDetectorImpl::detect(cv::Mat image) {
|
||||
std::vector<StateCandidate> results;
|
||||
|
||||
|
||||
cv::Mat debugImg(image.size(), image.type());
|
||||
image.copyTo(debugImg);
|
||||
std::vector<int> matchesArray(featureMatcher.numTrainingElements());
|
||||
|
||||
RecognitionResult result = featureMatcher.recognize(image, true, &debugImg, true, matchesArray );
|
||||
|
||||
if (result.haswinner == false)
|
||||
return results;
|
||||
|
||||
StateCandidate top_candidate;
|
||||
top_candidate.confidence = result.confidence;
|
||||
top_candidate.state_code = result.winner;
|
||||
|
||||
results.push_back(top_candidate);
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
@@ -17,38 +17,33 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef OPENALPR_STATEIDENTIFIER_H
|
||||
#define OPENALPR_STATEIDENTIFIER_H
|
||||
#ifndef SRC_STATE_DETECTOR_IMPL_H
|
||||
#define SRC_STATE_DETECTOR_IMPL_H
|
||||
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "constants.h"
|
||||
#include "state_detector.h"
|
||||
#include "featurematcher.h"
|
||||
#include "utility.h"
|
||||
#include "config.h"
|
||||
#include "pipeline_data.h"
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
namespace alpr
|
||||
{
|
||||
|
||||
class StateIdentifier
|
||||
{
|
||||
namespace alpr {
|
||||
|
||||
class StateDetectorImpl {
|
||||
public:
|
||||
StateIdentifier(Config* config);
|
||||
virtual ~StateIdentifier();
|
||||
StateDetectorImpl(const std::string country, const std::string runtimeDir);
|
||||
virtual ~StateDetectorImpl();
|
||||
|
||||
bool recognize(PipelineData* pipeline_data);
|
||||
bool isLoaded();
|
||||
|
||||
//int confidence;
|
||||
// Maximum number of candidates to return
|
||||
void setTopN(int topN);
|
||||
|
||||
protected:
|
||||
Config* config;
|
||||
|
||||
private:
|
||||
|
||||
FeatureMatcher* featureMatcher;
|
||||
std::vector<StateCandidate> detect(std::vector<char> imageBytes);
|
||||
std::vector<StateCandidate> detect(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight);
|
||||
std::vector<StateCandidate> detect(cv::Mat image);
|
||||
|
||||
FeatureMatcher featureMatcher;
|
||||
};
|
||||
|
||||
}
|
||||
#endif // OPENALPR_STATEIDENTIFIER_H
|
||||
|
||||
#endif //SRC_STATE_DETECTOR_IMPL_H
|
Reference in New Issue
Block a user