tesseract  4.0.0-1-g2a2b
cntraining.cpp File Reference
#include "oldlist.h"
#include "emalloc.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

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

Variables

CLUSTERCONFIG CNConfig
 

Macro Definition Documentation

◆ PROGRAM_FEATURE_TYPE

#define PROGRAM_FEATURE_TYPE   "cn"

Definition at line 39 of file cntraining.cpp.

Function Documentation

◆ DECLARE_STRING_PARAM_FLAG()

DECLARE_STRING_PARAM_FLAG ( )

◆ 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
none
Note
Globals: none

Definition at line 113 of file cntraining.cpp.

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

Variable Documentation

◆ CNConfig

CLUSTERCONFIG CNConfig
Initial value:
=
{
elliptical, 0.025, 0.05, 0.8, 1e-3, 0
}

Definition at line 58 of file cntraining.cpp.