Tesseract  3.02
tesseract-ocr/cube/classifier_factory.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        classifier_factory.cpp
00003  * Description: Implementation of the Base Character Classifier
00004  * Author:    Ahmad Abdulkader
00005  * Created:   2007
00006  *
00007  * (C) Copyright 2008, Google Inc.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string>
00023 #include "classifier_factory.h"
00024 #include "conv_net_classifier.h"
00025 #include "feature_base.h"
00026 #include "feature_bmp.h"
00027 #include "feature_chebyshev.h"
00028 #include "feature_hybrid.h"
00029 #include "hybrid_neural_net_classifier.h"
00030 
00031 namespace tesseract {
00032 
00033 // Creates a CharClassifier object of the appropriate type depending on the
00034 // classifier type in the settings file
00035 CharClassifier *CharClassifierFactory::Create(const string &data_file_path,
00036                                               const string &lang,
00037                                               LangModel *lang_mod,
00038                                               CharSet *char_set,
00039                                               TuningParams *params) {
00040   // create the feature extraction object
00041   FeatureBase *feat_extract;
00042 
00043   switch (params->TypeFeature()) {
00044     case TuningParams::BMP:
00045       feat_extract = new FeatureBmp(params);
00046       break;
00047     case TuningParams::CHEBYSHEV:
00048       feat_extract = new FeatureChebyshev(params);
00049       break;
00050     case TuningParams::HYBRID:
00051       feat_extract = new FeatureHybrid(params);
00052       break;
00053     default:
00054       fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): invalid "
00055               "feature type.\n");
00056       return NULL;
00057   }
00058 
00059   if (feat_extract == NULL) {
00060     fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): unable "
00061               "to instantiate feature extraction object.\n");
00062     return NULL;
00063   }
00064 
00065   // create the classifier object
00066   CharClassifier *classifier_obj;
00067   switch (params->TypeClassifier()) {
00068     case TuningParams::NN:
00069       classifier_obj = new ConvNetCharClassifier(char_set, params,
00070                                                  feat_extract);
00071       break;
00072     case TuningParams::HYBRID_NN:
00073       classifier_obj = new HybridNeuralNetCharClassifier(char_set, params,
00074                                                          feat_extract);
00075       break;
00076     default:
00077       fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): invalid "
00078               "classifier type.\n");
00079       return NULL;
00080   }
00081 
00082   if (classifier_obj == NULL) {
00083     fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): error "
00084             "allocating memory for character classifier object.\n");
00085     return NULL;
00086   }
00087 
00088   // Init the classifier
00089   if (!classifier_obj->Init(data_file_path, lang, lang_mod)) {
00090     delete classifier_obj;
00091     fprintf(stderr, "Cube ERROR (CharClassifierFactory::Create): unable "
00092             "to Init() character classifier object.\n");
00093     return NULL;
00094   }
00095   return classifier_obj;
00096 }
00097 }