diff --git a/src/openalpr/stateidentifier.cpp b/src/openalpr/stateidentifier.cpp deleted file mode 100644 index 9e02ffb..0000000 --- a/src/openalpr/stateidentifier.cpp +++ /dev/null @@ -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 . -*/ - -#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 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; - } - -} \ No newline at end of file diff --git a/src/statedetection/CMakeLists.txt b/src/statedetection/CMakeLists.txt new file mode 100644 index 0000000..3c15c6d --- /dev/null +++ b/src/statedetection/CMakeLists.txt @@ -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) + + diff --git a/src/statedetection/state_detector.cpp b/src/statedetection/state_detector.cpp new file mode 100644 index 0000000..8c27045 --- /dev/null +++ b/src/statedetection/state_detector.cpp @@ -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 . +*/ + +#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 StateDetector::detect(vector imageBytes) { + return impl->detect(imageBytes); + } + + vector StateDetector::detect(unsigned char *pixelData, int bytesPerPixel, int imgWidth, + int imgHeight) { + return impl->detect(pixelData, bytesPerPixel, imgWidth, imgHeight); + } + +} diff --git a/src/statedetection/state_detector.h b/src/statedetection/state_detector.h new file mode 100644 index 0000000..4cd29b6 --- /dev/null +++ b/src/statedetection/state_detector.h @@ -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 . +*/ + +#ifndef OPENALPR_STATE_DETECTOR_H +#define OPENALPR_STATE_DETECTOR_H + +#include +#include + +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 detect(std::vector imageBytes); + std::vector detect(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight); + + StateDetectorImpl* impl; + }; + +} + +#endif //OPENALPR_STATE_DETECTOR_H diff --git a/src/statedetection/state_detector_impl.cpp b/src/statedetection/state_detector_impl.cpp new file mode 100644 index 0000000..e4daa66 --- /dev/null +++ b/src/statedetection/state_detector_impl.cpp @@ -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 . +*/ + +#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 StateDetectorImpl::detect(std::vector imageBytes) { + cv::Mat img = cv::imdecode(cv::Mat(imageBytes), 1); + + return this->detect(img); + } + + std::vector 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 StateDetectorImpl::detect(cv::Mat image) { + std::vector results; + + + cv::Mat debugImg(image.size(), image.type()); + image.copyTo(debugImg); + std::vector 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; + } +} \ No newline at end of file diff --git a/src/openalpr/stateidentifier.h b/src/statedetection/state_detector_impl.h similarity index 52% rename from src/openalpr/stateidentifier.h rename to src/statedetection/state_detector_impl.h index c7680c0..273fbcb 100644 --- a/src/openalpr/stateidentifier.h +++ b/src/statedetection/state_detector_impl.h @@ -17,38 +17,33 @@ * along with this program. If not, see . */ -#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 +#include -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 detect(std::vector imageBytes); + std::vector detect(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight); + std::vector detect(cv::Mat image); + FeatureMatcher featureMatcher; }; } -#endif // OPENALPR_STATEIDENTIFIER_H + +#endif //SRC_STATE_DETECTOR_IMPL_H