tesseract  4.1.0
tesseractclass.cpp
Go to the documentation of this file.
1 // File: tesseractclass.cpp
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 // variable that was previously global or static (except for
7 // constant data, and some visual debugging flags) has been moved
8 // in here, directly, or indirectly.
9 // This makes it safe to run multiple Tesseracts in different
10 // threads in parallel, and keeps the different language
11 // instances separate.
12 // Some global functions remain, but they are isolated re-entrant
13 // functions that operate on their arguments. Functions that work
14 // on variable data have been moved to an appropriate class based
15 // mostly on the directory hierarchy. For more information see
16 // slide 6 of "2ArchitectureAndDataStructures" in
17 // https://drive.google.com/file/d/0B7l10Bj_LprhbUlIUFlCdGtDYkE/edit?usp=sharing
18 // Some global data and related functions still exist in the
19 // training-related code, but they don't interfere with normal
20 // recognition operation.
21 // Author: Ray Smith
22 //
23 // (C) Copyright 2008, Google Inc.
24 // Licensed under the Apache License, Version 2.0 (the "License");
25 // you may not use this file except in compliance with the License.
26 // You may obtain a copy of the License at
27 // http://www.apache.org/licenses/LICENSE-2.0
28 // Unless required by applicable law or agreed to in writing, software
29 // distributed under the License is distributed on an "AS IS" BASIS,
30 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 // See the License for the specific language governing permissions and
32 // limitations under the License.
33 //
35 
36 // Include automatically generated configuration file if running autoconf.
37 #ifdef HAVE_CONFIG_H
38 #include "config_auto.h"
39 #endif
40 
41 #include "tesseractclass.h"
42 
43 #include "allheaders.h"
44 #include "edgblob.h"
45 #include "equationdetect.h"
46 #ifndef ANDROID_BUILD
47 #include "lstmrecognizer.h"
48 #endif
49 
50 namespace tesseract {
51 
53  : BOOL_MEMBER(tessedit_resegment_from_boxes, false,
54  "Take segmentation and labeling from box file",
55  this->params()),
56  BOOL_MEMBER(tessedit_resegment_from_line_boxes, false,
57  "Conversion of word/line box file to char box file",
58  this->params()),
59  BOOL_MEMBER(tessedit_train_from_boxes, false,
60  "Generate training data from boxed chars", this->params()),
61  BOOL_MEMBER(tessedit_make_boxes_from_boxes, false,
62  "Generate more boxes from boxed chars", this->params()),
63  BOOL_MEMBER(tessedit_train_line_recognizer, false,
64  "Break input into lines and remap boxes if present",
65  this->params()),
66  BOOL_MEMBER(tessedit_dump_pageseg_images, false,
67  "Dump intermediate images made during page segmentation",
68  this->params()),
69  // The default for pageseg_mode is the old behaviour, so as not to
70  // upset anything that relies on that.
71  INT_MEMBER(
72  tessedit_pageseg_mode, PSM_SINGLE_BLOCK,
73  "Page seg mode: 0=osd only, 1=auto+osd, 2=auto_only, 3=auto, 4=column,"
74  " 5=block_vert, 6=block, 7=line, 8=word, 9=word_circle, 10=char,"
75  "11=sparse_text, 12=sparse_text+osd, 13=raw_line"
76  " (Values from PageSegMode enum in publictypes.h)",
77  this->params()),
78  INT_INIT_MEMBER(tessedit_ocr_engine_mode, tesseract::OEM_DEFAULT,
79  "Which OCR engine(s) to run (Tesseract, LSTM, both)."
80  " Defaults to loading and running the most accurate"
81  " available.",
82  this->params()),
83  STRING_MEMBER(tessedit_char_blacklist, "",
84  "Blacklist of chars not to recognize", this->params()),
85  STRING_MEMBER(tessedit_char_whitelist, "",
86  "Whitelist of chars to recognize", this->params()),
87  STRING_MEMBER(tessedit_char_unblacklist, "",
88  "List of chars to override tessedit_char_blacklist",
89  this->params()),
90  BOOL_MEMBER(tessedit_ambigs_training, false,
91  "Perform training for ambiguities", this->params()),
92  INT_MEMBER(pageseg_devanagari_split_strategy,
93  tesseract::ShiroRekhaSplitter::NO_SPLIT,
94  "Whether to use the top-line splitting process for Devanagari "
95  "documents while performing page-segmentation.",
96  this->params()),
97  INT_MEMBER(ocr_devanagari_split_strategy,
98  tesseract::ShiroRekhaSplitter::NO_SPLIT,
99  "Whether to use the top-line splitting process for Devanagari "
100  "documents while performing ocr.",
101  this->params()),
102  STRING_MEMBER(tessedit_write_params_to_file, "",
103  "Write all parameters to the given file.", this->params()),
104  BOOL_MEMBER(tessedit_adaption_debug, false,
105  "Generate and print debug"
106  " information for adaption",
107  this->params()),
108  INT_MEMBER(bidi_debug, 0, "Debug level for BiDi", this->params()),
109  INT_MEMBER(applybox_debug, 1, "Debug level", this->params()),
110  INT_MEMBER(applybox_page, 0, "Page number to apply boxes from",
111  this->params()),
112  STRING_MEMBER(applybox_exposure_pattern, ".exp",
113  "Exposure value follows"
114  " this pattern in the image filename. The name of the image"
115  " files are expected to be in the form"
116  " [lang].[fontname].exp[num].tif",
117  this->params()),
118  BOOL_MEMBER(applybox_learn_chars_and_char_frags_mode, false,
119  "Learn both character fragments (as is done in the"
120  " special low exposure mode) as well as unfragmented"
121  " characters.",
122  this->params()),
123  BOOL_MEMBER(applybox_learn_ngrams_mode, false,
124  "Each bounding box"
125  " is assumed to contain ngrams. Only learn the ngrams"
126  " whose outlines overlap horizontally.",
127  this->params()),
128  BOOL_MEMBER(tessedit_display_outwords, false, "Draw output words",
129  this->params()),
130  BOOL_MEMBER(tessedit_dump_choices, false, "Dump char choices",
131  this->params()),
132  BOOL_MEMBER(tessedit_timing_debug, false, "Print timing stats",
133  this->params()),
134  BOOL_MEMBER(tessedit_fix_fuzzy_spaces, true,
135  "Try to improve fuzzy spaces", this->params()),
136  BOOL_MEMBER(tessedit_unrej_any_wd, false,
137  "Don't bother with word plausibility", this->params()),
138  BOOL_MEMBER(tessedit_fix_hyphens, true, "Crunch double hyphens?",
139  this->params()),
140  BOOL_MEMBER(tessedit_redo_xheight, true, "Check/Correct x-height",
141  this->params()),
142  BOOL_MEMBER(tessedit_enable_doc_dict, true,
143  "Add words to the document dictionary", this->params()),
144  BOOL_MEMBER(tessedit_debug_fonts, false, "Output font info per char",
145  this->params()),
146  BOOL_MEMBER(tessedit_debug_block_rejection, false, "Block and Row stats",
147  this->params()),
148  BOOL_MEMBER(tessedit_enable_bigram_correction, true,
149  "Enable correction based on the word bigram dictionary.",
150  this->params()),
151  BOOL_MEMBER(tessedit_enable_dict_correction, false,
152  "Enable single word correction based on the dictionary.",
153  this->params()),
154  INT_MEMBER(tessedit_bigram_debug, 0,
155  "Amount of debug output for bigram correction.",
156  this->params()),
157  BOOL_MEMBER(enable_noise_removal, true,
158  "Remove and conditionally reassign small outlines when they"
159  " confuse layout analysis, determining diacritics vs noise",
160  this->params()),
161  INT_MEMBER(debug_noise_removal, 0, "Debug reassignment of small outlines",
162  this->params()),
163  // Worst (min) certainty, for which a diacritic is allowed to make the
164  // base
165  // character worse and still be included.
166  double_MEMBER(noise_cert_basechar, -8.0,
167  "Hingepoint for base char certainty", this->params()),
168  // Worst (min) certainty, for which a non-overlapping diacritic is allowed
169  // to make the base character worse and still be included.
170  double_MEMBER(noise_cert_disjoint, -1.0,
171  "Hingepoint for disjoint certainty", this->params()),
172  // Worst (min) certainty, for which a diacritic is allowed to make a new
173  // stand-alone blob.
174  double_MEMBER(noise_cert_punc, -3.0,
175  "Threshold for new punc char certainty", this->params()),
176  // Factor of certainty margin for adding diacritics to not count as worse.
177  double_MEMBER(noise_cert_factor, 0.375,
178  "Scaling on certainty diff from Hingepoint",
179  this->params()),
180  INT_MEMBER(noise_maxperblob, 8, "Max diacritics to apply to a blob",
181  this->params()),
182  INT_MEMBER(noise_maxperword, 16, "Max diacritics to apply to a word",
183  this->params()),
184  INT_MEMBER(debug_x_ht_level, 0, "Reestimate debug", this->params()),
185  BOOL_MEMBER(debug_acceptable_wds, false, "Dump word pass/fail chk",
186  this->params()),
187  STRING_MEMBER(chs_leading_punct, "('`\"", "Leading punctuation",
188  this->params()),
189  STRING_MEMBER(chs_trailing_punct1, ").,;:?!", "1st Trailing punctuation",
190  this->params()),
191  STRING_MEMBER(chs_trailing_punct2, ")'`\"", "2nd Trailing punctuation",
192  this->params()),
193  double_MEMBER(quality_rej_pc, 0.08,
194  "good_quality_doc lte rejection limit", this->params()),
195  double_MEMBER(quality_blob_pc, 0.0,
196  "good_quality_doc gte good blobs limit", this->params()),
197  double_MEMBER(quality_outline_pc, 1.0,
198  "good_quality_doc lte outline error limit", this->params()),
199  double_MEMBER(quality_char_pc, 0.95,
200  "good_quality_doc gte good char limit", this->params()),
201  INT_MEMBER(quality_min_initial_alphas_reqd, 2, "alphas in a good word",
202  this->params()),
203  INT_MEMBER(tessedit_tess_adaption_mode, 0x27,
204  "Adaptation decision algorithm for tess", this->params()),
205  BOOL_MEMBER(tessedit_minimal_rej_pass1, false,
206  "Do minimal rejection on pass 1 output", this->params()),
207  BOOL_MEMBER(tessedit_test_adaption, false, "Test adaption criteria",
208  this->params()),
209  BOOL_MEMBER(tessedit_matcher_log, false, "Log matcher activity",
210  this->params()),
211  INT_MEMBER(tessedit_test_adaption_mode, 3,
212  "Adaptation decision algorithm for tess", this->params()),
213  BOOL_MEMBER(test_pt, false, "Test for point", this->params()),
214  double_MEMBER(test_pt_x, 99999.99, "xcoord", this->params()),
215  double_MEMBER(test_pt_y, 99999.99, "ycoord", this->params()),
216  INT_MEMBER(multilang_debug_level, 0, "Print multilang debug info.",
217  this->params()),
218  INT_MEMBER(paragraph_debug_level, 0, "Print paragraph debug info.",
219  this->params()),
220  BOOL_MEMBER(paragraph_text_based, true,
221  "Run paragraph detection on the post-text-recognition "
222  "(more accurate)",
223  this->params()),
224  BOOL_MEMBER(lstm_use_matrix, 1,
225  "Use ratings matrix/beam search with lstm", this->params()),
226  STRING_MEMBER(outlines_odd, "%| ", "Non standard number of outlines",
227  this->params()),
228  STRING_MEMBER(outlines_2, "ij!?%\":;", "Non standard number of outlines",
229  this->params()),
230  BOOL_MEMBER(docqual_excuse_outline_errs, false,
231  "Allow outline errs in unrejection?", this->params()),
232  BOOL_MEMBER(tessedit_good_quality_unrej, true,
233  "Reduce rejection on good docs", this->params()),
234  BOOL_MEMBER(tessedit_use_reject_spaces, true, "Reject spaces?",
235  this->params()),
236  double_MEMBER(tessedit_reject_doc_percent, 65.00,
237  "%rej allowed before rej whole doc", this->params()),
238  double_MEMBER(tessedit_reject_block_percent, 45.00,
239  "%rej allowed before rej whole block", this->params()),
240  double_MEMBER(tessedit_reject_row_percent, 40.00,
241  "%rej allowed before rej whole row", this->params()),
242  double_MEMBER(tessedit_whole_wd_rej_row_percent, 70.00,
243  "Number of row rejects in whole word rejects"
244  " which prevents whole row rejection",
245  this->params()),
246  BOOL_MEMBER(tessedit_preserve_blk_rej_perfect_wds, true,
247  "Only rej partially rejected words in block rejection",
248  this->params()),
249  BOOL_MEMBER(tessedit_preserve_row_rej_perfect_wds, true,
250  "Only rej partially rejected words in row rejection",
251  this->params()),
252  BOOL_MEMBER(tessedit_dont_blkrej_good_wds, false,
253  "Use word segmentation quality metric", this->params()),
254  BOOL_MEMBER(tessedit_dont_rowrej_good_wds, false,
255  "Use word segmentation quality metric", this->params()),
256  INT_MEMBER(tessedit_preserve_min_wd_len, 2,
257  "Only preserve wds longer than this", this->params()),
258  BOOL_MEMBER(tessedit_row_rej_good_docs, true,
259  "Apply row rejection to good docs", this->params()),
260  double_MEMBER(tessedit_good_doc_still_rowrej_wd, 1.1,
261  "rej good doc wd if more than this fraction rejected",
262  this->params()),
263  BOOL_MEMBER(tessedit_reject_bad_qual_wds, true,
264  "Reject all bad quality wds", this->params()),
265  BOOL_MEMBER(tessedit_debug_doc_rejection, false, "Page stats",
266  this->params()),
267  BOOL_MEMBER(tessedit_debug_quality_metrics, false,
268  "Output data to debug file", this->params()),
269  BOOL_MEMBER(bland_unrej, false, "unrej potential with no checks",
270  this->params()),
271  double_MEMBER(quality_rowrej_pc, 1.1,
272  "good_quality_doc gte good char limit", this->params()),
273  BOOL_MEMBER(unlv_tilde_crunching, false,
274  "Mark v.bad words for tilde crunch", this->params()),
275  BOOL_MEMBER(hocr_font_info, false, "Add font info to hocr output",
276  this->params()),
277  BOOL_MEMBER(hocr_char_boxes, false, "Add coordinates for each character to hocr output",
278  this->params()),
279  BOOL_MEMBER(crunch_early_merge_tess_fails, true, "Before word crunch?",
280  this->params()),
281  BOOL_MEMBER(crunch_early_convert_bad_unlv_chs, false,
282  "Take out ~^ early?", this->params()),
283  double_MEMBER(crunch_terrible_rating, 80.0, "crunch rating lt this",
284  this->params()),
285  BOOL_MEMBER(crunch_terrible_garbage, true, "As it says", this->params()),
286  double_MEMBER(crunch_poor_garbage_cert, -9.0,
287  "crunch garbage cert lt this", this->params()),
288  double_MEMBER(crunch_poor_garbage_rate, 60,
289  "crunch garbage rating lt this", this->params()),
290  double_MEMBER(crunch_pot_poor_rate, 40, "POTENTIAL crunch rating lt this",
291  this->params()),
292  double_MEMBER(crunch_pot_poor_cert, -8.0, "POTENTIAL crunch cert lt this",
293  this->params()),
294  BOOL_MEMBER(crunch_pot_garbage, true, "POTENTIAL crunch garbage",
295  this->params()),
296  double_MEMBER(crunch_del_rating, 60, "POTENTIAL crunch rating lt this",
297  this->params()),
298  double_MEMBER(crunch_del_cert, -10.0, "POTENTIAL crunch cert lt this",
299  this->params()),
300  double_MEMBER(crunch_del_min_ht, 0.7, "Del if word ht lt xht x this",
301  this->params()),
302  double_MEMBER(crunch_del_max_ht, 3.0, "Del if word ht gt xht x this",
303  this->params()),
304  double_MEMBER(crunch_del_min_width, 3.0,
305  "Del if word width lt xht x this", this->params()),
306  double_MEMBER(crunch_del_high_word, 1.5,
307  "Del if word gt xht x this above bl", this->params()),
308  double_MEMBER(crunch_del_low_word, 0.5,
309  "Del if word gt xht x this below bl", this->params()),
310  double_MEMBER(crunch_small_outlines_size, 0.6, "Small if lt xht x this",
311  this->params()),
312  INT_MEMBER(crunch_rating_max, 10, "For adj length in rating per ch",
313  this->params()),
314  INT_MEMBER(crunch_pot_indicators, 1,
315  "How many potential indicators needed", this->params()),
316  BOOL_MEMBER(crunch_leave_ok_strings, true, "Don't touch sensible strings",
317  this->params()),
318  BOOL_MEMBER(crunch_accept_ok, true, "Use acceptability in okstring",
319  this->params()),
320  BOOL_MEMBER(crunch_leave_accept_strings, false,
321  "Don't pot crunch sensible strings", this->params()),
322  BOOL_MEMBER(crunch_include_numerals, false, "Fiddle alpha figures",
323  this->params()),
324  INT_MEMBER(crunch_leave_lc_strings, 4,
325  "Don't crunch words with long lower case strings",
326  this->params()),
327  INT_MEMBER(crunch_leave_uc_strings, 4,
328  "Don't crunch words with long lower case strings",
329  this->params()),
330  INT_MEMBER(crunch_long_repetitions, 3,
331  "Crunch words with long repetitions", this->params()),
332  INT_MEMBER(crunch_debug, 0, "As it says", this->params()),
333  INT_MEMBER(fixsp_non_noise_limit, 1,
334  "How many non-noise blbs either side?", this->params()),
335  double_MEMBER(fixsp_small_outlines_size, 0.28, "Small if lt xht x this",
336  this->params()),
337  BOOL_MEMBER(tessedit_prefer_joined_punct, false,
338  "Reward punctuation joins", this->params()),
339  INT_MEMBER(fixsp_done_mode, 1, "What constitues done for spacing",
340  this->params()),
341  INT_MEMBER(debug_fix_space_level, 0, "Contextual fixspace debug",
342  this->params()),
343  STRING_MEMBER(numeric_punctuation, ".,",
344  "Punct. chs expected WITHIN numbers", this->params()),
345  INT_MEMBER(x_ht_acceptance_tolerance, 8,
346  "Max allowed deviation of blob top outside of font data",
347  this->params()),
348  INT_MEMBER(x_ht_min_change, 8,
349  "Min change in xht before actually trying it", this->params()),
350  INT_MEMBER(superscript_debug, 0,
351  "Debug level for sub & superscript fixer", this->params()),
353  superscript_worse_certainty, 2.0,
354  "How many times worse "
355  "certainty does a superscript position glyph need to be for "
356  "us to try classifying it as a char with a different "
357  "baseline?",
358  this->params()),
360  superscript_bettered_certainty, 0.97,
361  "What reduction in "
362  "badness do we think sufficient to choose a superscript "
363  "over what we'd thought. For example, a value of 0.6 means "
364  "we want to reduce badness of certainty by at least 40%",
365  this->params()),
366  double_MEMBER(superscript_scaledown_ratio, 0.4,
367  "A superscript scaled down more than this is unbelievably "
368  "small. For example, 0.3 means we expect the font size to "
369  "be no smaller than 30% of the text line font size.",
370  this->params()),
371  double_MEMBER(subscript_max_y_top, 0.5,
372  "Maximum top of a character measured as a multiple of "
373  "x-height above the baseline for us to reconsider whether "
374  "it's a subscript.",
375  this->params()),
376  double_MEMBER(superscript_min_y_bottom, 0.3,
377  "Minimum bottom of a character measured as a multiple of "
378  "x-height above the baseline for us to reconsider whether "
379  "it's a superscript.",
380  this->params()),
381  BOOL_MEMBER(tessedit_write_block_separators, false,
382  "Write block separators in output", this->params()),
383  BOOL_MEMBER(tessedit_write_rep_codes, false, "Write repetition char code",
384  this->params()),
385  BOOL_MEMBER(tessedit_write_unlv, false, "Write .unlv output file",
386  this->params()),
387  BOOL_MEMBER(tessedit_create_txt, false, "Write .txt output file",
388  this->params()),
389  BOOL_MEMBER(tessedit_create_hocr, false, "Write .html hOCR output file",
390  this->params()),
391  BOOL_MEMBER(tessedit_create_alto, false, "Write .xml ALTO file",
392  this->params()),
393  BOOL_MEMBER(tessedit_create_lstmbox, false, "Write .box file for LSTM training",
394  this->params()),
395  BOOL_MEMBER(tessedit_create_tsv, false, "Write .tsv output file",
396  this->params()),
397  BOOL_MEMBER(tessedit_create_wordstrbox, false, "Write WordStr format .box output file",
398  this->params()),
399  BOOL_MEMBER(tessedit_create_pdf, false, "Write .pdf output file",
400  this->params()),
401  BOOL_MEMBER(textonly_pdf, false,
402  "Create PDF with only one invisible text layer",
403  this->params()),
404  INT_MEMBER(jpg_quality, 85, "Set JPEG quality level", this->params()),
405  INT_MEMBER(user_defined_dpi, 0, "Specify DPI for input image",
406  this->params()),
407  INT_MEMBER(min_characters_to_try, 50,
408  "Specify minimum characters to try during OSD",
409  this->params()),
410  STRING_MEMBER(unrecognised_char, "|",
411  "Output char for unidentified blobs", this->params()),
412  INT_MEMBER(suspect_level, 99, "Suspect marker level", this->params()),
413  INT_MEMBER(suspect_space_level, 100,
414  "Min suspect level for rejecting spaces", this->params()),
415  INT_MEMBER(suspect_short_words, 2,
416  "Don't suspect dict wds longer than this", this->params()),
417  BOOL_MEMBER(suspect_constrain_1Il, false, "UNLV keep 1Il chars rejected",
418  this->params()),
419  double_MEMBER(suspect_rating_per_ch, 999.9,
420  "Don't touch bad rating limit", this->params()),
421  double_MEMBER(suspect_accept_rating, -999.9, "Accept good rating limit",
422  this->params()),
423  BOOL_MEMBER(tessedit_minimal_rejection, false,
424  "Only reject tess failures", this->params()),
425  BOOL_MEMBER(tessedit_zero_rejection, false, "Don't reject ANYTHING",
426  this->params()),
427  BOOL_MEMBER(tessedit_word_for_word, false,
428  "Make output have exactly one word per WERD", this->params()),
429  BOOL_MEMBER(tessedit_zero_kelvin_rejection, false,
430  "Don't reject ANYTHING AT ALL", this->params()),
431  BOOL_MEMBER(tessedit_consistent_reps, true,
432  "Force all rep chars the same", this->params()),
433  INT_MEMBER(tessedit_reject_mode, 0, "Rejection algorithm",
434  this->params()),
435  BOOL_MEMBER(tessedit_rejection_debug, false, "Adaption debug",
436  this->params()),
437  BOOL_MEMBER(tessedit_flip_0O, true, "Contextual 0O O0 flips",
438  this->params()),
439  double_MEMBER(tessedit_lower_flip_hyphen, 1.5,
440  "Aspect ratio dot/hyphen test", this->params()),
441  double_MEMBER(tessedit_upper_flip_hyphen, 1.8,
442  "Aspect ratio dot/hyphen test", this->params()),
443  BOOL_MEMBER(rej_trust_doc_dawg, false,
444  "Use DOC dawg in 11l conf. detector", this->params()),
445  BOOL_MEMBER(rej_1Il_use_dict_word, false, "Use dictword test",
446  this->params()),
447  BOOL_MEMBER(rej_1Il_trust_permuter_type, true, "Don't double check",
448  this->params()),
449  BOOL_MEMBER(rej_use_tess_accepted, true, "Individual rejection control",
450  this->params()),
451  BOOL_MEMBER(rej_use_tess_blanks, true, "Individual rejection control",
452  this->params()),
453  BOOL_MEMBER(rej_use_good_perm, true, "Individual rejection control",
454  this->params()),
455  BOOL_MEMBER(rej_use_sensible_wd, false, "Extend permuter check",
456  this->params()),
457  BOOL_MEMBER(rej_alphas_in_number_perm, false, "Extend permuter check",
458  this->params()),
459  double_MEMBER(rej_whole_of_mostly_reject_word_fract, 0.85,
460  "if >this fract", this->params()),
461  INT_MEMBER(tessedit_image_border, 2, "Rej blbs near image edge limit",
462  this->params()),
463  STRING_MEMBER(ok_repeated_ch_non_alphanum_wds, "-?*\075",
464  "Allow NN to unrej", this->params()),
465  STRING_MEMBER(conflict_set_I_l_1, "Il1[]", "Il1 conflict set",
466  this->params()),
467  INT_MEMBER(min_sane_x_ht_pixels, 8, "Reject any x-ht lt or eq than this",
468  this->params()),
469  BOOL_MEMBER(tessedit_create_boxfile, false, "Output text with boxes",
470  this->params()),
471  INT_MEMBER(tessedit_page_number, -1,
472  "-1 -> All pages"
473  " , else specific page to process",
474  this->params()),
475  BOOL_MEMBER(tessedit_write_images, false,
476  "Capture the image from the IPE", this->params()),
477  BOOL_MEMBER(interactive_display_mode, false, "Run interactively?",
478  this->params()),
479  STRING_MEMBER(file_type, ".tif", "Filename extension", this->params()),
480  BOOL_MEMBER(tessedit_override_permuter, true, "According to dict_word",
481  this->params()),
482  STRING_MEMBER(tessedit_load_sublangs, "",
483  "List of languages to load with this one", this->params()),
484  BOOL_MEMBER(tessedit_use_primary_params_model, false,
485  "In multilingual mode use params model of the"
486  " primary language",
487  this->params()),
488  double_MEMBER(min_orientation_margin, 7.0,
489  "Min acceptable orientation margin", this->params()),
490  BOOL_MEMBER(textord_tabfind_show_vlines, false, "Debug line finding",
491  this->params()),
492  BOOL_MEMBER(textord_use_cjk_fp_model, false, "Use CJK fixed pitch model",
493  this->params()),
494  BOOL_MEMBER(poly_allow_detailed_fx, false,
495  "Allow feature extractors to see the original outline",
496  this->params()),
497  BOOL_INIT_MEMBER(tessedit_init_config_only, false,
498  "Only initialize with the config file. Useful if the "
499  "instance is not going to be used for OCR but say only "
500  "for layout analysis.",
501  this->params()),
502  BOOL_MEMBER(textord_equation_detect, false, "Turn on equation detector",
503  this->params()),
504  BOOL_MEMBER(textord_tabfind_vertical_text, true,
505  "Enable vertical detection", this->params()),
506  BOOL_MEMBER(textord_tabfind_force_vertical_text, false,
507  "Force using vertical text page mode", this->params()),
509  textord_tabfind_vertical_text_ratio, 0.5,
510  "Fraction of textlines deemed vertical to use vertical page "
511  "mode",
512  this->params()),
514  textord_tabfind_aligned_gap_fraction, 0.75,
515  "Fraction of height used as a minimum gap for aligned blobs.",
516  this->params()),
517  INT_MEMBER(tessedit_parallelize, 0, "Run in parallel where possible",
518  this->params()),
519  BOOL_MEMBER(preserve_interword_spaces, false,
520  "Preserve multiple interword spaces", this->params()),
521  STRING_MEMBER(page_separator, "\f",
522  "Page separator (default is form feed control character)",
523  this->params()),
524  INT_MEMBER(lstm_choice_mode, 0,
525  "Allows to include alternative symbols choices in the hOCR output. "
526  "Valid input values are 0, 1, 2 and 3. 0 is the default value. "
527  "With 1 the alternative symbol choices per timestep are included. "
528  "With 2 the alternative symbol choices are accumulated per "
529  "character. ",
530  this->params()),
531 
532  backup_config_file_(nullptr),
533  pix_binary_(nullptr),
534  pix_grey_(nullptr),
535  pix_original_(nullptr),
536  pix_thresholds_(nullptr),
537  source_resolution_(0),
538  textord_(this),
539  right_to_left_(false),
540  scaled_color_(nullptr),
541  scaled_factor_(-1),
542  deskew_(1.0f, 0.0f),
543  reskew_(1.0f, 0.0f),
544  most_recently_used_(this),
545  font_table_size_(0),
546  equ_detect_(nullptr),
547 #ifndef ANDROID_BUILD
548  lstm_recognizer_(nullptr),
549 #endif
550  train_line_page_num_(0) {
551 }
552 
554  Clear();
555  pixDestroy(&pix_original_);
556  end_tesseract();
557  sub_langs_.delete_data_pointers();
558 #ifndef ANDROID_BUILD
559  delete lstm_recognizer_;
560  lstm_recognizer_ = nullptr;
561 #endif
562 }
563 
565 {
566  if (0 == Classify::getDict().NumDawgs() && AnyLSTMLang())
567  {
568  if (lstm_recognizer_ && lstm_recognizer_->GetDict())
569  {
570  return *const_cast<Dict*>(lstm_recognizer_->GetDict());
571  }
572  }
573  return Classify::getDict();
574  }
575 
576 
578  STRING debug_name = imagebasename + "_debug.pdf";
579  pixa_debug_.WritePDF(debug_name.string());
580  pixDestroy(&pix_binary_);
581  pixDestroy(&pix_grey_);
582  pixDestroy(&pix_thresholds_);
583  pixDestroy(&scaled_color_);
584  deskew_ = FCOORD(1.0f, 0.0f);
585  reskew_ = FCOORD(1.0f, 0.0f);
586  splitter_.Clear();
587  scaled_factor_ = -1;
588  for (int i = 0; i < sub_langs_.size(); ++i)
589  sub_langs_[i]->Clear();
590 }
591 
592 #ifndef DISABLED_LEGACY_ENGINE
593 
595  equ_detect_ = detector;
596  equ_detect_->SetLangTesseract(this);
597 }
598 
599 // Clear all memory of adaption for this and all subclassifiers.
602  for (int i = 0; i < sub_langs_.size(); ++i) {
603  sub_langs_[i]->ResetAdaptiveClassifierInternal();
604  }
605 }
606 
607 #endif //ndef DISABLED_LEGACY_ENGINE
608 
609 // Clear the document dictionary for this and all subclassifiers.
612  for (int i = 0; i < sub_langs_.size(); ++i) {
613  sub_langs_[i]->getDict().ResetDocumentDictionary();
614  }
615 }
616 
618  // Set the white and blacklists (if any)
620  tessedit_char_whitelist.string(),
621  tessedit_char_unblacklist.string());
622  if (lstm_recognizer_) {
623  UNICHARSET& lstm_unicharset = const_cast<UNICHARSET&> (lstm_recognizer_->GetUnicharset());
624  lstm_unicharset.set_black_and_whitelist(tessedit_char_blacklist.string(),
625  tessedit_char_whitelist.string(),
626  tessedit_char_unblacklist.string());
627  }
628  // Black and white lists should apply to all loaded classifiers.
629  for (int i = 0; i < sub_langs_.size(); ++i) {
630  sub_langs_[i]->unicharset.set_black_and_whitelist(
632  tessedit_char_unblacklist.string());
633  if (sub_langs_[i]->lstm_recognizer_) {
634  UNICHARSET& lstm_unicharset = const_cast<UNICHARSET&> (sub_langs_[i]->lstm_recognizer_->GetUnicharset());
635  lstm_unicharset.set_black_and_whitelist(tessedit_char_blacklist.string(),
636  tessedit_char_whitelist.string(),
637  tessedit_char_unblacklist.string());
638  }
639  }
640 }
641 
642 // Perform steps to prepare underlying binary image/other data structures for
643 // page segmentation.
646  // Find the max splitter strategy over all langs.
647  auto max_pageseg_strategy =
649  static_cast<int32_t>(pageseg_devanagari_split_strategy));
650  for (int i = 0; i < sub_langs_.size(); ++i) {
651  auto pageseg_strategy =
653  static_cast<int32_t>(sub_langs_[i]->pageseg_devanagari_split_strategy));
654  if (pageseg_strategy > max_pageseg_strategy)
655  max_pageseg_strategy = pageseg_strategy;
656  pixDestroy(&sub_langs_[i]->pix_binary_);
657  sub_langs_[i]->pix_binary_ = pixClone(pix_binary());
658  }
659  // Perform shiro-rekha (top-line) splitting and replace the current image by
660  // the newly split image.
661  splitter_.set_orig_pix(pix_binary());
662  splitter_.set_pageseg_split_strategy(max_pageseg_strategy);
663  if (splitter_.Split(true, &pixa_debug_)) {
664  ASSERT_HOST(splitter_.splitted_image());
665  pixDestroy(&pix_binary_);
666  pix_binary_ = pixClone(splitter_.splitted_image());
667  }
668 }
669 
670 // Perform steps to prepare underlying binary image/other data structures for
671 // OCR. The current segmentation is required by this method.
672 // Note that this method resets pix_binary_ to the original binarized image,
673 // which may be different from the image actually used for OCR depending on the
674 // value of devanagari_ocr_split_strategy.
675 void Tesseract::PrepareForTessOCR(BLOCK_LIST* block_list,
676  Tesseract* osd_tess, OSResults* osr) {
677  // Find the max splitter strategy over all langs.
678  auto max_ocr_strategy =
680  static_cast<int32_t>(ocr_devanagari_split_strategy));
681  for (int i = 0; i < sub_langs_.size(); ++i) {
682  auto ocr_strategy =
684  static_cast<int32_t>(sub_langs_[i]->ocr_devanagari_split_strategy));
685  if (ocr_strategy > max_ocr_strategy)
686  max_ocr_strategy = ocr_strategy;
687  }
688  // Utilize the segmentation information available.
689  splitter_.set_segmentation_block_list(block_list);
690  splitter_.set_ocr_split_strategy(max_ocr_strategy);
691  // Run the splitter for OCR
692  bool split_for_ocr = splitter_.Split(false, &pixa_debug_);
693  // Restore pix_binary to the binarized original pix for future reference.
694  ASSERT_HOST(splitter_.orig_pix());
695  pixDestroy(&pix_binary_);
696  pix_binary_ = pixClone(splitter_.orig_pix());
697  // If the pageseg and ocr strategies are different, refresh the block list
698  // (from the last SegmentImage call) with blobs from the real image to be used
699  // for OCR.
700  if (splitter_.HasDifferentSplitStrategies()) {
701  BLOCK block("", true, 0, 0, 0, 0, pixGetWidth(pix_binary_),
702  pixGetHeight(pix_binary_));
703  Pix* pix_for_ocr = split_for_ocr ? splitter_.splitted_image() :
704  splitter_.orig_pix();
705  extract_edges(pix_for_ocr, &block);
706  splitter_.RefreshSegmentationWithNewBlobs(block.blob_list());
707  }
708  // The splitter isn't needed any more after this, so save memory by clearing.
709  splitter_.Clear();
710 }
711 
712 } // namespace tesseract
void ResetAdaptiveClassifierInternal()
Definition: adaptmatch.cpp:598
const Dict * GetDict() const
Definition: strngs.h:45
Definition: points.h:188
#define double_MEMBER(name, val, comment, vec)
Definition: params.h:324
void PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, OSResults *osr)
virtual Dict & getDict()
Definition: classify.h:107
void ResetDocumentDictionary()
Definition: dict.h:317
const UNICHARSET & GetUnicharset() const
#define BOOL_MEMBER(name, val, comment, vec)
Definition: params.h:318
void SetLangTesseract(Tesseract *lang_tesseract)
UNICHARSET unicharset
Definition: ccutil.h:71
void extract_edges(Pix *pix, BLOCK *block)
Definition: edgblob.cpp:330
void SetEquationDetect(EquationDetect *detector)
#define INT_MEMBER(name, val, comment, vec)
Definition: params.h:315
bool Split(bool split_for_pageseg, DebugPixa *pixa_debug)
#define INT_INIT_MEMBER(name, val, comment, vec)
Definition: params.h:327
void set_black_and_whitelist(const char *blacklist, const char *whitelist, const char *unblacklist)
const char * string() const
Definition: strngs.cpp:194
Dict & getDict() override
void set_pageseg_split_strategy(SplitStrategy strategy)
void set_segmentation_block_list(BLOCK_LIST *block_list)
bool AnyLSTMLang() const
#define ASSERT_HOST(x)
Definition: errcode.h:88
Pix * pix_binary() const
#define STRING_MEMBER(name, val, comment, vec)
Definition: params.h:321
void set_ocr_split_strategy(SplitStrategy strategy)
void RefreshSegmentationWithNewBlobs(C_BLOB_LIST *new_blobs)
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:172
void set_use_cjk_fp_model(bool flag)
Definition: textord.h:95
C_BLOB_LIST * blob_list()
get blobs
Definition: ocrblock.h:129
void WritePDF(const char *filename)
Definition: debugpixa.h:36
#define BOOL_INIT_MEMBER(name, val, comment, vec)
Definition: params.h:330
Definition: ocrblock.h:29
STRING imagebasename
Definition: ccutil.h:68