tesseract  5.0.0-alpha-619-ge9db
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 <tesseract/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
ReadTrainingSamples
void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const char *feature_name, int max_samples, UNICHARSET *unicharset, FILE *file, LIST *training_samples)
Definition: commontraining.cpp:389
CLUSTERCONFIG::MagicSamples
int MagicSamples
Definition: cluster.h:52
InitFeatureDefs
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:111
NumberOfProtos
int NumberOfProtos(LIST ProtoList, bool CountSigProtos, bool CountInsigProtos)
Definition: commontraining.cpp:839
FreeClusterer
void FreeClusterer(CLUSTERER *Clusterer)
Definition: cluster.cpp:514
first_node
#define first_node(l)
Definition: oldlist.h:84
ShortNameToFeatureType
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:269
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
list_rec
Definition: oldlist.h:73
FreeProtoList
void FreeProtoList(LIST *ProtoList)
Definition: cluster.cpp:538
Config
CLUSTERCONFIG Config
Definition: commontraining.cpp:88
NIL_LIST
#define NIL_LIST
Definition: oldlist.h:68
FEATURE_DEFS_STRUCT::FeatureDesc
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:46
ClusterSamples
LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
Definition: cluster.cpp:483
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
FEATURE_DEFS_STRUCT
Definition: featdefs.h:44
GetNextFilename
const char * GetNextFilename(int argc, const char *const *argv)
Definition: commontraining.cpp:323
PROGRAM_FEATURE_TYPE
#define PROGRAM_FEATURE_TYPE
Definition: cntraining.cpp:34
FreeTrainingSamples
void FreeTrainingSamples(LIST CharList)
Definition: commontraining.cpp:450
GenericVector
Definition: baseapi.h:40
LABELEDLISTNODE
Definition: commontraining.h:78
SetUpForClustering
CLUSTERER * SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, LABELEDLIST char_sample, const char *program_feature_type)
Definition: commontraining.cpp:494
ParseArguments
void ParseArguments(int *argc, char ***argv)
Definition: commontraining.cpp:122
CLUSTERER
Definition: cluster.h:81
iterate
#define iterate(l)
Definition: oldlist.h:92
LABELEDLISTNODE::SampleCount
int SampleCount
Definition: commontraining.h:81
GenericVector::size
int size() const
Definition: genericvector.h:71
CLUSTERCONFIG::MinSamples
float MinSamples
Definition: cluster.h:47
AddToNormProtosList
void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, char *CharName)
Definition: commontraining.cpp:821
LABELEDLISTNODE::Label
char * Label
Definition: commontraining.h:80
FreeNormProtoList
void FreeNormProtoList(LIST CharList)
Definition: commontraining.cpp:805