* change to train Chinese model.

v1.6alpha
liuruoze 9 years ago
parent 510f019cae
commit d2af6b0464

@ -28,11 +28,11 @@ static const float kSvmPercentage = 0.7f;
static const int kCharacterInput = 120;
static const int kChineseInput = 440;
static const int kAnnInput = kCharacterInput;
static const int kAnnInput = kChineseInput;
static const int kCharacterSize = 10;
static const int kChineseSize = 20;
static const int kPredictSize = kCharacterSize;
static const int kPredictSize = kChineseSize;
static const int kNeurons = 40;

@ -37,6 +37,10 @@ void getLBPFeatures(const cv::Mat& image, cv::Mat& features);
//! get character feature
cv::Mat charFeatures(cv::Mat in, int sizeData);
//! get character feature
cv::Mat charFeatures2(cv::Mat in, int sizeData);
//! LBP feature + Histom feature
void getLBPplusHistFeatures(const cv::Mat& image, cv::Mat& features);
} /*! \namespace easypr*/

@ -92,6 +92,8 @@ void getLBPFeatures(const Mat& image, Mat& features) {
}
Mat charFeatures(Mat in, int sizeData) {
const int VERTICAL = 0;
const int HORIZONTAL = 1;
@ -139,6 +141,53 @@ Mat charFeatures(Mat in, int sizeData) {
}
Mat charFeatures2(Mat in, int sizeData) {
const int VERTICAL = 0;
const int HORIZONTAL = 1;
// cut the cetner, will afect 5% perices.
Rect _rect = GetCenterRect(in);
Mat tmpIn = CutTheRect(in, _rect);
//Mat tmpIn = in.clone();
// Low data feature
Mat lowData;
resize(tmpIn, lowData, Size(sizeData, sizeData));
// Histogram features
Mat vhist = ProjectedHistogram(lowData, VERTICAL);
Mat hhist = ProjectedHistogram(lowData, HORIZONTAL);
// Last 10 is the number of moments components
int numCols = vhist.cols + hhist.cols + lowData.cols * lowData.cols;
Mat out = Mat::zeros(1, numCols, CV_32F);
// Asign values to
// feature,ANN的样本特征为水平、垂直直方图和低分辨率图像所组成的矢量
int j = 0;
for (int i = 0; i < vhist.cols; i++) {
out.at<float>(j) = vhist.at<float>(i);
j++;
}
for (int i = 0; i < hhist.cols; i++) {
out.at<float>(j) = hhist.at<float>(i);
j++;
}
for (int x = 0; x < lowData.cols; x++) {
for (int y = 0; y < lowData.rows; y++) {
out.at<float>(j) += (float)lowData.at <unsigned char>(x, y);
j++;
}
}
//std::cout << out << std::endl;
return out;
}
void getLBPplusHistFeatures(const Mat& image, Mat& features) {
// TODO
Mat grayImage;

@ -46,7 +46,7 @@ void AnnTrain::train() {
int first_hidden_neurons = int(std::sqrt((m + 2) * N) + 2 * std::sqrt(N / (m + 2)));
int second_hidden_neurons = int(m * std::sqrt(N / (m + 2)));
bool useTLFN = false;
bool useTLFN = true;
if (!useTLFN) {
layers.create(1, 3, CV_32SC1);
layers.at<int>(0) = input_number;
@ -68,29 +68,28 @@ void AnnTrain::train() {
ann_->setLayerSizes(layers);
ann_->setActivationFunction(cv::ml::ANN_MLP::SIGMOID_SYM, 1, 1);
ann_->setTrainMethod(cv::ml::ANN_MLP::TrainingMethods::BACKPROP);
ann_->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER, 10000, 0.001));
ann_->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER, 30000, 0.0001));
ann_->setBackpropWeightScale(0.1);
ann_->setBackpropMomentumScale(0.1);
//using raw data or raw + synthic data.
//auto traindata = tdata();
auto traindata = sdata(100);
auto traindata = sdata(350);
std::cout << "Training ANN model, please wait..." << std::endl;
long start = utils::getTimestamp();
ann_->train(traindata);
long end = utils::getTimestamp();
std::cout << "Training done. Time elapse: " << (end - start) / (1000 * 60) << "minute"
<< std::endl;
ann_->save(ann_xml_);
std::cout << "Your ANN Model was saved to " << ann_xml_ << std::endl;
test();
std::cout << "Your ANN Model was saved to " << ann_xml_ << std::endl;
std::cout << "Training done. Time elapse: " << (end - start) / (1000 * 60) << "minute"
<< std::endl;
}
std::pair<std::string, std::string> AnnTrain::identifyChinese(cv::Mat input) {
cv::Mat feature = charFeatures(input, kPredictSize);
cv::Mat feature = charFeatures2(input, kPredictSize);
float maxVal = -2;
int result = -1;
@ -116,7 +115,7 @@ std::pair<std::string, std::string> AnnTrain::identifyChinese(cv::Mat input) {
std::pair<std::string, std::string> AnnTrain::identify(cv::Mat input) {
cv::Mat feature = charFeatures(input, kPredictSize);
cv::Mat feature = charFeatures2(input, kPredictSize);
float maxVal = -2;
int result = -1;
@ -216,8 +215,8 @@ cv::Mat getSyntheticImage(const Mat& image) {
Mat result = image.clone();
if (rand_type % 2 == 0) {
int ran_x = rand() % 7 - 3;
int ran_y = rand() % 7 - 3;
int ran_x = rand() % 5 - 2;
int ran_y = rand() % 5 - 2;
result = translateImg(result, ran_x, ran_y);
}
@ -276,7 +275,7 @@ cv::Ptr<cv::ml::TrainData> AnnTrain::sdata(size_t number_for_count) {
fprintf(stdout, ">> Characters count: %d \n", matVec.size());
for (auto img : matVec) {
auto fps = charFeatures(img, kPredictSize);
auto fps = charFeatures2(img, kPredictSize);
samples.push_back(fps);
labels.push_back(i);
@ -319,7 +318,7 @@ cv::Ptr<cv::ml::TrainData> AnnTrain::tdata() {
auto chars_files = utils::getFiles(sub_folder);
for (auto file : chars_files) {
auto img = cv::imread(file, 0); // a grayscale image
auto fps = charFeatures(img, kPredictSize);
auto fps = charFeatures2(img, kPredictSize);
samples.push_back(fps);
labels.push_back(i);

Loading…
Cancel
Save