Tesseract  3.02
tesseract-ocr/training/commontraining.h
Go to the documentation of this file.
00001 // Copyright 2008 Google Inc. All Rights Reserved.
00002 // Author: scharron@google.com (Samuel Charron)
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 // http://www.apache.org/licenses/LICENSE-2.0
00008 // Unless required by applicable law or agreed to in writing, software
00009 // distributed under the License is distributed on an "AS IS" BASIS,
00010 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00011 // See the License for the specific language governing permissions and
00012 // limitations under the License.
00013 
00014 #ifndef TESSERACT_TRAINING_COMMONTRAINING_H__
00015 #define TESSERACT_TRAINING_COMMONTRAINING_H__
00016 
00017 #include "oldlist.h"
00018 #include "cluster.h"
00019 #include "intproto.h"
00020 #include "featdefs.h"
00021 
00022 // Macros to merge tesseract params with command-line flags.
00023 #ifdef USE_STD_NAMESPACE
00024 #include "params.h"
00025 #  define INT_PARAM_FLAG(name, val, comment) \
00026     INT_VAR(FLAGS_##name, val, comment)
00027 #  define DECLARE_INT_PARAM_FLAG(name) extern INT_VAR_H(FLAGS_##name, 0, "")
00028 #  define STRING_PARAM_FLAG(name, val, comment) \
00029     STRING_VAR(FLAGS_##name, val, comment)
00030 #  define DECLARE_STRING_PARAM_FLAG(name) \
00031     extern STRING_VAR_H(FLAGS_##name, "", "")
00032 #  define c_str string
00033 #else
00034 #include "base/commandlineflags.h"
00035 #  define INT_PARAM_FLAG(name, val, comment) \
00036     DEFINE_int32(name, val, comment)
00037 #  define DECLARE_INT_PARAM_FLAG(name) DECLARE_int32(name)
00038 #  define STRING_PARAM_FLAG(name, val, comment) \
00039     DEFINE_string(name, val, comment)
00040 #  define DECLARE_STRING_PARAM_FLAG(name) DECLARE_string(name)
00041 #endif
00042 
00043 namespace tesseract {
00044 class Classify;
00045 class MasterTrainer;
00046 class ShapeTable;
00047 }
00048 
00050 // Globals ///////////////////////////////////////////////////////////////////
00052 
00053 extern FEATURE_DEFS_STRUCT feature_defs;
00054 
00055 // Must be defined in the file that "implements" commonTraining facilities.
00056 extern CLUSTERCONFIG Config;
00057 
00059 // Structs ///////////////////////////////////////////////////////////////////
00061 typedef struct
00062 {
00063   char  *Label;
00064   int   SampleCount;
00065   int   font_sample_count;
00066   LIST  List;
00067 }
00068 LABELEDLISTNODE, *LABELEDLIST;
00069 
00070 typedef struct
00071 {
00072   char* Label;
00073   int   NumMerged[MAX_NUM_PROTOS];
00074   CLASS_TYPE Class;
00075 }MERGE_CLASS_NODE;
00076 typedef MERGE_CLASS_NODE* MERGE_CLASS;
00077 
00078 
00080 // Functions /////////////////////////////////////////////////////////////////
00082 void ParseArguments(int* argc, char*** argv);
00083 
00084 namespace tesseract {
00085 // Helper loads shape table from the given file.
00086 ShapeTable* LoadShapeTable(const STRING& file_prefix);
00087 // Helper to write the shape_table.
00088 void WriteShapeTable(const STRING& file_prefix, const ShapeTable& shape_table);
00089 
00090 // Creates a MasterTraininer and loads the training data into it:
00091 // Initializes feature_defs and IntegerFX.
00092 // Loads the shape_table if shape_table != NULL.
00093 // Loads initial unicharset from -U command-line option.
00094 // If FLAGS_input_trainer is set, loads the majority of data from there, else:
00095 //   Loads font info from -F option.
00096 //   Loads xheights from -X option.
00097 //   Loads samples from .tr files in remaining command-line args.
00098 //   Deletes outliers and computes canonical samples.
00099 //   If FLAGS_output_trainer is set, saves the trainer for future use.
00100 // Computes canonical and cloud features.
00101 // If shape_table is not NULL, but failed to load, make a fake flat one,
00102 // as shape clustering was not run.
00103 MasterTrainer* LoadTrainingData(int argc, const char* const * argv,
00104                                 bool replication,
00105                                 ShapeTable** shape_table,
00106                                 STRING* file_prefix);
00107 }  // namespace tesseract.
00108 
00109 const char *GetNextFilename(int argc, const char* const * argv);
00110 
00111 LABELEDLIST FindList(
00112     LIST        List,
00113     char        *Label);
00114 
00115 LABELEDLIST NewLabeledList(
00116     const char  *Label);
00117 
00118 void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs,
00119                          const char *feature_name, int max_samples,
00120                          UNICHARSET* unicharset,
00121                          FILE* file, LIST* training_samples);
00122 
00123 void WriteTrainingSamples(
00124     const FEATURE_DEFS_STRUCT &FeatureDefs,
00125     char *Directory,
00126     LIST CharList,
00127     const char  *program_feature_type);
00128 
00129 void FreeTrainingSamples(
00130     LIST        CharList);
00131 
00132 void FreeLabeledList(
00133     LABELEDLIST LabeledList);
00134 
00135 void FreeLabeledClassList(
00136     LIST        ClassListList);
00137 
00138 CLUSTERER *SetUpForClustering(
00139     const FEATURE_DEFS_STRUCT &FeatureDefs,
00140     LABELEDLIST CharSample,
00141     const char  *program_feature_type);
00142 
00143 LIST RemoveInsignificantProtos(
00144     LIST        ProtoList,
00145     BOOL8       KeepSigProtos,
00146     BOOL8       KeepInsigProtos,
00147     int         N);
00148 
00149 void CleanUpUnusedData(
00150     LIST        ProtoList);
00151 
00152 void MergeInsignificantProtos(
00153     LIST        ProtoList,
00154     const char  *label,
00155     CLUSTERER   *Clusterer,
00156     CLUSTERCONFIG *Config);
00157 
00158 MERGE_CLASS FindClass(
00159     LIST        List,
00160     const char        *Label);
00161 
00162 MERGE_CLASS NewLabeledClass(
00163     const char        *Label);
00164 
00165 void FreeTrainingSamples(
00166     LIST        CharList);
00167 
00168 CLASS_STRUCT* SetUpForFloat2Int(const UNICHARSET& unicharset,
00169                                 LIST LabeledClassList);
00170 
00171 void Normalize(
00172     float       *Values);
00173 
00174 void FreeNormProtoList(
00175     LIST        CharList);
00176 
00177 void AddToNormProtosList(
00178     LIST*       NormProtoList,
00179     LIST        ProtoList,
00180     char        *CharName);
00181 
00182 int NumberOfProtos(
00183     LIST        ProtoList,
00184     BOOL8       CountSigProtos,
00185     BOOL8       CountInsigProtos);
00186 
00187 
00188 void allocNormProtos();
00189 #endif  // TESSERACT_TRAINING_COMMONTRAINING_H__