tesseract  4.1.1
cntraining.cpp File Reference
#include "oldlist.h"
#include "featdefs.h"
#include "tessopt.h"
#include "ocrfeatures.h"
#include "clusttool.h"
#include "cluster.h"
#include <cstring>
#include <cstdio>
#include <cmath>
#include "unichar.h"
#include "commontraining.h"

Go to the source code of this file.

Macros

#define PROGRAM_FEATURE_TYPE   "cn"
 

Functions

int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ PROGRAM_FEATURE_TYPE

#define PROGRAM_FEATURE_TYPE   "cn"

Definition at line 34 of file cntraining.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 CharName NumberOfFeatureTypes(N)
      FeatureTypeName1 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      FeatureTypeName2 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      ...
      FeatureTypeNameN NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
   FontName CharName ...

It then appends these samples into a separate file for each character. The name of the file is

DirectoryName/FontName/CharName.FeatureTypeName

The DirectoryName can be specified via a command line argument. If not specified, it defaults to the current directory. The format of the resulting files is:

   NumberOfFeatures(M)
      Feature1
      ...
      FeatureM
   NumberOfFeatures(M)
   ...

The output files each have a header which describes the type of feature which the file contains. This header is in the format required by the clusterer. A command line argument can also be used to specify that only the first N samples of each class should be used.

Parameters
argcnumber of command line arguments
argvarray of command line arguments
Returns
0 on success

Definition at line 104 of file cntraining.cpp.

104  {
105  tesseract::CheckSharedLibraryVersion();
106 
107  // Set the global Config parameters before parsing the command line.
108  Config = CNConfig;
109 
110  const char *PageName;
111  LIST CharList = NIL_LIST;
112  CLUSTERER *Clusterer = nullptr;
113  LIST ProtoList = NIL_LIST;
114  LIST NormProtoList = NIL_LIST;
115  LIST pCharList;
116  LABELEDLIST CharSample;
117  FEATURE_DEFS_STRUCT FeatureDefs;
118  InitFeatureDefs(&FeatureDefs);
119 
120  ParseArguments(&argc, &argv);
121  int num_fonts = 0;
122  while ((PageName = GetNextFilename(argc, argv)) != nullptr) {
123  printf("Reading %s ...\n", PageName);
124  FILE *TrainingPage = fopen(PageName, "rb");
125  ASSERT_HOST(TrainingPage);
126  if (TrainingPage) {
127  ReadTrainingSamples(FeatureDefs, PROGRAM_FEATURE_TYPE, 100, nullptr,
128  TrainingPage, &CharList);
129  fclose(TrainingPage);
130  ++num_fonts;
131  }
132  }
133  printf("Clustering ...\n");
134  // To allow an individual font to form a separate cluster,
135  // reduce the min samples:
136  // Config.MinSamples = 0.5 / num_fonts;
137  pCharList = CharList;
138  // The norm protos will count the source protos, so we keep them here in
139  // freeable_protos, so they can be freed later.
140  GenericVector<LIST> freeable_protos;
141  iterate(pCharList) {
142  //Cluster
143  CharSample = reinterpret_cast<LABELEDLIST>first_node(pCharList);
144  Clusterer =
145  SetUpForClustering(FeatureDefs, CharSample, PROGRAM_FEATURE_TYPE);
146  if (Clusterer == nullptr) { // To avoid a SIGSEGV
147  fprintf(stderr, "Error: nullptr clusterer!\n");
148  return 1;
149  }
150  float SavedMinSamples = Config.MinSamples;
151  // To disable the tendency to produce a single cluster for all fonts,
152  // make MagicSamples an impossible to achieve number:
153  // Config.MagicSamples = CharSample->SampleCount * 10;
154  Config.MagicSamples = CharSample->SampleCount;
155  while (Config.MinSamples > 0.001) {
156  ProtoList = ClusterSamples(Clusterer, &Config);
157  if (NumberOfProtos(ProtoList, true, false) > 0) {
158  break;
159  } else {
160  Config.MinSamples *= 0.95;
161  printf("0 significant protos for %s."
162  " Retrying clustering with MinSamples = %f%%\n",
163  CharSample->Label, Config.MinSamples);
164  }
165  }
166  Config.MinSamples = SavedMinSamples;
167  AddToNormProtosList(&NormProtoList, ProtoList, CharSample->Label);
168  freeable_protos.push_back(ProtoList);
169  FreeClusterer(Clusterer);
170  }
171  FreeTrainingSamples(CharList);
172  int desc_index = ShortNameToFeatureType(FeatureDefs, PROGRAM_FEATURE_TYPE);
173  WriteNormProtos(FLAGS_D.c_str(), NormProtoList,
174  FeatureDefs.FeatureDesc[desc_index]);
175  FreeNormProtoList(NormProtoList);
176  for (int i = 0; i < freeable_protos.size(); ++i) {
177  FreeProtoList(&freeable_protos[i]);
178  }
179  printf ("\n");
180  return 0;
181 } // main
#define ASSERT_HOST(x)
Definition: errcode.h:88
void FreeProtoList(LIST *ProtoList)
Definition: cluster.cpp:538
void FreeClusterer(CLUSTERER *Clusterer)
Definition: cluster.cpp:514
LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
Definition: cluster.cpp:483
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:112
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:270
#define iterate(l)
Definition: oldlist.h:101
#define first_node(l)
Definition: oldlist.h:92
#define NIL_LIST
Definition: oldlist.h:76
#define PROGRAM_FEATURE_TYPE
Definition: cntraining.cpp:34
void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const char *feature_name, int max_samples, UNICHARSET *unicharset, FILE *file, LIST *training_samples)
CLUSTERCONFIG Config
void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, char *CharName)
int NumberOfProtos(LIST ProtoList, bool CountSigProtos, bool CountInsigProtos)
void FreeTrainingSamples(LIST CharList)
void ParseArguments(int *argc, char ***argv)
void FreeNormProtoList(LIST CharList)
const char * GetNextFilename(int argc, const char *const *argv)
CLUSTERER * SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, LABELEDLIST char_sample, const char *program_feature_type)
int push_back(T object)
int size() const
Definition: genericvector.h:72
int MagicSamples
Definition: cluster.h:53
float MinSamples
Definition: cluster.h:48
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:47