tesseract  3.05.02
mftraining.cpp File Reference
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "classify.h"
#include "cluster.h"
#include "clusttool.h"
#include "commontraining.h"
#include "danerror.h"
#include "efio.h"
#include "emalloc.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 "ndminx.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
 
#define PROGRAM_FEATURE_TYPE   "mf"
 

Functions

 DECLARE_STRING_PARAM_FLAG (test_ch)
 
int main (int argc, char **argv)
 

Variables

const int kMaxShapeLabelLength = 10
 

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

Definition at line 35 of file mftraining.cpp.

◆ PROGRAM_FEATURE_TYPE

#define PROGRAM_FEATURE_TYPE   "mf"

Definition at line 72 of file mftraining.cpp.

Function Documentation

◆ DECLARE_STRING_PARAM_FLAG()

DECLARE_STRING_PARAM_FLAG ( test_ch  )

◆ 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
none
Note
Exceptions: none
History: Fri Aug 18 08:56:17 1989, DSJ, Created.
History: Mon May 18 1998, Christy Russson, Revistion started.

Definition at line 238 of file mftraining.cpp.

238  {
239  ParseArguments(&argc, &argv);
240 
241  ShapeTable* shape_table = NULL;
242  STRING file_prefix;
243  // Load the training data.
244  MasterTrainer* trainer = tesseract::LoadTrainingData(argc, argv,
245  false,
246  &shape_table,
247  &file_prefix);
248  if (trainer == NULL)
249  return 1; // Failed.
250 
251  // Setup an index mapping from the shapes in the shape table to the classes
252  // that will be trained. In keeping with the original design, each shape
253  // with the same list of unichars becomes a different class and the configs
254  // represent the different combinations of fonts.
255  IndexMapBiDi config_map;
256  SetupConfigMap(shape_table, &config_map);
257 
258  WriteShapeTable(file_prefix, *shape_table);
259  // If the shape_table is flat, then either we didn't run shape clustering, or
260  // it did nothing, so we just output the trainer's unicharset.
261  // Otherwise shape_set will hold a fake unicharset with an entry for each
262  // shape in the shape table, and we will output that instead.
263  UNICHARSET shape_set;
264  const UNICHARSET* unicharset = &trainer->unicharset();
265  // If we ran shapeclustering (and it worked) then at least one shape will
266  // have multiple unichars, so we have to build a fake unicharset.
267  if (shape_table->AnyMultipleUnichars()) {
268  unicharset = &shape_set;
269  // Now build a fake unicharset for the compact shape space to keep the
270  // output modules happy that we are doing things correctly.
271  int num_shapes = config_map.CompactSize();
272  for (int s = 0; s < num_shapes; ++s) {
273  char shape_label[kMaxShapeLabelLength + 1];
274  snprintf(shape_label, kMaxShapeLabelLength, "sh%04d", s);
275  shape_set.unichar_insert(shape_label);
276  }
277  }
278 
279  // Now train each config separately.
280  int num_configs = shape_table->NumShapes();
281  LIST mf_classes = NIL_LIST;
282  for (int s = 0; s < num_configs; ++s) {
283  int unichar_id, font_id;
284  if (unicharset == &shape_set) {
285  // Using fake unichar_ids from the config_map/shape_set.
286  unichar_id = config_map.SparseToCompact(s);
287  } else {
288  // Get the real unichar_id from the shape table/unicharset.
289  shape_table->GetFirstUnicharAndFont(s, &unichar_id, &font_id);
290  }
291  const char* class_label = unicharset->id_to_unichar(unichar_id);
292  mf_classes = ClusterOneConfig(s, class_label, mf_classes, *shape_table,
293  trainer);
294  }
295  STRING inttemp_file = file_prefix;
296  inttemp_file += "inttemp";
297  STRING pffmtable_file = file_prefix;
298  pffmtable_file += "pffmtable";
299  CLASS_STRUCT* float_classes = SetUpForFloat2Int(*unicharset, mf_classes);
300  // Now write the inttemp and pffmtable.
301  trainer->WriteInttempAndPFFMTable(trainer->unicharset(), *unicharset,
302  *shape_table, float_classes,
303  inttemp_file.string(),
304  pffmtable_file.string());
305  for (int c = 0; c < unicharset->size(); ++c) {
306  FreeClassFields(&float_classes[c]);
307  }
308  delete [] float_classes;
309  FreeLabeledClassList(mf_classes);
310  delete trainer;
311  delete shape_table;
312  printf("Done!\n");
313  if (!FLAGS_test_ch.empty()) {
314  // If we are displaying debug window(s), wait for the user to look at them.
315  printf("Hit return to exit...\n");
316  while (getchar() != '\n');
317  }
318  return 0;
319 } /* main */
int NumShapes() const
Definition: shapetable.h:278
virtual int SparseToCompact(int sparse_index) const
Definition: indexmapbidi.h:138
int size() const
Definition: unicharset.h:297
CLASS_STRUCT * SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassList)
#define NIL_LIST
Definition: oldlist.h:126
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:228
void ParseArguments(int *argc, char ***argv)
const int kMaxShapeLabelLength
Definition: mftraining.cpp:75
const char * string() const
Definition: strngs.cpp:201
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)
bool AnyMultipleUnichars() const
Definition: shapetable.cpp:454
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)
void TESS_API unichar_insert(const char *const unichar_repr)
Definition: unicharset.cpp:612
void GetFirstUnicharAndFont(int shape_id, int *unichar_id, int *font_id) const
Definition: shapetable.cpp:414
Definition: strngs.h:44
void FreeLabeledClassList(LIST ClassList)
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:266
int CompactSize() const
Definition: indexmapbidi.h:61
const UNICHARSET & unicharset() const

Variable Documentation

◆ kMaxShapeLabelLength

const int kMaxShapeLabelLength = 10

Definition at line 75 of file mftraining.cpp.