tesseract v5.3.3.20231005
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 194 of file mftraining.cpp.

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