tesseract  4.1.1
mftraining.cpp File Reference
#include <cmath>
#include <cstring>
#include <cstdio>
#include "classify.h"
#include "cluster.h"
#include "clusttool.h"
#include "commontraining.h"
#include "featdefs.h"
#include "fontinfo.h"
#include "genericvector.h"
#include "indexmapbidi.h"
#include "intproto.h"
#include "mastertrainer.h"
#include "mergenf.h"
#include "mf.h"
#include "ocrfeatures.h"
#include "oldlist.h"
#include "protos.h"
#include "shapetable.h"
#include "tessopt.h"
#include "tprintf.h"
#include "unicity_table.h"

Go to the source code of this file.

Macros

#define _USE_MATH_DEFINES
 

Functions

int main (int argc, char **argv)
 

Variables

const int kMaxShapeLabelLength = 10
 

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

Definition at line 24 of file mftraining.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

This program reads in a text file consisting of feature samples from a training page in the following format:

   FontName UTF8-char-str xmin ymin xmax ymax page-number
    NumberOfFeatureTypes(N)
      FeatureTypeName1 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      FeatureTypeName2 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      ...
      FeatureTypeNameN NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
   FontName CharName ...

The result of this program is a binary inttemp file used by the OCR engine.

Parameters
argcnumber of command line arguments
argvarray of command line arguments
Returns
0 if no error occurred

Definition at line 209 of file mftraining.cpp.

209  {
210  tesseract::CheckSharedLibraryVersion();
211 
212  ParseArguments(&argc, &argv);
213 
214  ShapeTable* shape_table = nullptr;
215  STRING file_prefix;
216  // Load the training data.
217  MasterTrainer* trainer = tesseract::LoadTrainingData(argc, argv,
218  false,
219  &shape_table,
220  &file_prefix);
221  if (trainer == nullptr) return 1; // Failed.
222 
223  // Setup an index mapping from the shapes in the shape table to the classes
224  // that will be trained. In keeping with the original design, each shape
225  // with the same list of unichars becomes a different class and the configs
226  // represent the different combinations of fonts.
227  IndexMapBiDi config_map;
228  SetupConfigMap(shape_table, &config_map);
229 
230  WriteShapeTable(file_prefix, *shape_table);
231  // If the shape_table is flat, then either we didn't run shape clustering, or
232  // it did nothing, so we just output the trainer's unicharset.
233  // Otherwise shape_set will hold a fake unicharset with an entry for each
234  // shape in the shape table, and we will output that instead.
235  UNICHARSET shape_set;
236  const UNICHARSET* unicharset = &trainer->unicharset();
237  // If we ran shapeclustering (and it worked) then at least one shape will
238  // have multiple unichars, so we have to build a fake unicharset.
239  if (shape_table->AnyMultipleUnichars()) {
240  unicharset = &shape_set;
241  // Now build a fake unicharset for the compact shape space to keep the
242  // output modules happy that we are doing things correctly.
243  int num_shapes = config_map.CompactSize();
244  for (int s = 0; s < num_shapes; ++s) {
245  char shape_label[kMaxShapeLabelLength + 1];
246  snprintf(shape_label, kMaxShapeLabelLength, "sh%04d", s);
247  shape_set.unichar_insert(shape_label);
248  }
249  }
250 
251  // Now train each config separately.
252  int num_configs = shape_table->NumShapes();
253  LIST mf_classes = NIL_LIST;
254  for (int s = 0; s < num_configs; ++s) {
255  int unichar_id, font_id;
256  if (unicharset == &shape_set) {
257  // Using fake unichar_ids from the config_map/shape_set.
258  unichar_id = config_map.SparseToCompact(s);
259  } else {
260  // Get the real unichar_id from the shape table/unicharset.
261  shape_table->GetFirstUnicharAndFont(s, &unichar_id, &font_id);
262  }
263  const char* class_label = unicharset->id_to_unichar(unichar_id);
264  mf_classes = ClusterOneConfig(s, class_label, mf_classes, *shape_table,
265  trainer);
266  }
267  STRING inttemp_file = file_prefix;
268  inttemp_file += "inttemp";
269  STRING pffmtable_file = file_prefix;
270  pffmtable_file += "pffmtable";
271  CLASS_STRUCT* float_classes = SetUpForFloat2Int(*unicharset, mf_classes);
272  // Now write the inttemp and pffmtable.
273  trainer->WriteInttempAndPFFMTable(trainer->unicharset(), *unicharset,
274  *shape_table, float_classes,
275  inttemp_file.string(),
276  pffmtable_file.string());
277  for (int c = 0; c < unicharset->size(); ++c) {
278  FreeClassFields(&float_classes[c]);
279  }
280  delete [] float_classes;
281  FreeLabeledClassList(mf_classes);
282  delete trainer;
283  delete shape_table;
284  printf("Done!\n");
285  if (!FLAGS_test_ch.empty()) {
286  // If we are displaying debug window(s), wait for the user to look at them.
287  printf("Hit return to exit...\n");
288  while (getchar() != '\n');
289  }
290  return 0;
291 } /* main */
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:138
#define NIL_LIST
Definition: oldlist.h:76
void FreeLabeledClassList(LIST ClassList)
void ParseArguments(int *argc, char ***argv)
CLASS_STRUCT * SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassList)
const int kMaxShapeLabelLength
Definition: mftraining.cpp:59
MasterTrainer * LoadTrainingData(int argc, const char *const *argv, bool replication, ShapeTable **shape_table, STRING *file_prefix)
void WriteShapeTable(const STRING &file_prefix, const ShapeTable &shape_table)
int CompactSize() const
Definition: indexmapbidi.h:61
int SparseToCompact(int sparse_index) const override
Definition: indexmapbidi.h:138
Definition: strngs.h:45
const char * string() const
Definition: strngs.cpp:194
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
Definition: unicharset.cpp:626
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:291
int size() const
Definition: unicharset.h:341
bool AnyMultipleUnichars() const
Definition: shapetable.cpp:444
int NumShapes() const
Definition: shapetable.h:274
void GetFirstUnicharAndFont(int shape_id, int *unichar_id, int *font_id) const
Definition: shapetable.cpp:404
const UNICHARSET & unicharset() const
void WriteInttempAndPFFMTable(const UNICHARSET &unicharset, const UNICHARSET &shape_set, const ShapeTable &shape_table, CLASS_STRUCT *float_classes, const char *inttemp_file, const char *pffmtable_file)

Variable Documentation

◆ kMaxShapeLabelLength

const int kMaxShapeLabelLength = 10

Definition at line 59 of file mftraining.cpp.