tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
tessdatamanager.h
1 // File: tessdatamanager.h
3 // Description: Functions to handle loading/combining tesseract data files.
4 // Author: Daria Antonova
5 // Created: Wed Jun 03 11:26:43 PST 2009
6 //
7 // (C) Copyright 2009, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifndef TESSERACT_CCUTIL_TESSDATAMANAGER_H_
21 #define TESSERACT_CCUTIL_TESSDATAMANAGER_H_
22 
23 #include "genericvector.h"
24 
25 static const char kTrainedDataSuffix[] = "traineddata";
26 
27 // When adding new tessdata types and file suffixes, please make sure to
28 // update TessdataType enum, kTessdataFileSuffixes and kTessdataFileIsText.
29 static const char kLangConfigFileSuffix[] = "config";
30 static const char kUnicharsetFileSuffix[] = "unicharset";
31 static const char kAmbigsFileSuffix[] = "unicharambigs";
32 static const char kBuiltInTemplatesFileSuffix[] = "inttemp";
33 static const char kBuiltInCutoffsFileSuffix[] = "pffmtable";
34 static const char kNormProtoFileSuffix[] = "normproto";
35 static const char kPuncDawgFileSuffix[] = "punc-dawg";
36 static const char kSystemDawgFileSuffix[] = "word-dawg";
37 static const char kNumberDawgFileSuffix[] = "number-dawg";
38 static const char kFreqDawgFileSuffix[] = "freq-dawg";
39 static const char kFixedLengthDawgsFileSuffix[] = "fixed-length-dawgs";
40 static const char kCubeUnicharsetFileSuffix[] = "cube-unicharset";
41 static const char kCubeSystemDawgFileSuffix[] = "cube-word-dawg";
42 static const char kShapeTableFileSuffix[] = "shapetable";
43 static const char kBigramDawgFileSuffix[] = "bigram-dawg";
44 static const char kUnambigDawgFileSuffix[] = "unambig-dawg";
45 static const char kParamsModelFileSuffix[] = "params-model";
46 static const char kLSTMModelFileSuffix[] = "lstm";
47 static const char kLSTMPuncDawgFileSuffix[] = "lstm-punc-dawg";
48 static const char kLSTMSystemDawgFileSuffix[] = "lstm-word-dawg";
49 static const char kLSTMNumberDawgFileSuffix[] = "lstm-number-dawg";
50 static const char kLSTMUnicharsetFileSuffix[] = "lstm-unicharset";
51 static const char kLSTMRecoderFileSuffix[] = "lstm-recoder";
52 static const char kVersionFileSuffix[] = "version";
53 
54 namespace tesseract {
55 
67  TESSDATA_FIXED_LENGTH_DAWGS, // 10 // deprecated
68  TESSDATA_CUBE_UNICHARSET, // 11 // deprecated
69  TESSDATA_CUBE_SYSTEM_DAWG, // 12 // deprecated
81 
83 };
84 
89 static const char *const kTessdataFileSuffixes[] = {
90  kLangConfigFileSuffix, // 0
91  kUnicharsetFileSuffix, // 1
92  kAmbigsFileSuffix, // 2
93  kBuiltInTemplatesFileSuffix, // 3
94  kBuiltInCutoffsFileSuffix, // 4
95  kNormProtoFileSuffix, // 5
96  kPuncDawgFileSuffix, // 6
97  kSystemDawgFileSuffix, // 7
98  kNumberDawgFileSuffix, // 8
99  kFreqDawgFileSuffix, // 9
100  kFixedLengthDawgsFileSuffix, // 10 // deprecated
101  kCubeUnicharsetFileSuffix, // 11 // deprecated
102  kCubeSystemDawgFileSuffix, // 12 // deprecated
103  kShapeTableFileSuffix, // 13
104  kBigramDawgFileSuffix, // 14
105  kUnambigDawgFileSuffix, // 15
106  kParamsModelFileSuffix, // 16
107  kLSTMModelFileSuffix, // 17
108  kLSTMPuncDawgFileSuffix, // 18
109  kLSTMSystemDawgFileSuffix, // 19
110  kLSTMNumberDawgFileSuffix, // 20
111  kLSTMUnicharsetFileSuffix, // 21
112  kLSTMRecoderFileSuffix, // 22
113  kVersionFileSuffix, // 23
114 };
115 
123 static const int kMaxNumTessdataEntries = 1000;
124 
125 
127  public:
128  TessdataManager();
129  explicit TessdataManager(FileReader reader);
130 
131  ~TessdataManager() = default;
132 
133  bool swap() const { return swap_; }
134  bool is_loaded() const { return is_loaded_; }
135 
136  // Lazily loads from the the given filename. Won't actually read the file
137  // until it needs it.
138  void LoadFileLater(const char *data_file_name);
143  bool Init(const char *data_file_name);
144  // Loads from the given memory buffer as if a file, remembering name as some
145  // arbitrary source id for caching.
146  bool LoadMemBuffer(const char *name, const char *data, int size);
147  // Overwrites a single entry of the given type.
148  void OverwriteEntry(TessdataType type, const char *data, int size);
149 
150  // Saves to the given filename.
151  bool SaveFile(const STRING &filename, FileWriter writer) const;
152  // Serializes to the given vector.
153  void Serialize(GenericVector<char> *data) const;
154  // Resets to the initial state, keeping the reader.
155  void Clear();
156 
157  // Prints a directory of contents.
158  void Directory() const;
159 
160  // Returns true if the component requested is present.
162  return !entries_[type].empty();
163  }
164  // Opens the given TFile pointer to the given component type.
165  // Returns false in case of failure.
166  bool GetComponent(TessdataType type, TFile *fp);
167  // As non-const version except it can't load the component if not already
168  // loaded.
169  bool GetComponent(TessdataType type, TFile *fp) const;
170 
171  // Returns the current version string.
172  std::string VersionString() const;
173  // Sets the version string to the given v_str.
174  void SetVersionString(const std::string &v_str);
175 
176  // Returns true if the base Tesseract components are present.
177  bool IsBaseAvailable() const {
178  return !entries_[TESSDATA_UNICHARSET].empty() &&
180  }
181 
182  // Returns true if the LSTM components are present.
183  bool IsLSTMAvailable() const { return !entries_[TESSDATA_LSTM].empty(); }
184 
185  // Return the name of the underlying data file.
186  const STRING &GetDataFileName() const { return data_file_name_; }
187 
193  bool CombineDataFiles(const char *language_data_path_prefix,
194  const char *output_filename);
195 
201  bool OverwriteComponents(const char *new_traineddata_filename,
202  char **component_filenames,
203  int num_new_components);
204 
215  bool ExtractToFile(const char *filename);
216 
223  static bool TessdataTypeFromFileSuffix(const char *suffix,
224  TessdataType *type);
225 
230  static bool TessdataTypeFromFileName(const char *filename,
231  TessdataType *type);
232 
233  private:
234  // Name of file it came from.
236  // Function to load the file when we need it.
238  // True if the file has been loaded.
240  // True if the bytes need swapping.
241  bool swap_;
242  // Contents of each element of the traineddata file.
244 };
245 
246 } // namespace tesseract
247 
248 #endif // TESSERACT_CCUTIL_TESSDATAMANAGER_H_
Definition: tessdatamanager.h:82
Definition: tessdatamanager.h:67
void OverwriteEntry(TessdataType type, const char *data, int size)
Definition: tessdatamanager.cpp:99
bool swap() const
Definition: tessdatamanager.h:133
static const char *const kTessdataFileSuffixes[]
Definition: tessdatamanager.h:89
Definition: tessdatamanager.h:70
Definition: tessdatamanager.h:77
bool OverwriteComponents(const char *new_traineddata_filename, char **component_filenames, int num_new_components)
Definition: tessdatamanager.cpp:227
bool Init(const char *data_file_name)
Definition: tessdatamanager.cpp:55
bool is_loaded_
Definition: tessdatamanager.h:239
void Directory() const
Definition: tessdatamanager.cpp:154
std::string VersionString() const
Definition: tessdatamanager.cpp:185
Definition: tessdatamanager.h:57
Definition: tessdatamanager.h:78
TessdataManager()
Definition: tessdatamanager.cpp:37
bool GetComponent(TessdataType type, TFile *fp)
Definition: tessdatamanager.cpp:168
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
Definition: genericvector.h:360
Definition: serialis.h:77
Definition: tessdatamanager.h:73
Definition: tessdatamanager.h:69
FileReader reader_
Definition: tessdatamanager.h:237
Definition: baseapi.cpp:94
bool IsComponentAvailable(TessdataType type) const
Definition: tessdatamanager.h:161
Definition: tessdatamanager.h:59
Definition: tessdatamanager.h:64
bool LoadMemBuffer(const char *name, const char *data, int size)
Definition: tessdatamanager.cpp:66
void Clear()
Definition: tessdatamanager.cpp:146
Definition: tessdatamanager.h:58
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
Definition: genericvector.h:363
Definition: tessdatamanager.h:80
STRING data_file_name_
Definition: tessdatamanager.h:235
GenericVector< char > entries_[TESSDATA_NUM_ENTRIES]
Definition: tessdatamanager.h:243
Definition: tessdatamanager.h:72
Definition: tessdatamanager.h:74
bool swap_
Definition: tessdatamanager.h:241
Definition: tessdatamanager.h:75
Definition: tessdatamanager.h:126
Definition: tessdatamanager.h:66
TessdataType
Definition: tessdatamanager.h:56
bool IsBaseAvailable() const
Definition: tessdatamanager.h:177
Definition: strngs.h:45
void SetVersionString(const std::string &v_str)
Definition: tessdatamanager.cpp:191
void Serialize(GenericVector< char > *data) const
Definition: tessdatamanager.cpp:119
bool SaveFile(const STRING &filename, FileWriter writer) const
Definition: tessdatamanager.cpp:107
bool ExtractToFile(const char *filename)
Definition: tessdatamanager.cpp:246
Definition: tessdatamanager.h:61
const STRING & GetDataFileName() const
Definition: tessdatamanager.h:186
static bool TessdataTypeFromFileName(const char *filename, TessdataType *type)
Definition: tessdatamanager.cpp:267
Definition: tessdatamanager.h:62
Definition: tessdatamanager.h:68
bool is_loaded() const
Definition: tessdatamanager.h:134
Definition: tessdatamanager.h:63
Definition: tessdatamanager.h:71
Definition: tessdatamanager.h:76
Definition: tessdatamanager.h:79
static const int kMaxNumTessdataEntries
Definition: tessdatamanager.h:123
bool empty() const
Definition: genericvector.h:90
bool IsLSTMAvailable() const
Definition: tessdatamanager.h:183
Definition: tessdatamanager.h:60
static bool TessdataTypeFromFileSuffix(const char *suffix, TessdataType *type)
Definition: tessdatamanager.cpp:254
bool CombineDataFiles(const char *language_data_path_prefix, const char *output_filename)
Definition: tessdatamanager.cpp:196
void LoadFileLater(const char *data_file_name)
Definition: tessdatamanager.cpp:50
Definition: tessdatamanager.h:65