tesseract  4.0.0-beta.1-59-g2cc4
combine_tessdata.cpp File Reference
#include "lstmrecognizer.h"
#include "tessdatamanager.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

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
none
Note
Exceptions: none
History: Fri Aug 18 08:56:17 1989, DSJ, Created.
History: Mon May 18 1998, Christy Russson, Revistion started.

Definition at line 67 of file combine_tessdata.cpp.

67  {
68  int i;
70  if (argc == 2) {
71  printf("Combining tessdata files\n");
72  STRING lang = argv[1];
73  char* last = &argv[1][strlen(argv[1])-1];
74  if (*last != '.')
75  lang += '.';
76  STRING output_file = lang;
77  output_file += kTrainedDataSuffix;
78  if (!tm.CombineDataFiles(lang.string(), output_file.string())) {
79  printf("Error combining tessdata files into %s\n",
80  output_file.string());
81  } else {
82  printf("Output %s created successfully.\n", output_file.string());
83  }
84  } else if (argc >= 4 && (strcmp(argv[1], "-e") == 0 ||
85  strcmp(argv[1], "-u") == 0)) {
86  // Initialize TessdataManager with the data in the given traineddata file.
87  if (!tm.Init(argv[2])) {
88  tprintf("Failed to read %s\n", argv[2]);
89  exit(1);
90  }
91  printf("Extracting tessdata components from %s\n", argv[2]);
92  if (strcmp(argv[1], "-e") == 0) {
93  for (i = 3; i < argc; ++i) {
94  if (tm.ExtractToFile(argv[i])) {
95  printf("Wrote %s\n", argv[i]);
96  } else {
97  printf("Not extracting %s, since this component"
98  " is not present\n", argv[i]);
99  }
100  }
101  } else { // extract all the components
102  for (i = 0; i < tesseract::TESSDATA_NUM_ENTRIES; ++i) {
103  STRING filename = argv[3];
104  char* last = &argv[3][strlen(argv[3])-1];
105  if (*last != '.')
106  filename += '.';
107  filename += tesseract::kTessdataFileSuffixes[i];
108  if (tm.ExtractToFile(filename.string())) {
109  printf("Wrote %s\n", filename.string());
110  }
111  }
112  }
113  } else if (argc >= 4 && strcmp(argv[1], "-o") == 0) {
114  // Rename the current traineddata file to a temporary name.
115  const char *new_traineddata_filename = argv[2];
116  STRING traineddata_filename = new_traineddata_filename;
117  traineddata_filename += ".__tmp__";
118  if (rename(new_traineddata_filename, traineddata_filename.string()) != 0) {
119  tprintf("Failed to create a temporary file %s\n",
120  traineddata_filename.string());
121  exit(1);
122  }
123 
124  // Initialize TessdataManager with the data in the given traineddata file.
125  tm.Init(traineddata_filename.string());
126 
127  // Write the updated traineddata file.
128  tm.OverwriteComponents(new_traineddata_filename, argv+3, argc-3);
129  } else if (argc == 3 && strcmp(argv[1], "-c") == 0) {
130  if (!tm.Init(argv[2])) {
131  tprintf("Failed to read %s\n", argv[2]);
132  exit(1);
133  }
134  tesseract::TFile fp;
135  if (!tm.GetComponent(tesseract::TESSDATA_LSTM, &fp)) {
136  tprintf("No LSTM Component found in %s!\n", argv[2]);
137  exit(1);
138  }
139  tesseract::LSTMRecognizer recognizer;
140  if (!recognizer.DeSerialize(&tm, &fp)) {
141  tprintf("Failed to deserialize LSTM in %s!\n", argv[2]);
142  exit(1);
143  }
144  recognizer.ConvertToInt();
145  GenericVector<char> lstm_data;
146  fp.OpenWrite(&lstm_data);
147  ASSERT_HOST(recognizer.Serialize(&tm, &fp));
148  tm.OverwriteEntry(tesseract::TESSDATA_LSTM, &lstm_data[0],
149  lstm_data.size());
150  if (!tm.SaveFile(argv[2], nullptr)) {
151  tprintf("Failed to write modified traineddata:%s!\n", argv[2]);
152  exit(1);
153  }
154  } else if (argc == 3 && strcmp(argv[1], "-d") == 0) {
155  // Initialize TessdataManager with the data in the given traineddata file.
156  tm.Init(argv[2]);
157  } else {
158  printf("Usage for combining tessdata components:\n"
159  " %s language_data_path_prefix\n"
160  " (e.g. %s tessdata/eng.)\n\n", argv[0], argv[0]);
161  printf("Usage for extracting tessdata components:\n"
162  " %s -e traineddata_file [output_component_file...]\n"
163  " (e.g. %s -e eng.traineddata eng.unicharset)\n\n",
164  argv[0], argv[0]);
165  printf("Usage for overwriting tessdata components:\n"
166  " %s -o traineddata_file [input_component_file...]\n"
167  " (e.g. %s -o eng.traineddata eng.unicharset)\n\n",
168  argv[0], argv[0]);
169  printf("Usage for unpacking all tessdata components:\n"
170  " %s -u traineddata_file output_path_prefix\n"
171  " (e.g. %s -u eng.traineddata tmp/eng.)\n", argv[0], argv[0]);
172  printf(
173  "Usage for listing directory of components:\n"
174  " %s -d traineddata_file\n",
175  argv[0]);
176  printf(
177  "Usage for compacting LSTM component to int:\n"
178  " %s -c traineddata_file\n",
179  argv[0]);
180  return 1;
181  }
182  tm.Directory();
183 }
bool ExtractToFile(const char *filename)
bool Init(const char *data_file_name)
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:129
Definition: strngs.h:45
bool SaveFile(const STRING &filename, FileWriter writer) const
int size() const
Definition: genericvector.h:72
bool GetComponent(TessdataType type, TFile *fp)
#define ASSERT_HOST(x)
Definition: errcode.h:84
LIST last(LIST var_list)
Definition: oldlist.cpp:247
bool Serialize(const TessdataManager *mgr, TFile *fp) const
#define tprintf(...)
Definition: tprintf.h:31
void OverwriteEntry(TessdataType type, const char *data, int size)
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
bool OverwriteComponents(const char *new_traineddata_filename, char **component_filenames, int num_new_components)
bool CombineDataFiles(const char *language_data_path_prefix, const char *output_filename)
const char * string() const
Definition: strngs.cpp:198