tesseract  5.0.0
mftraining.cpp File Reference
#include <cmath>
#include <cstdio>
#include <cstring>
#include "classify.h"
#include "cluster.h"
#include "clusttool.h"
#include "commontraining.h"
#include "featdefs.h"
#include "fontinfo.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 "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)
 

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 193 of file mftraining.cpp.

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