2009-07-11 10:51:09 +08:00
|
|
|
// Copyright 2008 Google Inc. All Rights Reserved.
|
|
|
|
// Author: scharron@google.com (Samuel Charron)
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-12-04 21:45:26 +08:00
|
|
|
#ifndef TESSERACT_TRAINING_COMMONTRAINING_H_
|
|
|
|
#define TESSERACT_TRAINING_COMMONTRAINING_H_
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
#include "cluster.h"
|
2013-09-23 23:26:50 +08:00
|
|
|
#include "commandlineflags.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "featdefs.h"
|
2013-09-23 23:26:50 +08:00
|
|
|
#include "intproto.h"
|
|
|
|
#include "oldlist.h"
|
2012-02-02 11:02:16 +08:00
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
class Classify;
|
|
|
|
class MasterTrainer;
|
|
|
|
class ShapeTable;
|
|
|
|
}
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Globals ///////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2012-02-02 11:02:16 +08:00
|
|
|
|
|
|
|
extern FEATURE_DEFS_STRUCT feature_defs;
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
// Must be defined in the file that "implements" commonTraining facilities.
|
|
|
|
extern CLUSTERCONFIG Config;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Structs ///////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *Label;
|
|
|
|
int SampleCount;
|
2010-11-24 02:34:14 +08:00
|
|
|
int font_sample_count;
|
2009-07-11 10:51:09 +08:00
|
|
|
LIST List;
|
|
|
|
}
|
|
|
|
LABELEDLISTNODE, *LABELEDLIST;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char* Label;
|
|
|
|
int NumMerged[MAX_NUM_PROTOS];
|
|
|
|
CLASS_TYPE Class;
|
|
|
|
}MERGE_CLASS_NODE;
|
|
|
|
typedef MERGE_CLASS_NODE* MERGE_CLASS;
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Functions /////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2012-02-02 11:02:16 +08:00
|
|
|
void ParseArguments(int* argc, char*** argv);
|
|
|
|
|
|
|
|
namespace tesseract {
|
2013-09-23 23:26:50 +08:00
|
|
|
|
2012-02-02 11:02:16 +08:00
|
|
|
// Helper loads shape table from the given file.
|
|
|
|
ShapeTable* LoadShapeTable(const STRING& file_prefix);
|
|
|
|
// Helper to write the shape_table.
|
|
|
|
void WriteShapeTable(const STRING& file_prefix, const ShapeTable& shape_table);
|
|
|
|
|
|
|
|
// Creates a MasterTraininer and loads the training data into it:
|
|
|
|
// Initializes feature_defs and IntegerFX.
|
2016-12-13 15:08:01 +08:00
|
|
|
// Loads the shape_table if shape_table != nullptr.
|
2012-02-02 11:02:16 +08:00
|
|
|
// Loads initial unicharset from -U command-line option.
|
|
|
|
// If FLAGS_input_trainer is set, loads the majority of data from there, else:
|
|
|
|
// Loads font info from -F option.
|
|
|
|
// Loads xheights from -X option.
|
|
|
|
// Loads samples from .tr files in remaining command-line args.
|
|
|
|
// Deletes outliers and computes canonical samples.
|
|
|
|
// If FLAGS_output_trainer is set, saves the trainer for future use.
|
|
|
|
// Computes canonical and cloud features.
|
2016-12-13 15:08:01 +08:00
|
|
|
// If shape_table is not nullptr, but failed to load, make a fake flat one,
|
2012-02-02 11:02:16 +08:00
|
|
|
// as shape clustering was not run.
|
|
|
|
MasterTrainer* LoadTrainingData(int argc, const char* const * argv,
|
|
|
|
bool replication,
|
|
|
|
ShapeTable** shape_table,
|
|
|
|
STRING* file_prefix);
|
|
|
|
} // namespace tesseract.
|
|
|
|
|
|
|
|
const char *GetNextFilename(int argc, const char* const * argv);
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
LABELEDLIST FindList(
|
|
|
|
LIST List,
|
|
|
|
char *Label);
|
|
|
|
|
|
|
|
LABELEDLIST NewLabeledList(
|
|
|
|
const char *Label);
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs,
|
|
|
|
const char *feature_name, int max_samples,
|
|
|
|
UNICHARSET* unicharset,
|
|
|
|
FILE* file, LIST* training_samples);
|
|
|
|
|
2009-07-11 10:51:09 +08:00
|
|
|
void WriteTrainingSamples(
|
2010-11-24 02:34:14 +08:00
|
|
|
const FEATURE_DEFS_STRUCT &FeatureDefs,
|
|
|
|
char *Directory,
|
|
|
|
LIST CharList,
|
2009-07-11 10:51:09 +08:00
|
|
|
const char *program_feature_type);
|
|
|
|
|
|
|
|
void FreeTrainingSamples(
|
|
|
|
LIST CharList);
|
|
|
|
|
|
|
|
void FreeLabeledList(
|
|
|
|
LABELEDLIST LabeledList);
|
|
|
|
|
|
|
|
void FreeLabeledClassList(
|
|
|
|
LIST ClassListList);
|
|
|
|
|
|
|
|
CLUSTERER *SetUpForClustering(
|
2010-11-24 02:34:14 +08:00
|
|
|
const FEATURE_DEFS_STRUCT &FeatureDefs,
|
2009-07-11 10:51:09 +08:00
|
|
|
LABELEDLIST CharSample,
|
|
|
|
const char *program_feature_type);
|
|
|
|
|
|
|
|
LIST RemoveInsignificantProtos(
|
|
|
|
LIST ProtoList,
|
|
|
|
BOOL8 KeepSigProtos,
|
|
|
|
BOOL8 KeepInsigProtos,
|
|
|
|
int N);
|
|
|
|
|
|
|
|
void CleanUpUnusedData(
|
|
|
|
LIST ProtoList);
|
|
|
|
|
|
|
|
void MergeInsignificantProtos(
|
|
|
|
LIST ProtoList,
|
|
|
|
const char *label,
|
|
|
|
CLUSTERER *Clusterer,
|
|
|
|
CLUSTERCONFIG *Config);
|
|
|
|
|
|
|
|
MERGE_CLASS FindClass(
|
|
|
|
LIST List,
|
2012-02-02 11:02:16 +08:00
|
|
|
const char *Label);
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
MERGE_CLASS NewLabeledClass(
|
2012-02-02 11:02:16 +08:00
|
|
|
const char *Label);
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
void FreeTrainingSamples(
|
|
|
|
LIST CharList);
|
|
|
|
|
2012-02-02 11:02:16 +08:00
|
|
|
CLASS_STRUCT* SetUpForFloat2Int(const UNICHARSET& unicharset,
|
|
|
|
LIST LabeledClassList);
|
2009-07-11 10:51:09 +08:00
|
|
|
|
|
|
|
void Normalize(
|
|
|
|
float *Values);
|
|
|
|
|
|
|
|
void FreeNormProtoList(
|
|
|
|
LIST CharList);
|
|
|
|
|
|
|
|
void AddToNormProtosList(
|
|
|
|
LIST* NormProtoList,
|
|
|
|
LIST ProtoList,
|
|
|
|
char *CharName);
|
|
|
|
|
|
|
|
int NumberOfProtos(
|
|
|
|
LIST ProtoList,
|
|
|
|
BOOL8 CountSigProtos,
|
|
|
|
BOOL8 CountInsigProtos);
|
|
|
|
|
|
|
|
|
|
|
|
void allocNormProtos();
|
2016-12-04 21:45:26 +08:00
|
|
|
#endif // TESSERACT_TRAINING_COMMONTRAINING_H_
|