parent
586f9fdb69
commit
dafe058d2e
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 56 KiB |
File diff suppressed because it is too large
Load Diff
@ -1,237 +0,0 @@
|
||||
#include "easypr/core/chars_identify.h"
|
||||
#include "easypr/core/character.hpp"
|
||||
#include "easypr/config.h"
|
||||
#include "easypr/core/core_func.h"
|
||||
|
||||
namespace easypr {
|
||||
|
||||
CharsIdentify* CharsIdentify::instance_ = nullptr;
|
||||
|
||||
CharsIdentify* CharsIdentify::instance() {
|
||||
if (!instance_) {
|
||||
instance_ = new CharsIdentify;
|
||||
}
|
||||
return instance_;
|
||||
}
|
||||
|
||||
CharsIdentify::CharsIdentify() {
|
||||
ann_ = ml::ANN_MLP::load<ml::ANN_MLP>(kDefaultAnnPath);
|
||||
kv_ = std::shared_ptr<Kv>(new Kv);
|
||||
kv_->load("etc/province_mapping");
|
||||
}
|
||||
|
||||
void CharsIdentify::LoadModel(std::string path) {
|
||||
ann_->clear();
|
||||
ann_->ml::ANN_MLP::load<ml::ANN_MLP>(path);
|
||||
}
|
||||
|
||||
void CharsIdentify::classify(cv::Mat featureRows, std::vector<int>& out_maxIndexs,
|
||||
std::vector<float>& out_maxVals, std::vector<bool> isChineseVec){
|
||||
int rowNum = featureRows.rows;
|
||||
|
||||
cv::Mat output(rowNum, kCharsTotalNumber, CV_32FC1);
|
||||
ann_->predict(featureRows, output);
|
||||
|
||||
for (int output_index = 0; output_index < rowNum; output_index++) {
|
||||
int result = -1;
|
||||
float maxVal = -2.f;
|
||||
bool isChinses = isChineseVec[output_index];
|
||||
if (!isChinses) {
|
||||
result = 0;
|
||||
for (int j = 0; j < kCharactersNumber; j++) {
|
||||
float val = output.at<float>(j);
|
||||
// std::cout << "j:" << j << "val:" << val << std::endl;
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
result = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = kCharactersNumber;
|
||||
for (int j = kCharactersNumber; j < kCharsTotalNumber; j++) {
|
||||
float val = output.at<float>(j);
|
||||
//std::cout << "j:" << j << "val:" << val << std::endl;
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
result = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
out_maxIndexs[output_index] = result;
|
||||
out_maxVals[output_index] = maxVal;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CharsIdentify::classify(std::vector<CCharacter>& charVec){
|
||||
size_t charVec_size = charVec.size();
|
||||
|
||||
Mat featureRows;
|
||||
for (size_t index = 0; index < charVec_size; index++) {
|
||||
Mat charInput = charVec[index].getCharacterMat();
|
||||
Mat feature = charFeatures(charInput, kPredictSize);
|
||||
featureRows.push_back(feature);
|
||||
}
|
||||
|
||||
int rowNum = featureRows.rows;
|
||||
cv::Mat output(rowNum, kCharsTotalNumber, CV_32FC1);
|
||||
ann_->predict(featureRows, output);
|
||||
|
||||
for (int output_index = 0; output_index < rowNum; output_index++) {
|
||||
CCharacter& character = charVec[output_index];
|
||||
int result = -1;
|
||||
float maxVal = -2.f;
|
||||
string label = "";
|
||||
bool isChinses = character.getIsChinese();
|
||||
if (!isChinses) {
|
||||
result = 0;
|
||||
for (int j = 0; j < kCharactersNumber; j++) {
|
||||
float val = output.at<float>(j);
|
||||
// std::cout << "j:" << j << "val:" << val << std::endl;
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
result = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = kCharactersNumber;
|
||||
for (int j = kCharactersNumber; j < kCharsTotalNumber; j++) {
|
||||
float val = output.at<float>(j);
|
||||
//std::cout << "j:" << j << "val:" << val << std::endl;
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
result = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
character.setCharacterScore(maxVal);
|
||||
character.setCharacterStr(result);
|
||||
}
|
||||
}
|
||||
|
||||
int CharsIdentify::classify(cv::Mat f, float& maxVal, bool isChinses){
|
||||
int result = -1;
|
||||
|
||||
cv::Mat output(1, kCharsTotalNumber, CV_32FC1);
|
||||
ann_->predict(f, output);
|
||||
|
||||
maxVal = -2.f;
|
||||
if (!isChinses) {
|
||||
result = 0;
|
||||
for (int j = 0; j < kCharactersNumber; j++) {
|
||||
float val = output.at<float>(j);
|
||||
// std::cout << "j:" << j << "val:" << val << std::endl;
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
result = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = kCharactersNumber;
|
||||
for (int j = kCharactersNumber; j < kCharsTotalNumber; j++) {
|
||||
float val = output.at<float>(j);
|
||||
//std::cout << "j:" << j << "val:" << val << std::endl;
|
||||
if (val > maxVal) {
|
||||
maxVal = val;
|
||||
result = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
//std::cout << "maxVal:" << maxVal << std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CharsIdentify::isCharacter(cv::Mat input, std::string& label, float& maxVal, bool isChinese) {
|
||||
cv::Mat feature = charFeatures(input, kPredictSize);
|
||||
auto index = static_cast<int>(classify(feature, maxVal, isChinese));
|
||||
|
||||
if (isChinese)
|
||||
std::cout << "maxVal:" << maxVal << std::endl;
|
||||
|
||||
if (maxVal >= 0.9) {
|
||||
if (index < kCharactersNumber) {
|
||||
label = std::make_pair(kChars[index], kChars[index]).second;
|
||||
}
|
||||
else {
|
||||
const char* key = kChars[index];
|
||||
std::string s = key;
|
||||
std::string province = kv_->get(s);
|
||||
label = std::make_pair(s, province).second;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/*bool CharsIdentify::charsJudge(std::vector<CCharacter>& charVec) {
|
||||
cv::Mat feature = charFeatures(input, kPredictSize);
|
||||
auto index = static_cast<int>(classify(feature, maxVal, isChinese));
|
||||
|
||||
if (isChinese)
|
||||
std::cout << "maxVal:" << maxVal << std::endl;
|
||||
|
||||
if (maxVal >= 0.9) {
|
||||
if (index < kCharactersNumber) {
|
||||
label = std::make_pair(kChars[index], kChars[index]).second;
|
||||
}
|
||||
else {
|
||||
const char* key = kChars[index];
|
||||
std::string s = key;
|
||||
std::string province = kv_->get(s);
|
||||
label = std::make_pair(s, province).second;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}*/
|
||||
|
||||
|
||||
std::pair<std::string, std::string> CharsIdentify::identify(cv::Mat input, bool isChinese) {
|
||||
cv::Mat feature = charFeatures(input, kPredictSize);
|
||||
float maxVal = -2;
|
||||
auto index = static_cast<int>(classify(feature, maxVal, isChinese));
|
||||
if (index < kCharactersNumber) {
|
||||
return std::make_pair(kChars[index], kChars[index]);
|
||||
}
|
||||
else {
|
||||
const char* key = kChars[index];
|
||||
std::string s = key;
|
||||
std::string province = kv_->get(s);
|
||||
return std::make_pair(s, province);
|
||||
}
|
||||
}
|
||||
|
||||
int CharsIdentify::identify(std::vector<cv::Mat> inputs, std::vector<std::pair<std::string, std::string>>& outputs,
|
||||
std::vector<bool> isChineseVec) {
|
||||
Mat featureRows;
|
||||
size_t input_size = inputs.size();
|
||||
for (size_t i = 0; i < input_size; i++) {
|
||||
Mat input = inputs[i];
|
||||
cv::Mat feature = charFeatures(input, kPredictSize);
|
||||
featureRows.push_back(feature);
|
||||
}
|
||||
|
||||
std::vector<int> maxIndexs;
|
||||
std::vector<float> maxVals;
|
||||
classify(featureRows, maxIndexs, maxVals, isChineseVec);
|
||||
|
||||
for (size_t row_index = 0; row_index < input_size; row_index++) {
|
||||
int index = maxIndexs[row_index];
|
||||
if (index < kCharactersNumber) {
|
||||
outputs[row_index] = std::make_pair(kChars[index], kChars[index]);
|
||||
}
|
||||
else {
|
||||
const char* key = kChars[index];
|
||||
std::string s = key;
|
||||
std::string province = kv_->get(s);
|
||||
outputs[row_index] = std::make_pair(s, province);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue