tesseract  4.00.00dev
tesseractclass.h
Go to the documentation of this file.
1 // File: tesseractclass.h
3 // Description: The Tesseract class. It holds/owns everything needed
4 // to run Tesseract on a single language, and also a set of
5 // sub-Tesseracts to run sub-languages. For thread safety, *every*
6 // global variable goes in here, directly, or indirectly.
7 // This makes it safe to run multiple Tesseracts in different
8 // threads in parallel, and keeps the different language
9 // instances separate.
10 // Author: Ray Smith
11 // Created: Fri Mar 07 08:17:01 PST 2008
12 //
13 // (C) Copyright 2008, Google Inc.
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 // http://www.apache.org/licenses/LICENSE-2.0
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
23 //
25 
26 #ifndef TESSERACT_CCMAIN_TESSERACTCLASS_H_
27 #define TESSERACT_CCMAIN_TESSERACTCLASS_H_
28 
29 #include "allheaders.h"
30 #include "control.h"
31 #include "debugpixa.h"
32 #include "devanagari_processing.h"
33 #include "docqual.h"
34 #include "genericvector.h"
35 #include "ocrclass.h"
36 #include "params.h"
37 #include "textord.h"
38 #include "wordrec.h"
39 
40 class BLOB_CHOICE_LIST_CLIST;
41 class BLOCK_LIST;
42 struct OSResults;
43 class PAGE_RES;
44 class PAGE_RES_IT;
45 struct Pix;
46 class ROW;
47 class SVMenuNode;
48 class TBOX;
49 class TO_BLOCK_LIST;
50 class WERD;
51 class WERD_CHOICE;
52 class WERD_RES;
53 
54 
55 // Top-level class for all tesseract global instance data.
56 // This class either holds or points to all data used by an instance
57 // of Tesseract, including the memory allocator. When this is
58 // complete, Tesseract will be thread-safe. UNTIL THEN, IT IS NOT!
59 //
60 // NOTE to developers: Do not create cyclic dependencies through this class!
61 // The directory dependency tree must remain a tree! The keep this clean,
62 // lower-level code (eg in ccutil, the bottom level) must never need to
63 // know about the content of a higher-level directory.
64 // The following scheme will grant the easiest access to lower-level
65 // global members without creating a cyclic dependency:
66 //
67 // Class Hierarchy (^ = inheritance):
68 //
69 // CCUtil (ccutil/ccutil.h)
70 // ^ Members include: UNICHARSET
71 // CUtil (cutil/cutil_class.h)
72 // ^ Members include: TBLOB*, TEXTBLOCK*
73 // CCStruct (ccstruct/ccstruct.h)
74 // ^ Members include: Image
75 // Classify (classify/classify.h)
76 // ^ Members include: Dict
77 // WordRec (wordrec/wordrec.h)
78 // ^ Members include: WERD*, DENORM*
79 // Tesseract (ccmain/tesseractclass.h)
80 // Members include: Pix*
81 //
82 // Other important classes:
83 //
84 // TessBaseAPI (api/baseapi.h)
85 // Members include: BLOCK_LIST*, PAGE_RES*,
86 // Tesseract*, ImageThresholder*
87 // Dict (dict/dict.h)
88 // Members include: Image* (private)
89 //
90 // NOTE: that each level contains members that correspond to global
91 // data that is defined (and used) at that level, not necessarily where
92 // the type is defined so for instance:
93 // BOOL_VAR_H(textord_show_blobs, false, "Display unsorted blobs");
94 // goes inside the Textord class, not the cc_util class.
95 
96 namespace tesseract {
97 
98 class ColumnFinder;
99 class DocumentData;
100 class EquationDetect;
101 class ImageData;
102 class LSTMRecognizer;
103 class Tesseract;
104 
105 // A collection of various variables for statistics and debugging.
109  doc_blob_quality(0),
110  doc_outline_errs(0),
111  doc_char_quality(0),
112  good_char_count(0),
114  word_count(0),
115  dict_words(0),
116  tilde_crunch_written(false),
117  last_char_was_newline(true),
118  last_char_was_tilde(false),
120 
127  inT32 word_count; // count of word in the document
128  inT32 dict_words; // number of dicitionary words in the document
129  STRING dump_words_str; // accumulator used by dump_words()
130  // Flags used by write_results()
135 };
136 
137 // Struct to hold all the pointers to relevant data for processing a word.
138 struct WordData {
139  WordData() : word(NULL), row(NULL), block(NULL), prev_word(NULL) {}
140  explicit WordData(const PAGE_RES_IT& page_res_it)
141  : word(page_res_it.word()), row(page_res_it.row()->row),
142  block(page_res_it.block()->block), prev_word(NULL) {}
143  WordData(BLOCK* block_in, ROW* row_in, WERD_RES* word_res)
144  : word(word_res), row(row_in), block(block_in), prev_word(NULL) {}
145 
151 };
152 
153 // Definition of a Tesseract WordRecognizer. The WordData provides the context
154 // of row/block, in_word holds an initialized, possibly pre-classified word,
155 // that the recognizer may or may not consume (but if so it sets *in_word=NULL)
156 // and produces one or more output words in out_words, which may be the
157 // consumed in_word, or may be generated independently.
158 // This api allows both a conventional tesseract classifier to work, or a
159 // line-level classifier that generates multiple words from a merged input.
160 typedef void (Tesseract::*WordRecognizer)(const WordData& word_data,
161  WERD_RES** in_word,
162  PointerVector<WERD_RES>* out_words);
163 
164 class Tesseract : public Wordrec {
165  public:
166  Tesseract();
167  ~Tesseract();
168 
169  // Clear as much used memory as possible without resetting the adaptive
170  // classifier or losing any other classifier data.
171  void Clear();
172  // Clear all memory of adaption for this and all subclassifiers.
173  void ResetAdaptiveClassifier();
174  // Clear the document dictionary for this and all subclassifiers.
175  void ResetDocumentDictionary();
176 
177  // Set the equation detector.
178  void SetEquationDetect(EquationDetect* detector);
179 
180  // Simple accessors.
181  const FCOORD& reskew() const {
182  return reskew_;
183  }
184  // Destroy any existing pix and return a pointer to the pointer.
186  pixDestroy(&pix_binary_);
187  return &pix_binary_;
188  }
189  Pix* pix_binary() const {
190  return pix_binary_;
191  }
192  Pix* pix_grey() const {
193  return pix_grey_;
194  }
195  void set_pix_grey(Pix* grey_pix) {
196  pixDestroy(&pix_grey_);
197  pix_grey_ = grey_pix;
198  }
199  Pix* pix_original() const { return pix_original_; }
200  // Takes ownership of the given original_pix.
201  void set_pix_original(Pix* original_pix) {
202  pixDestroy(&pix_original_);
203  pix_original_ = original_pix;
204  // Clone to sublangs as well.
205  for (int i = 0; i < sub_langs_.size(); ++i)
206  sub_langs_[i]->set_pix_original(original_pix ? pixClone(original_pix)
207  : nullptr);
208  }
209  // Returns a pointer to a Pix representing the best available (original) image
210  // of the page. Can be of any bit depth, but never color-mapped, as that has
211  // always been dealt with. Note that in grey and color, 0 is black and 255 is
212  // white. If the input was binary, then black is 1 and white is 0.
213  // To tell the difference pixGetDepth() will return 32, 8 or 1.
214  // In any case, the return value is a borrowed Pix, and should not be
215  // deleted or pixDestroyed.
216  Pix* BestPix() const { return pix_original_; }
217  void set_pix_thresholds(Pix* thresholds) {
218  pixDestroy(&pix_thresholds_);
219  pix_thresholds_ = thresholds;
220  }
221  int source_resolution() const {
222  return source_resolution_;
223  }
224  void set_source_resolution(int ppi) {
225  source_resolution_ = ppi;
226  }
227  int ImageWidth() const {
228  return pixGetWidth(pix_binary_);
229  }
230  int ImageHeight() const {
231  return pixGetHeight(pix_binary_);
232  }
233  Pix* scaled_color() const {
234  return scaled_color_;
235  }
236  int scaled_factor() const {
237  return scaled_factor_;
238  }
239  void SetScaledColor(int factor, Pix* color) {
240  scaled_factor_ = factor;
241  scaled_color_ = color;
242  }
243  const Textord& textord() const {
244  return textord_;
245  }
247  return &textord_;
248  }
249 
250  bool right_to_left() const {
251  return right_to_left_;
252  }
253  int num_sub_langs() const {
254  return sub_langs_.size();
255  }
256  Tesseract* get_sub_lang(int index) const {
257  return sub_langs_[index];
258  }
259  // Returns true if any language uses Tesseract (as opposed to LSTM).
260  bool AnyTessLang() const {
261  if (tessedit_ocr_engine_mode != OEM_LSTM_ONLY) return true;
262  for (int i = 0; i < sub_langs_.size(); ++i) {
263  if (sub_langs_[i]->tessedit_ocr_engine_mode != OEM_LSTM_ONLY) return true;
264  }
265  return false;
266  }
267  // Returns true if any language uses the LSTM.
268  bool AnyLSTMLang() const {
269  if (tessedit_ocr_engine_mode != OEM_TESSERACT_ONLY) return true;
270  for (int i = 0; i < sub_langs_.size(); ++i) {
271  if (sub_langs_[i]->tessedit_ocr_engine_mode != OEM_TESSERACT_ONLY)
272  return true;
273  }
274  return false;
275  }
276 
277  void SetBlackAndWhitelist();
278 
279  // Perform steps to prepare underlying binary image/other data structures for
280  // page segmentation. Uses the strategy specified in the global variable
281  // pageseg_devanagari_split_strategy for perform splitting while preparing for
282  // page segmentation.
283  void PrepareForPageseg();
284 
285  // Perform steps to prepare underlying binary image/other data structures for
286  // Tesseract OCR. The current segmentation is required by this method.
287  // Uses the strategy specified in the global variable
288  // ocr_devanagari_split_strategy for performing splitting while preparing for
289  // Tesseract ocr.
290  void PrepareForTessOCR(BLOCK_LIST* block_list,
291  Tesseract* osd_tess, OSResults* osr);
292 
293  int SegmentPage(const STRING* input_file, BLOCK_LIST* blocks,
294  Tesseract* osd_tess, OSResults* osr);
295  void SetupWordScripts(BLOCK_LIST* blocks);
296  int AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST* blocks,
297  TO_BLOCK_LIST* to_blocks, BLOBNBOX_LIST* diacritic_blobs,
298  Tesseract* osd_tess, OSResults* osr);
299  ColumnFinder* SetupPageSegAndDetectOrientation(
300  PageSegMode pageseg_mode, BLOCK_LIST* blocks, Tesseract* osd_tess,
301  OSResults* osr, TO_BLOCK_LIST* to_blocks, Pix** photo_mask_pix,
302  Pix** music_mask_pix);
303  // par_control.cpp
304  void PrerecAllWordsPar(const GenericVector<WordData>& words);
305 
307  // Generates training data for training a line recognizer, eg LSTM.
308  // Breaks the page into lines, according to the boxes, and writes them to a
309  // serialized DocumentData based on output_basename.
310  void TrainLineRecognizer(const STRING& input_imagename,
311  const STRING& output_basename,
312  BLOCK_LIST *block_list);
313  // Generates training data for training a line recognizer, eg LSTM.
314  // Breaks the boxes into lines, normalizes them, converts to ImageData and
315  // appends them to the given training_data.
316  void TrainFromBoxes(const GenericVector<TBOX>& boxes,
317  const GenericVector<STRING>& texts,
318  BLOCK_LIST *block_list,
319  DocumentData* training_data);
320 
321  // Returns an Imagedata containing the image of the given textline,
322  // and ground truth boxes/truth text if available in the input.
323  // The image is not normalized in any way.
324  ImageData* GetLineData(const TBOX& line_box,
325  const GenericVector<TBOX>& boxes,
326  const GenericVector<STRING>& texts,
327  int start_box, int end_box,
328  const BLOCK& block);
329  // Helper gets the image of a rectangle, using the block.re_rotation() if
330  // needed to get to the image, and rotating the result back to horizontal
331  // layout. (CJK characters will be on their left sides) The vertical text flag
332  // is set in the returned ImageData if the text was originally vertical, which
333  // can be used to invoke a different CJK recognition engine. The revised_box
334  // is also returned to enable calculation of output bounding boxes.
335  ImageData* GetRectImage(const TBOX& box, const BLOCK& block, int padding,
336  TBOX* revised_box) const;
337  // Recognizes a word or group of words, converting to WERD_RES in *words.
338  // Analogous to classify_word_pass1, but can handle a group of words as well.
339  void LSTMRecognizeWord(const BLOCK& block, ROW *row, WERD_RES *word,
340  PointerVector<WERD_RES>* words);
341  // Apply segmentation search to the given set of words, within the constraints
342  // of the existing ratings matrix. If there is already a best_choice on a word
343  // leaves it untouched and just sets the done/accepted etc flags.
344  void SearchWords(PointerVector<WERD_RES>* words);
345 
347  bool ProcessTargetWord(const TBOX& word_box, const TBOX& target_word_box,
348  const char* word_config, int pass);
349  // Sets up the words ready for whichever engine is to be run
350  void SetupAllWordsPassN(int pass_n,
351  const TBOX* target_word_box,
352  const char* word_config,
353  PAGE_RES* page_res,
354  GenericVector<WordData>* words);
355  // Sets up the single word ready for whichever engine is to be run.
356  void SetupWordPassN(int pass_n, WordData* word);
357  // Runs word recognition on all the words.
358  bool RecogAllWordsPassN(int pass_n, ETEXT_DESC* monitor,
359  PAGE_RES_IT* pr_it,
360  GenericVector<WordData>* words);
361  bool recog_all_words(PAGE_RES* page_res,
362  ETEXT_DESC* monitor,
363  const TBOX* target_word_box,
364  const char* word_config,
365  int dopasses);
366  void rejection_passes(PAGE_RES* page_res,
367  ETEXT_DESC* monitor,
368  const TBOX* target_word_box,
369  const char* word_config);
370  void bigram_correction_pass(PAGE_RES *page_res);
371  void blamer_pass(PAGE_RES* page_res);
372  // Sets script positions and detects smallcaps on all output words.
373  void script_pos_pass(PAGE_RES* page_res);
374  // Helper to recognize the word using the given (language-specific) tesseract.
375  // Returns positive if this recognizer found more new best words than the
376  // number kept from best_words.
377  int RetryWithLanguage(const WordData& word_data, WordRecognizer recognizer,
378  bool debug, WERD_RES** in_word,
379  PointerVector<WERD_RES>* best_words);
380  // Moves good-looking "noise"/diacritics from the reject list to the main
381  // blob list on the current word. Returns true if anything was done, and
382  // sets make_next_word_fuzzy if blob(s) were added to the end of the word.
383  bool ReassignDiacritics(int pass, PAGE_RES_IT* pr_it,
384  bool* make_next_word_fuzzy);
385  // Attempts to put noise/diacritic outlines into the blobs that they overlap.
386  // Input: a set of noisy outlines that probably belong to the real_word.
387  // Output: outlines that overlapped blobs are set to NULL and put back into
388  // the word, either in the blobs or in the reject list.
389  void AssignDiacriticsToOverlappingBlobs(
390  const GenericVector<C_OUTLINE*>& outlines, int pass, WERD* real_word,
391  PAGE_RES_IT* pr_it, GenericVector<bool>* word_wanted,
392  GenericVector<bool>* overlapped_any_blob,
393  GenericVector<C_BLOB*>* target_blobs);
394  // Attempts to assign non-overlapping outlines to their nearest blobs or
395  // make new blobs out of them.
396  void AssignDiacriticsToNewBlobs(const GenericVector<C_OUTLINE*>& outlines,
397  int pass, WERD* real_word, PAGE_RES_IT* pr_it,
398  GenericVector<bool>* word_wanted,
399  GenericVector<C_BLOB*>* target_blobs);
400  // Starting with ok_outlines set to indicate which outlines overlap the blob,
401  // chooses the optimal set (approximately) and returns true if any outlines
402  // are desired, in which case ok_outlines indicates which ones.
403  bool SelectGoodDiacriticOutlines(int pass, float certainty_threshold,
404  PAGE_RES_IT* pr_it, C_BLOB* blob,
405  const GenericVector<C_OUTLINE*>& outlines,
406  int num_outlines,
407  GenericVector<bool>* ok_outlines);
408  // Classifies the given blob plus the outlines flagged by ok_outlines, undoes
409  // the inclusion of the outlines, and returns the certainty of the raw choice.
410  float ClassifyBlobPlusOutlines(const GenericVector<bool>& ok_outlines,
411  const GenericVector<C_OUTLINE*>& outlines,
412  int pass_n, PAGE_RES_IT* pr_it, C_BLOB* blob,
413  STRING* best_str);
414  // Classifies the given blob (part of word_data->word->word) as an individual
415  // word, using languages, chopper etc, returning only the certainty of the
416  // best raw choice, and undoing all the work done to fake out the word.
417  float ClassifyBlobAsWord(int pass_n, PAGE_RES_IT* pr_it, C_BLOB* blob,
418  STRING* best_str, float* c2);
419  void classify_word_and_language(int pass_n, PAGE_RES_IT* pr_it,
420  WordData* word_data);
421  void classify_word_pass1(const WordData& word_data,
422  WERD_RES** in_word,
423  PointerVector<WERD_RES>* out_words);
424  void recog_pseudo_word(PAGE_RES* page_res, // blocks to check
425  TBOX &selection_box);
426 
427  void fix_rep_char(PAGE_RES_IT* page_res_it);
428 
429  ACCEPTABLE_WERD_TYPE acceptable_word_string(const UNICHARSET& char_set,
430  const char *s,
431  const char *lengths);
432  void match_word_pass_n(int pass_n, WERD_RES *word, ROW *row, BLOCK* block);
433  void classify_word_pass2(const WordData& word_data,
434  WERD_RES** in_word,
435  PointerVector<WERD_RES>* out_words);
436  void ReportXhtFixResult(bool accept_new_word, float new_x_ht,
437  WERD_RES* word, WERD_RES* new_word);
438  bool RunOldFixXht(WERD_RES *word, BLOCK* block, ROW *row);
439  bool TrainedXheightFix(WERD_RES *word, BLOCK* block, ROW *row);
440  // Runs recognition with the test baseline shift and x-height and returns true
441  // if there was an improvement in recognition result.
442  bool TestNewNormalization(int original_misfits, float baseline_shift,
443  float new_x_ht, WERD_RES *word, BLOCK* block,
444  ROW *row);
445  BOOL8 recog_interactive(PAGE_RES_IT* pr_it);
446 
447  // Set fonts of this word.
448  void set_word_fonts(WERD_RES *word);
449  void font_recognition_pass(PAGE_RES* page_res);
450  void dictionary_correction_pass(PAGE_RES* page_res);
451  BOOL8 check_debug_pt(WERD_RES *word, int location);
452 
454  bool SubAndSuperscriptFix(WERD_RES *word_res);
455  void GetSubAndSuperscriptCandidates(const WERD_RES *word,
456  int *num_rebuilt_leading,
457  ScriptPos *leading_pos,
458  float *leading_certainty,
459  int *num_rebuilt_trailing,
460  ScriptPos *trailing_pos,
461  float *trailing_certainty,
462  float *avg_certainty,
463  float *unlikely_threshold);
464  WERD_RES *TrySuperscriptSplits(int num_chopped_leading,
465  float leading_certainty,
466  ScriptPos leading_pos,
467  int num_chopped_trailing,
468  float trailing_certainty,
469  ScriptPos trailing_pos,
470  WERD_RES *word,
471  bool *is_good,
472  int *retry_leading,
473  int *retry_trailing);
474  bool BelievableSuperscript(bool debug,
475  const WERD_RES &word,
476  float certainty_threshold,
477  int *left_ok,
478  int *right_ok) const;
479 
481 
482  void output_pass(PAGE_RES_IT &page_res_it, const TBOX *target_word_box);
483  void write_results(PAGE_RES_IT &page_res_it, // full info
484  char newline_type, // type of newline
485  BOOL8 force_eol // override tilde crunch?
486  );
487  void set_unlv_suspects(WERD_RES *word);
488  UNICHAR_ID get_rep_char(WERD_RES *word); // what char is repeated?
489  BOOL8 acceptable_number_string(const char *s,
490  const char *lengths);
491  inT16 count_alphanums(const WERD_CHOICE &word);
492  inT16 count_alphas(const WERD_CHOICE &word);
494  void read_config_file(const char *filename, SetParamConstraint constraint);
495  // Initialize for potentially a set of languages defined by the language
496  // string and recursively any additional languages required by any language
497  // traineddata file (via tessedit_load_sublangs in its config) that is loaded.
498  // See init_tesseract_internal for args.
499  int init_tesseract(const char* arg0, const char* textbase,
500  const char* language, OcrEngineMode oem, char** configs,
501  int configs_size, const GenericVector<STRING>* vars_vec,
502  const GenericVector<STRING>* vars_values,
503  bool set_only_init_params, TessdataManager* mgr);
504  int init_tesseract(const char *datapath,
505  const char *language,
506  OcrEngineMode oem) {
507  TessdataManager mgr;
508  return init_tesseract(datapath, NULL, language, oem, NULL, 0, NULL, NULL,
509  false, &mgr);
510  }
511  // Common initialization for a single language.
512  // arg0 is the datapath for the tessdata directory, which could be the
513  // path of the tessdata directory with no trailing /, or (if tessdata
514  // lives in the same directory as the executable, the path of the executable,
515  // hence the name arg0.
516  // textbase is an optional output file basename (used only for training)
517  // language is the language code to load.
518  // oem controls which engine(s) will operate on the image
519  // configs (argv) is an array of config filenames to load variables from.
520  // May be NULL.
521  // configs_size (argc) is the number of elements in configs.
522  // vars_vec is an optional vector of variables to set.
523  // vars_values is an optional corresponding vector of values for the variables
524  // in vars_vec.
525  // If set_only_init_params is true, then only the initialization variables
526  // will be set.
527  int init_tesseract_internal(const char* arg0, const char* textbase,
528  const char* language, OcrEngineMode oem,
529  char** configs, int configs_size,
530  const GenericVector<STRING>* vars_vec,
531  const GenericVector<STRING>* vars_values,
532  bool set_only_init_params, TessdataManager* mgr);
533 
534  // Set the universal_id member of each font to be unique among all
535  // instances of the same font loaded.
536  void SetupUniversalFontIds();
537 
538  int init_tesseract_lm(const char* arg0, const char* textbase,
539  const char* language, TessdataManager* mgr);
540 
541  void recognize_page(STRING& image_name);
542  void end_tesseract();
543 
544  bool init_tesseract_lang_data(const char* arg0, const char* textbase,
545  const char* language, OcrEngineMode oem,
546  char** configs, int configs_size,
547  const GenericVector<STRING>* vars_vec,
548  const GenericVector<STRING>* vars_values,
549  bool set_only_init_params,
550  TessdataManager* mgr);
551 
552  void ParseLanguageString(const char* lang_str,
553  GenericVector<STRING>* to_load,
554  GenericVector<STRING>* not_to_load);
555 
557  SVMenuNode *build_menu_new();
558  #ifndef GRAPHICS_DISABLED
559  void pgeditor_main(int width, int height, PAGE_RES* page_res);
560  #endif // GRAPHICS_DISABLED
561  void process_image_event( // action in image win
562  const SVEvent &event);
563  BOOL8 process_cmd_win_event( // UI command semantics
564  inT32 cmd_event, // which menu item?
565  char *new_value // any prompt data
566  );
567  void debug_word(PAGE_RES* page_res, const TBOX &selection_box);
568  void do_re_display(
569  BOOL8 (tesseract::Tesseract::*word_painter)(PAGE_RES_IT* pr_it));
570  BOOL8 word_display(PAGE_RES_IT* pr_it);
571  BOOL8 word_bln_display(PAGE_RES_IT* pr_it);
572  BOOL8 word_blank_and_set_display(PAGE_RES_IT* pr_its);
573  BOOL8 word_set_display(PAGE_RES_IT* pr_it);
574  // #ifndef GRAPHICS_DISABLED
575  BOOL8 word_dumper(PAGE_RES_IT* pr_it);
576  // #endif // GRAPHICS_DISABLED
577  void blob_feature_display(PAGE_RES* page_res, const TBOX& selection_box);
579  // make rej map for word
580  void make_reject_map(WERD_RES *word, ROW *row, inT16 pass);
581  BOOL8 one_ell_conflict(WERD_RES *word_res, BOOL8 update_map);
582  inT16 first_alphanum_index(const char *word,
583  const char *word_lengths);
584  inT16 first_alphanum_offset(const char *word,
585  const char *word_lengths);
586  inT16 alpha_count(const char *word,
587  const char *word_lengths);
588  BOOL8 word_contains_non_1_digit(const char *word,
589  const char *word_lengths);
590  void dont_allow_1Il(WERD_RES *word);
591  inT16 count_alphanums( //how many alphanums
592  WERD_RES *word);
593  void flip_0O(WERD_RES *word);
594  BOOL8 non_0_digit(const UNICHARSET& ch_set, UNICHAR_ID unichar_id);
595  BOOL8 non_O_upper(const UNICHARSET& ch_set, UNICHAR_ID unichar_id);
596  BOOL8 repeated_nonalphanum_wd(WERD_RES *word, ROW *row);
597  void nn_match_word( //Match a word
598  WERD_RES *word,
599  ROW *row);
600  void nn_recover_rejects(WERD_RES *word, ROW *row);
601  void set_done( //set done flag
602  WERD_RES *word,
603  inT16 pass);
604  inT16 safe_dict_word(const WERD_RES *werd_res); // is best_choice in dict?
605  void flip_hyphens(WERD_RES *word);
606  void reject_I_1_L(WERD_RES *word);
607  void reject_edge_blobs(WERD_RES *word);
608  void reject_mostly_rejects(WERD_RES *word);
610  BOOL8 word_adaptable( //should we adapt?
611  WERD_RES *word,
612  uinT16 mode);
613 
615  void recog_word_recursive(WERD_RES* word);
616  void recog_word(WERD_RES *word);
617  void split_and_recog_word(WERD_RES* word);
618  void split_word(WERD_RES *word,
619  int split_pt,
620  WERD_RES **right_piece,
621  BlamerBundle **orig_blamer_bundle) const;
622  void join_words(WERD_RES *word,
623  WERD_RES *word2,
624  BlamerBundle *orig_bb) const;
626  BOOL8 digit_or_numeric_punct(WERD_RES *word, int char_position);
627  inT16 eval_word_spacing(WERD_RES_LIST &word_res_list);
628  void match_current_words(WERD_RES_LIST &words, ROW *row, BLOCK* block);
629  inT16 fp_eval_word_spacing(WERD_RES_LIST &word_res_list);
630  void fix_noisy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK* block);
631  void fix_fuzzy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK* block);
632  void fix_sp_fp_word(WERD_RES_IT &word_res_it, ROW *row, BLOCK* block);
633  void fix_fuzzy_spaces( //find fuzzy words
634  ETEXT_DESC *monitor, //progress monitor
635  inT32 word_count, //count of words in doc
636  PAGE_RES *page_res);
637  void dump_words(WERD_RES_LIST &perm, inT16 score,
638  inT16 mode, BOOL8 improved);
639  BOOL8 fixspace_thinks_word_done(WERD_RES *word);
640  inT16 worst_noise_blob(WERD_RES *word_res, float *worst_noise_score);
641  float blob_noise_score(TBLOB *blob);
642  void break_noisiest_blob_word(WERD_RES_LIST &words);
644  GARBAGE_LEVEL garbage_word(WERD_RES *word, BOOL8 ok_dict_word);
645  BOOL8 potential_word_crunch(WERD_RES *word,
646  GARBAGE_LEVEL garbage_level,
647  BOOL8 ok_dict_word);
648  void tilde_crunch(PAGE_RES_IT &page_res_it);
649  void unrej_good_quality_words( //unreject potential
650  PAGE_RES_IT &page_res_it);
651  void doc_and_block_rejection( //reject big chunks
652  PAGE_RES_IT &page_res_it,
653  BOOL8 good_quality_doc);
654  void quality_based_rejection(PAGE_RES_IT &page_res_it,
655  BOOL8 good_quality_doc);
656  void convert_bad_unlv_chs(WERD_RES *word_res);
657  void tilde_delete(PAGE_RES_IT &page_res_it);
658  inT16 word_blob_quality(WERD_RES *word, ROW *row);
659  void word_char_quality(WERD_RES *word, ROW *row, inT16 *match_count,
660  inT16 *accepted_match_count);
661  void unrej_good_chs(WERD_RES *word, ROW *row);
662  inT16 count_outline_errs(char c, inT16 outline_count);
663  inT16 word_outline_errs(WERD_RES *word);
664  BOOL8 terrible_word_crunch(WERD_RES *word, GARBAGE_LEVEL garbage_level);
665  CRUNCH_MODE word_deletable(WERD_RES *word, inT16 &delete_mode);
666  inT16 failure_count(WERD_RES *word);
667  BOOL8 noise_outlines(TWERD *word);
669  void
670  process_selected_words (
671  PAGE_RES* page_res, // blocks to check
672  //function to call
673  TBOX & selection_box,
674  BOOL8 (tesseract::Tesseract::*word_processor)(PAGE_RES_IT* pr_it));
676  void tess_add_doc_word( //test acceptability
677  WERD_CHOICE *word_choice //after context
678  );
679  void tess_segment_pass_n(int pass_n, WERD_RES *word);
680  bool tess_acceptable_word(WERD_RES *word);
681 
683  // Applies the box file based on the image name fname, and resegments
684  // the words in the block_list (page), with:
685  // blob-mode: one blob per line in the box file, words as input.
686  // word/line-mode: one blob per space-delimited unit after the #, and one word
687  // per line in the box file. (See comment above for box file format.)
688  // If find_segmentation is true, (word/line mode) then the classifier is used
689  // to re-segment words/lines to match the space-delimited truth string for
690  // each box. In this case, the input box may be for a word or even a whole
691  // text line, and the output words will contain multiple blobs corresponding
692  // to the space-delimited input string.
693  // With find_segmentation false, no classifier is needed, but the chopper
694  // can still be used to correctly segment touching characters with the help
695  // of the input boxes.
696  // In the returned PAGE_RES, the WERD_RES are setup as they would be returned
697  // from normal classification, ie. with a word, chopped_word, rebuild_word,
698  // seam_array, denorm, box_word, and best_state, but NO best_choice or
699  // raw_choice, as they would require a UNICHARSET, which we aim to avoid.
700  // Instead, the correct_text member of WERD_RES is set, and this may be later
701  // converted to a best_choice using CorrectClassifyWords. CorrectClassifyWords
702  // is not required before calling ApplyBoxTraining.
703  PAGE_RES* ApplyBoxes(const STRING& fname, bool find_segmentation,
704  BLOCK_LIST *block_list);
705 
706  // Any row xheight that is significantly different from the median is set
707  // to the median.
708  void PreenXHeights(BLOCK_LIST *block_list);
709 
710  // Builds a PAGE_RES from the block_list in the way required for ApplyBoxes:
711  // All fuzzy spaces are removed, and all the words are maximally chopped.
712  PAGE_RES* SetupApplyBoxes(const GenericVector<TBOX>& boxes,
713  BLOCK_LIST *block_list);
714  // Tests the chopper by exhaustively running chop_one_blob.
715  // The word_res will contain filled chopped_word, seam_array, denorm,
716  // box_word and best_state for the maximally chopped word.
717  void MaximallyChopWord(const GenericVector<TBOX>& boxes,
718  BLOCK* block, ROW* row, WERD_RES* word_res);
719  // Gather consecutive blobs that match the given box into the best_state
720  // and corresponding correct_text.
721  // Fights over which box owns which blobs are settled by pre-chopping and
722  // applying the blobs to box or next_box with the least non-overlap.
723  // Returns false if the box was in error, which can only be caused by
724  // failing to find an appropriate blob for a box.
725  // This means that occasionally, blobs may be incorrectly segmented if the
726  // chopper fails to find a suitable chop point.
727  bool ResegmentCharBox(PAGE_RES* page_res, const TBOX *prev_box,
728  const TBOX& box, const TBOX& next_box,
729  const char* correct_text);
730  // Consume all source blobs that strongly overlap the given box,
731  // putting them into a new word, with the correct_text label.
732  // Fights over which box owns which blobs are settled by
733  // applying the blobs to box or next_box with the least non-overlap.
734  // Returns false if the box was in error, which can only be caused by
735  // failing to find an overlapping blob for a box.
736  bool ResegmentWordBox(BLOCK_LIST *block_list,
737  const TBOX& box, const TBOX& next_box,
738  const char* correct_text);
739  // Resegments the words by running the classifier in an attempt to find the
740  // correct segmentation that produces the required string.
741  void ReSegmentByClassification(PAGE_RES* page_res);
742  // Converts the space-delimited string of utf8 text to a vector of UNICHAR_ID.
743  // Returns false if an invalid UNICHAR_ID is encountered.
744  bool ConvertStringToUnichars(const char* utf8,
745  GenericVector<UNICHAR_ID>* class_ids);
746  // Resegments the word to achieve the target_text from the classifier.
747  // Returns false if the re-segmentation fails.
748  // Uses brute-force combination of up to kMaxGroupSize adjacent blobs, and
749  // applies a full search on the classifier results to find the best classified
750  // segmentation. As a compromise to obtain better recall, 1-1 ambigiguity
751  // substitutions ARE used.
752  bool FindSegmentation(const GenericVector<UNICHAR_ID>& target_text,
753  WERD_RES* word_res);
754  // Recursive helper to find a match to the target_text (from text_index
755  // position) in the choices (from choices_pos position).
756  // Choices is an array of GenericVectors, of length choices_length, with each
757  // element representing a starting position in the word, and the
758  // GenericVector holding classification results for a sequence of consecutive
759  // blobs, with index 0 being a single blob, index 1 being 2 blobs etc.
760  void SearchForText(const GenericVector<BLOB_CHOICE_LIST*>* choices,
761  int choices_pos, int choices_length,
762  const GenericVector<UNICHAR_ID>& target_text,
763  int text_index,
764  float rating, GenericVector<int>* segmentation,
765  float* best_rating, GenericVector<int>* best_segmentation);
766  // Counts up the labelled words and the blobs within.
767  // Deletes all unused or emptied words, counting the unused ones.
768  // Resets W_BOL and W_EOL flags correctly.
769  // Builds the rebuild_word and rebuilds the box_word.
770  void TidyUp(PAGE_RES* page_res);
771  // Logs a bad box by line in the box file and box coords.
772  void ReportFailedBox(int boxfile_lineno, TBOX box, const char *box_ch,
773  const char *err_msg);
774  // Creates a fake best_choice entry in each WERD_RES with the correct text.
775  void CorrectClassifyWords(PAGE_RES* page_res);
776  // Call LearnWord to extract features for labelled blobs within each word.
777  // Features are stored in an internal buffer.
778  void ApplyBoxTraining(const STRING& fontname, PAGE_RES* page_res);
779 
781  // Returns the number of misfit blob tops in this word.
782  int CountMisfitTops(WERD_RES *word_res);
783  // Returns a new x-height in pixels (original image coords) that is
784  // maximally compatible with the result in word_res.
785  // Returns 0.0f if no x-height is found that is better than the current
786  // estimate.
787  float ComputeCompatibleXheight(WERD_RES *word_res, float* baseline_shift);
789  // TODO(ocr-team): Find and remove obsolete parameters.
790  BOOL_VAR_H(tessedit_resegment_from_boxes, false,
791  "Take segmentation and labeling from box file");
792  BOOL_VAR_H(tessedit_resegment_from_line_boxes, false,
793  "Conversion of word/line box file to char box file");
794  BOOL_VAR_H(tessedit_train_from_boxes, false,
795  "Generate training data from boxed chars");
796  BOOL_VAR_H(tessedit_make_boxes_from_boxes, false,
797  "Generate more boxes from boxed chars");
798  BOOL_VAR_H(tessedit_train_line_recognizer, false,
799  "Break input into lines and remap boxes if present");
800  BOOL_VAR_H(tessedit_dump_pageseg_images, false,
801  "Dump intermediate images made during page segmentation");
802  INT_VAR_H(tessedit_pageseg_mode, PSM_SINGLE_BLOCK,
803  "Page seg mode: 0=osd only, 1=auto+osd, 2=auto, 3=col, 4=block,"
804  " 5=line, 6=word, 7=char"
805  " (Values from PageSegMode enum in publictypes.h)");
806  INT_VAR_H(tessedit_ocr_engine_mode, tesseract::OEM_DEFAULT,
807  "Which OCR engine(s) to run (Tesseract, LSTM, both). Defaults"
808  " to loading and running the most accurate available.");
809  STRING_VAR_H(tessedit_char_blacklist, "",
810  "Blacklist of chars not to recognize");
811  STRING_VAR_H(tessedit_char_whitelist, "",
812  "Whitelist of chars to recognize");
813  STRING_VAR_H(tessedit_char_unblacklist, "",
814  "List of chars to override tessedit_char_blacklist");
815  BOOL_VAR_H(tessedit_ambigs_training, false,
816  "Perform training for ambiguities");
817  INT_VAR_H(pageseg_devanagari_split_strategy,
819  "Whether to use the top-line splitting process for Devanagari "
820  "documents while performing page-segmentation.");
821  INT_VAR_H(ocr_devanagari_split_strategy,
823  "Whether to use the top-line splitting process for Devanagari "
824  "documents while performing ocr.");
825  STRING_VAR_H(tessedit_write_params_to_file, "",
826  "Write all parameters to the given file.");
827  BOOL_VAR_H(tessedit_adaption_debug, false,
828  "Generate and print debug information for adaption");
829  INT_VAR_H(bidi_debug, 0, "Debug level for BiDi");
830  INT_VAR_H(applybox_debug, 1, "Debug level");
831  INT_VAR_H(applybox_page, 0, "Page number to apply boxes from");
832  STRING_VAR_H(applybox_exposure_pattern, ".exp",
833  "Exposure value follows this pattern in the image"
834  " filename. The name of the image files are expected"
835  " to be in the form [lang].[fontname].exp[num].tif");
836  BOOL_VAR_H(applybox_learn_chars_and_char_frags_mode, false,
837  "Learn both character fragments (as is done in the"
838  " special low exposure mode) as well as unfragmented"
839  " characters.");
840  BOOL_VAR_H(applybox_learn_ngrams_mode, false,
841  "Each bounding box is assumed to contain ngrams. Only"
842  " learn the ngrams whose outlines overlap horizontally.");
843  BOOL_VAR_H(tessedit_display_outwords, false, "Draw output words");
844  BOOL_VAR_H(tessedit_dump_choices, false, "Dump char choices");
845  BOOL_VAR_H(tessedit_timing_debug, false, "Print timing stats");
846  BOOL_VAR_H(tessedit_fix_fuzzy_spaces, true,
847  "Try to improve fuzzy spaces");
848  BOOL_VAR_H(tessedit_unrej_any_wd, false,
849  "Don't bother with word plausibility");
850  BOOL_VAR_H(tessedit_fix_hyphens, true, "Crunch double hyphens?");
851  BOOL_VAR_H(tessedit_redo_xheight, true, "Check/Correct x-height");
852  BOOL_VAR_H(tessedit_enable_doc_dict, true,
853  "Add words to the document dictionary");
854  BOOL_VAR_H(tessedit_debug_fonts, false, "Output font info per char");
855  BOOL_VAR_H(tessedit_debug_block_rejection, false, "Block and Row stats");
856  BOOL_VAR_H(tessedit_enable_bigram_correction, true,
857  "Enable correction based on the word bigram dictionary.");
858  BOOL_VAR_H(tessedit_enable_dict_correction, false,
859  "Enable single word correction based on the dictionary.");
860  INT_VAR_H(tessedit_bigram_debug, 0, "Amount of debug output for bigram "
861  "correction.");
862  BOOL_VAR_H(enable_noise_removal, true,
863  "Remove and conditionally reassign small outlines when they"
864  " confuse layout analysis, determining diacritics vs noise");
865  INT_VAR_H(debug_noise_removal, 0, "Debug reassignment of small outlines");
866  // Worst (min) certainty, for which a diacritic is allowed to make the base
867  // character worse and still be included.
868  double_VAR_H(noise_cert_basechar, -8.0, "Hingepoint for base char certainty");
869  // Worst (min) certainty, for which a non-overlapping diacritic is allowed to
870  // make the base character worse and still be included.
871  double_VAR_H(noise_cert_disjoint, -2.5, "Hingepoint for disjoint certainty");
872  // Worst (min) certainty, for which a diacritic is allowed to make a new
873  // stand-alone blob.
874  double_VAR_H(noise_cert_punc, -2.5, "Threshold for new punc char certainty");
875  // Factor of certainty margin for adding diacritics to not count as worse.
876  double_VAR_H(noise_cert_factor, 0.375,
877  "Scaling on certainty diff from Hingepoint");
878  INT_VAR_H(noise_maxperblob, 8, "Max diacritics to apply to a blob");
879  INT_VAR_H(noise_maxperword, 16, "Max diacritics to apply to a word");
880  INT_VAR_H(debug_x_ht_level, 0, "Reestimate debug");
881  BOOL_VAR_H(debug_acceptable_wds, false, "Dump word pass/fail chk");
882  STRING_VAR_H(chs_leading_punct, "('`\"", "Leading punctuation");
883  STRING_VAR_H(chs_trailing_punct1, ").,;:?!", "1st Trailing punctuation");
884  STRING_VAR_H(chs_trailing_punct2, ")'`\"", "2nd Trailing punctuation");
885  double_VAR_H(quality_rej_pc, 0.08, "good_quality_doc lte rejection limit");
886  double_VAR_H(quality_blob_pc, 0.0, "good_quality_doc gte good blobs limit");
887  double_VAR_H(quality_outline_pc, 1.0,
888  "good_quality_doc lte outline error limit");
889  double_VAR_H(quality_char_pc, 0.95, "good_quality_doc gte good char limit");
890  INT_VAR_H(quality_min_initial_alphas_reqd, 2, "alphas in a good word");
891  INT_VAR_H(tessedit_tess_adaption_mode, 0x27,
892  "Adaptation decision algorithm for tess");
893  BOOL_VAR_H(tessedit_minimal_rej_pass1, false,
894  "Do minimal rejection on pass 1 output");
895  BOOL_VAR_H(tessedit_test_adaption, false, "Test adaption criteria");
896  BOOL_VAR_H(tessedit_matcher_log, false, "Log matcher activity");
897  INT_VAR_H(tessedit_test_adaption_mode, 3,
898  "Adaptation decision algorithm for tess");
899  BOOL_VAR_H(test_pt, false, "Test for point");
900  double_VAR_H(test_pt_x, 99999.99, "xcoord");
901  double_VAR_H(test_pt_y, 99999.99, "ycoord");
902  INT_VAR_H(multilang_debug_level, 0, "Print multilang debug info.");
903  INT_VAR_H(paragraph_debug_level, 0, "Print paragraph debug info.");
904  BOOL_VAR_H(paragraph_text_based, true,
905  "Run paragraph detection on the post-text-recognition "
906  "(more accurate)");
907  BOOL_VAR_H(lstm_use_matrix, 1, "Use ratings matrix/beam searct with lstm");
908  STRING_VAR_H(outlines_odd, "%| ", "Non standard number of outlines");
909  STRING_VAR_H(outlines_2, "ij!?%\":;", "Non standard number of outlines");
910  BOOL_VAR_H(docqual_excuse_outline_errs, false,
911  "Allow outline errs in unrejection?");
912  BOOL_VAR_H(tessedit_good_quality_unrej, true,
913  "Reduce rejection on good docs");
914  BOOL_VAR_H(tessedit_use_reject_spaces, true, "Reject spaces?");
915  double_VAR_H(tessedit_reject_doc_percent, 65.00,
916  "%rej allowed before rej whole doc");
917  double_VAR_H(tessedit_reject_block_percent, 45.00,
918  "%rej allowed before rej whole block");
919  double_VAR_H(tessedit_reject_row_percent, 40.00,
920  "%rej allowed before rej whole row");
921  double_VAR_H(tessedit_whole_wd_rej_row_percent, 70.00,
922  "Number of row rejects in whole word rejects"
923  "which prevents whole row rejection");
924  BOOL_VAR_H(tessedit_preserve_blk_rej_perfect_wds, true,
925  "Only rej partially rejected words in block rejection");
926  BOOL_VAR_H(tessedit_preserve_row_rej_perfect_wds, true,
927  "Only rej partially rejected words in row rejection");
928  BOOL_VAR_H(tessedit_dont_blkrej_good_wds, false,
929  "Use word segmentation quality metric");
930  BOOL_VAR_H(tessedit_dont_rowrej_good_wds, false,
931  "Use word segmentation quality metric");
932  INT_VAR_H(tessedit_preserve_min_wd_len, 2,
933  "Only preserve wds longer than this");
934  BOOL_VAR_H(tessedit_row_rej_good_docs, true,
935  "Apply row rejection to good docs");
936  double_VAR_H(tessedit_good_doc_still_rowrej_wd, 1.1,
937  "rej good doc wd if more than this fraction rejected");
938  BOOL_VAR_H(tessedit_reject_bad_qual_wds, true,
939  "Reject all bad quality wds");
940  BOOL_VAR_H(tessedit_debug_doc_rejection, false, "Page stats");
941  BOOL_VAR_H(tessedit_debug_quality_metrics, false,
942  "Output data to debug file");
943  BOOL_VAR_H(bland_unrej, false, "unrej potential with no checks");
944  double_VAR_H(quality_rowrej_pc, 1.1,
945  "good_quality_doc gte good char limit");
946  BOOL_VAR_H(unlv_tilde_crunching, true,
947  "Mark v.bad words for tilde crunch");
948  BOOL_VAR_H(hocr_font_info, false,
949  "Add font info to hocr output");
950  BOOL_VAR_H(crunch_early_merge_tess_fails, true, "Before word crunch?");
951  BOOL_VAR_H(crunch_early_convert_bad_unlv_chs, false, "Take out ~^ early?");
952  double_VAR_H(crunch_terrible_rating, 80.0, "crunch rating lt this");
953  BOOL_VAR_H(crunch_terrible_garbage, true, "As it says");
954  double_VAR_H(crunch_poor_garbage_cert, -9.0,
955  "crunch garbage cert lt this");
956  double_VAR_H(crunch_poor_garbage_rate, 60, "crunch garbage rating lt this");
957  double_VAR_H(crunch_pot_poor_rate, 40, "POTENTIAL crunch rating lt this");
958  double_VAR_H(crunch_pot_poor_cert, -8.0, "POTENTIAL crunch cert lt this");
959  BOOL_VAR_H(crunch_pot_garbage, true, "POTENTIAL crunch garbage");
960  double_VAR_H(crunch_del_rating, 60, "POTENTIAL crunch rating lt this");
961  double_VAR_H(crunch_del_cert, -10.0, "POTENTIAL crunch cert lt this");
962  double_VAR_H(crunch_del_min_ht, 0.7, "Del if word ht lt xht x this");
963  double_VAR_H(crunch_del_max_ht, 3.0, "Del if word ht gt xht x this");
964  double_VAR_H(crunch_del_min_width, 3.0, "Del if word width lt xht x this");
965  double_VAR_H(crunch_del_high_word, 1.5,
966  "Del if word gt xht x this above bl");
967  double_VAR_H(crunch_del_low_word, 0.5, "Del if word gt xht x this below bl");
968  double_VAR_H(crunch_small_outlines_size, 0.6, "Small if lt xht x this");
969  INT_VAR_H(crunch_rating_max, 10, "For adj length in rating per ch");
970  INT_VAR_H(crunch_pot_indicators, 1, "How many potential indicators needed");
971  BOOL_VAR_H(crunch_leave_ok_strings, true, "Don't touch sensible strings");
972  BOOL_VAR_H(crunch_accept_ok, true, "Use acceptability in okstring");
973  BOOL_VAR_H(crunch_leave_accept_strings, false,
974  "Don't pot crunch sensible strings");
975  BOOL_VAR_H(crunch_include_numerals, false, "Fiddle alpha figures");
976  INT_VAR_H(crunch_leave_lc_strings, 4,
977  "Don't crunch words with long lower case strings");
978  INT_VAR_H(crunch_leave_uc_strings, 4,
979  "Don't crunch words with long lower case strings");
980  INT_VAR_H(crunch_long_repetitions, 3, "Crunch words with long repetitions");
981  INT_VAR_H(crunch_debug, 0, "As it says");
982  INT_VAR_H(fixsp_non_noise_limit, 1,
983  "How many non-noise blbs either side?");
984  double_VAR_H(fixsp_small_outlines_size, 0.28, "Small if lt xht x this");
985  BOOL_VAR_H(tessedit_prefer_joined_punct, false, "Reward punctation joins");
986  INT_VAR_H(fixsp_done_mode, 1, "What constitues done for spacing");
987  INT_VAR_H(debug_fix_space_level, 0, "Contextual fixspace debug");
988  STRING_VAR_H(numeric_punctuation, ".,",
989  "Punct. chs expected WITHIN numbers");
990  INT_VAR_H(x_ht_acceptance_tolerance, 8,
991  "Max allowed deviation of blob top outside of font data");
992  INT_VAR_H(x_ht_min_change, 8, "Min change in xht before actually trying it");
993  INT_VAR_H(superscript_debug, 0, "Debug level for sub & superscript fixer");
994  double_VAR_H(superscript_worse_certainty, 2.0, "How many times worse "
995  "certainty does a superscript position glyph need to be for us "
996  "to try classifying it as a char with a different baseline?");
997  double_VAR_H(superscript_bettered_certainty, 0.97, "What reduction in "
998  "badness do we think sufficient to choose a superscript over "
999  "what we'd thought. For example, a value of 0.6 means we want "
1000  "to reduce badness of certainty by 40%");
1001  double_VAR_H(superscript_scaledown_ratio, 0.4,
1002  "A superscript scaled down more than this is unbelievably "
1003  "small. For example, 0.3 means we expect the font size to "
1004  "be no smaller than 30% of the text line font size.");
1005  double_VAR_H(subscript_max_y_top, 0.5,
1006  "Maximum top of a character measured as a multiple of x-height "
1007  "above the baseline for us to reconsider whether it's a "
1008  "subscript.");
1009  double_VAR_H(superscript_min_y_bottom, 0.3,
1010  "Minimum bottom of a character measured as a multiple of "
1011  "x-height above the baseline for us to reconsider whether it's "
1012  "a superscript.");
1013  BOOL_VAR_H(tessedit_write_block_separators, false,
1014  "Write block separators in output");
1015  BOOL_VAR_H(tessedit_write_rep_codes, false,
1016  "Write repetition char code");
1017  BOOL_VAR_H(tessedit_write_unlv, false, "Write .unlv output file");
1018  BOOL_VAR_H(tessedit_create_txt, false, "Write .txt output file");
1019  BOOL_VAR_H(tessedit_create_hocr, false, "Write .html hOCR output file");
1020  BOOL_VAR_H(tessedit_create_tsv, false, "Write .tsv output file");
1021  BOOL_VAR_H(tessedit_create_pdf, false, "Write .pdf output file");
1022  BOOL_VAR_H(textonly_pdf, false,
1023  "Create PDF with only one invisible text layer");
1024  STRING_VAR_H(unrecognised_char, "|",
1025  "Output char for unidentified blobs");
1026  INT_VAR_H(suspect_level, 99, "Suspect marker level");
1027  INT_VAR_H(suspect_space_level, 100,
1028  "Min suspect level for rejecting spaces");
1029  INT_VAR_H(suspect_short_words, 2, "Don't Suspect dict wds longer than this");
1030  BOOL_VAR_H(suspect_constrain_1Il, false, "UNLV keep 1Il chars rejected");
1031  double_VAR_H(suspect_rating_per_ch, 999.9, "Don't touch bad rating limit");
1032  double_VAR_H(suspect_accept_rating, -999.9, "Accept good rating limit");
1033  BOOL_VAR_H(tessedit_minimal_rejection, false, "Only reject tess failures");
1034  BOOL_VAR_H(tessedit_zero_rejection, false, "Don't reject ANYTHING");
1035  BOOL_VAR_H(tessedit_word_for_word, false,
1036  "Make output have exactly one word per WERD");
1037  BOOL_VAR_H(tessedit_zero_kelvin_rejection, false,
1038  "Don't reject ANYTHING AT ALL");
1039  BOOL_VAR_H(tessedit_consistent_reps, true, "Force all rep chars the same");
1040  INT_VAR_H(tessedit_reject_mode, 0, "Rejection algorithm");
1041  BOOL_VAR_H(tessedit_rejection_debug, false, "Adaption debug");
1042  BOOL_VAR_H(tessedit_flip_0O, true, "Contextual 0O O0 flips");
1043  double_VAR_H(tessedit_lower_flip_hyphen, 1.5,
1044  "Aspect ratio dot/hyphen test");
1045  double_VAR_H(tessedit_upper_flip_hyphen, 1.8,
1046  "Aspect ratio dot/hyphen test");
1047  BOOL_VAR_H(rej_trust_doc_dawg, false, "Use DOC dawg in 11l conf. detector");
1048  BOOL_VAR_H(rej_1Il_use_dict_word, false, "Use dictword test");
1049  BOOL_VAR_H(rej_1Il_trust_permuter_type, true, "Don't double check");
1050  BOOL_VAR_H(rej_use_tess_accepted, true, "Individual rejection control");
1051  BOOL_VAR_H(rej_use_tess_blanks, true, "Individual rejection control");
1052  BOOL_VAR_H(rej_use_good_perm, true, "Individual rejection control");
1053  BOOL_VAR_H(rej_use_sensible_wd, false, "Extend permuter check");
1054  BOOL_VAR_H(rej_alphas_in_number_perm, false, "Extend permuter check");
1055  double_VAR_H(rej_whole_of_mostly_reject_word_fract, 0.85, "if >this fract");
1056  INT_VAR_H(tessedit_image_border, 2, "Rej blbs near image edge limit");
1057  STRING_VAR_H(ok_repeated_ch_non_alphanum_wds, "-?*\075",
1058  "Allow NN to unrej");
1059  STRING_VAR_H(conflict_set_I_l_1, "Il1[]", "Il1 conflict set");
1060  INT_VAR_H(min_sane_x_ht_pixels, 8, "Reject any x-ht lt or eq than this");
1061  BOOL_VAR_H(tessedit_create_boxfile, false, "Output text with boxes");
1062  INT_VAR_H(tessedit_page_number, -1,
1063  "-1 -> All pages, else specific page to process");
1064  BOOL_VAR_H(tessedit_write_images, false, "Capture the image from the IPE");
1065  BOOL_VAR_H(interactive_display_mode, false, "Run interactively?");
1066  STRING_VAR_H(file_type, ".tif", "Filename extension");
1067  BOOL_VAR_H(tessedit_override_permuter, true, "According to dict_word");
1068  STRING_VAR_H(tessedit_load_sublangs, "",
1069  "List of languages to load with this one");
1070  BOOL_VAR_H(tessedit_use_primary_params_model, false,
1071  "In multilingual mode use params model of the primary language");
1072  // Min acceptable orientation margin (difference in scores between top and 2nd
1073  // choice in OSResults::orientations) to believe the page orientation.
1074  double_VAR_H(min_orientation_margin, 7.0,
1075  "Min acceptable orientation margin");
1076  BOOL_VAR_H(textord_tabfind_show_vlines, false, "Debug line finding");
1077  BOOL_VAR_H(textord_use_cjk_fp_model, FALSE, "Use CJK fixed pitch model");
1078  BOOL_VAR_H(poly_allow_detailed_fx, false,
1079  "Allow feature extractors to see the original outline");
1080  BOOL_VAR_H(tessedit_init_config_only, false,
1081  "Only initialize with the config file. Useful if the instance is "
1082  "not going to be used for OCR but say only for layout analysis.");
1083  BOOL_VAR_H(textord_equation_detect, false, "Turn on equation detector");
1084  BOOL_VAR_H(textord_tabfind_vertical_text, true, "Enable vertical detection");
1085  BOOL_VAR_H(textord_tabfind_force_vertical_text, false,
1086  "Force using vertical text page mode");
1087  double_VAR_H(textord_tabfind_vertical_text_ratio, 0.5,
1088  "Fraction of textlines deemed vertical to use vertical page "
1089  "mode");
1090  double_VAR_H(textord_tabfind_aligned_gap_fraction, 0.75,
1091  "Fraction of height used as a minimum gap for aligned blobs.");
1092  INT_VAR_H(tessedit_parallelize, 0, "Run in parallel where possible");
1093  BOOL_VAR_H(preserve_interword_spaces, false,
1094  "Preserve multiple interword spaces");
1095  BOOL_VAR_H(include_page_breaks, false,
1096  "Include page separator string in output text after each "
1097  "image/page.");
1098  STRING_VAR_H(page_separator, "\f",
1099  "Page separator (default is form feed control character)");
1100 
1101  // The following parameters were deprecated and removed from their original
1102  // locations. The parameters are temporarily kept here to give Tesseract
1103  // users a chance to updated their [lang].traineddata and config files
1104  // without introducing failures during Tesseract initialization.
1105  // TODO(ocr-team): remove these parameters from the code once we are
1106  // reasonably sure that Tesseract users have updated their data files.
1107  //
1108  // BEGIN DEPRECATED PARAMETERS
1109  BOOL_VAR_H(textord_tabfind_vertical_horizontal_mix, true,
1110  "find horizontal lines such as headers in vertical page mode");
1111  INT_VAR_H(tessedit_ok_mode, 5, "Acceptance decision algorithm");
1112  BOOL_VAR_H(load_fixed_length_dawgs, true, "Load fixed length"
1113  " dawgs (e.g. for non-space delimited languages)");
1114  INT_VAR_H(segment_debug, 0, "Debug the whole segmentation process");
1115  BOOL_VAR_H(permute_debug, 0, "char permutation debug");
1116  double_VAR_H(bestrate_pruning_factor, 2.0, "Multiplying factor of"
1117  " current best rate to prune other hypotheses");
1118  BOOL_VAR_H(permute_script_word, 0,
1119  "Turn on word script consistency permuter");
1120  BOOL_VAR_H(segment_segcost_rating, 0,
1121  "incorporate segmentation cost in word rating?");
1122  double_VAR_H(segment_reward_script, 0.95,
1123  "Score multipler for script consistency within a word. "
1124  "Being a 'reward' factor, it should be <= 1. "
1125  "Smaller value implies bigger reward.");
1126  BOOL_VAR_H(permute_fixed_length_dawg, 0,
1127  "Turn on fixed-length phrasebook search permuter");
1128  BOOL_VAR_H(permute_chartype_word, 0,
1129  "Turn on character type (property) consistency permuter");
1130  double_VAR_H(segment_reward_chartype, 0.97,
1131  "Score multipler for char type consistency within a word. ");
1132  double_VAR_H(segment_reward_ngram_best_choice, 0.99,
1133  "Score multipler for ngram permuter's best choice"
1134  " (only used in the Han script path).");
1135  BOOL_VAR_H(ngram_permuter_activated, false,
1136  "Activate character-level n-gram-based permuter");
1137  BOOL_VAR_H(permute_only_top, false, "Run only the top choice permuter");
1138  INT_VAR_H(language_model_fixed_length_choices_depth, 3,
1139  "Depth of blob choice lists to explore"
1140  " when fixed length dawgs are on");
1141  BOOL_VAR_H(use_new_state_cost, FALSE,
1142  "use new state cost heuristics for segmentation state evaluation");
1143  double_VAR_H(heuristic_segcost_rating_base, 1.25,
1144  "base factor for adding segmentation cost into word rating."
1145  "It's a multiplying factor, the larger the value above 1, "
1146  "the bigger the effect of segmentation cost.");
1147  double_VAR_H(heuristic_weight_rating, 1,
1148  "weight associated with char rating in combined cost of state");
1149  double_VAR_H(heuristic_weight_width, 1000.0,
1150  "weight associated with width evidence in combined cost of"
1151  " state");
1152  double_VAR_H(heuristic_weight_seamcut, 0,
1153  "weight associated with seam cut in combined cost of state");
1154  double_VAR_H(heuristic_max_char_wh_ratio, 2.0,
1155  "max char width-to-height ratio allowed in segmentation");
1156  BOOL_VAR_H(enable_new_segsearch, false,
1157  "Enable new segmentation search path.");
1158  double_VAR_H(segsearch_max_fixed_pitch_char_wh_ratio, 2.0,
1159  "Maximum character width-to-height ratio for"
1160  "fixed pitch fonts");
1161  // END DEPRECATED PARAMETERS
1162 
1164  FILE *init_recog_training(const STRING &fname);
1165  void recog_training_segmented(const STRING &fname,
1166  PAGE_RES *page_res,
1167  volatile ETEXT_DESC *monitor,
1168  FILE *output_file);
1169  void ambigs_classify_and_output(const char *label,
1170  PAGE_RES_IT* pr_it,
1171  FILE *output_file);
1172 
1173  private:
1174  // The filename of a backup config file. If not null, then we currently
1175  // have a temporary debug config file loaded, and backup_config_file_
1176  // will be loaded, and set to null when debug is complete.
1177  const char* backup_config_file_;
1178  // The filename of a config file to read when processing a debug word.
1179  STRING word_config_;
1180  // Image used for input to layout analysis and tesseract recognition.
1181  // May be modified by the ShiroRekhaSplitter to eliminate the top-line.
1182  Pix* pix_binary_;
1183  // Grey-level input image if the input was not binary, otherwise NULL.
1184  Pix* pix_grey_;
1185  // Original input image. Color if the input was color.
1186  Pix* pix_original_;
1187  // Thresholds that were used to generate the thresholded image from grey.
1188  Pix* pix_thresholds_;
1189  // Debug images. If non-empty, will be written on destruction.
1190  DebugPixa pixa_debug_;
1191  // Input image resolution after any scaling. The resolution is not well
1192  // transmitted by operations on Pix, so we keep an independent record here.
1193  int source_resolution_;
1194  // The shiro-rekha splitter object which is used to split top-lines in
1195  // Devanagari words to provide a better word and grapheme segmentation.
1196  ShiroRekhaSplitter splitter_;
1197  // Page segmentation/layout
1198  Textord textord_;
1199  // True if the primary language uses right_to_left reading order.
1200  bool right_to_left_;
1201  Pix* scaled_color_;
1202  int scaled_factor_;
1203  FCOORD deskew_;
1204  FCOORD reskew_;
1205  TesseractStats stats_;
1206  // Sub-languages to be tried in addition to this.
1207  GenericVector<Tesseract*> sub_langs_;
1208  // Most recently used Tesseract out of this and sub_langs_. The default
1209  // language for the next word.
1210  Tesseract* most_recently_used_;
1211  // The size of the font table, ie max possible font id + 1.
1212  int font_table_size_;
1213  // Equation detector. Note: this pointer is NOT owned by the class.
1214  EquationDetect* equ_detect_;
1215  // LSTM recognizer, if available.
1216  LSTMRecognizer* lstm_recognizer_;
1217  // Output "page" number (actually line number) using TrainLineRecognizer.
1218  int train_line_page_num_;
1219 };
1220 
1221 } // namespace tesseract
1222 
1223 #endif // TESSERACT_CCMAIN_TESSERACTCLASS_H_
const FCOORD & reskew() const
void flip_hyphens(WERD_RES *word)
Definition: points.h:189
int32_t inT32
Definition: host.h:38
Textord * mutable_textord()
int num_sub_langs() const
int UNICHAR_ID
Definition: unichar.h:33
ACCEPTABLE_WERD_TYPE
Definition: control.h:34
WordData(const PAGE_RES_IT &page_res_it)
Pix * pix_binary() const
Tesseract * get_sub_lang(int index) const
void flip_0O(WERD_RES *word)
void set_source_resolution(int ppi)
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:160
BOOL8 word_contains_non_1_digit(const char *word, const char *word_lengths)
int16_t inT16
Definition: host.h:36
void SetScaledColor(int factor, Pix *color)
inT16 word_blob_quality(WERD_RES *word, ROW *row)
int init_tesseract(const char *datapath, const char *language, OcrEngineMode oem)
Pix * pix_original() const
Definition: blobs.h:395
BOOL8 non_0_digit(const char *str, int length)
CRUNCH_MODE
Definition: pageres.h:145
Pix * pix_grey() const
unsigned char BOOL8
Definition: host.h:44
Definition: strngs.h:45
#define FALSE
Definition: capi.h:46
const char int mode
Definition: ioapi.h:38
#define double_VAR_H(name, val, comment)
Definition: params.h:273
WordData(BLOCK *block_in, ROW *row_in, WERD_RES *word_res)
Pix * BestPix() const
Pix * scaled_color() const
Definition: rect.h:30
#define STRING_VAR_H(name, val, comment)
Definition: params.h:270
int source_resolution() const
Definition: blobs.h:261
SetParamConstraint
Definition: params.h:36
void set_pix_thresholds(Pix *thresholds)
const char * filename
Definition: ioapi.h:38
const Textord & textord() const
PointerVector< WERD_RES > lang_words
bool AnyTessLang() const
#define INT_VAR_H(name, val, comment)
Definition: params.h:264
void(Tesseract::* WordRecognizer)(const WordData &word_data, WERD_RES **in_word, PointerVector< WERD_RES > *out_words)
void dont_allow_1Il(WERD_RES *word)
void set_pix_grey(Pix *grey_pix)
#define BOOL_VAR_H(name, val, comment)
Definition: params.h:267
bool right_to_left() const
Definition: werd.h:60
GARBAGE_LEVEL
Definition: docqual.h:25
uint16_t uinT16
Definition: host.h:37
int scaled_factor() const
Definition: ocrrow.h:32
Definition: ocrblock.h:30
bool AnyLSTMLang() const
void set_pix_original(Pix *original_pix)