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