tesseract  4.0.0-1-g2a2b
classify.cpp
Go to the documentation of this file.
1 // File: classify.cpp
3 // Description: classify class.
4 // Author: Samuel Charron
5 //
6 // (C) Copyright 2006, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 
19 #include "classify.h"
20 
21 #ifdef DISABLED_LEGACY_ENGINE
22 
23 #include <string.h>
24 
25 namespace tesseract {
26 
28  :
29  INT_MEMBER(classify_debug_level, 0, "Classify debug level",
30  this->params()),
31 
32  BOOL_MEMBER(classify_bln_numeric_mode, 0,
33 "Assume the input is numbers [0-9].", this->params()),
34 
35  double_MEMBER(classify_max_rating_ratio, 1.5,
36  "Veto ratio between classifier ratings", this->params()),
37 
38  double_MEMBER(classify_max_certainty_margin, 5.5,
39  "Veto difference between classifier certainties",
40  this->params()),
41 
42  dict_(this) {}
43 
44 Classify::~Classify() {}
45 
46 } // namespace tesseract
47 
48 #else // DISABLED_LEGACY_ENGINE not defined
49 
50 #include "fontinfo.h"
51 #include "intproto.h"
52 #include "mfoutline.h"
53 #include "scrollview.h"
54 #include "shapeclassifier.h"
55 #include "shapetable.h"
56 #include "unicity_table.h"
57 #include <cstring>
58 
59 namespace tesseract {
61  : BOOL_MEMBER(allow_blob_division, true, "Use divisible blobs chopping",
62  this->params()),
63  BOOL_MEMBER(prioritize_division, FALSE,
64  "Prioritize blob division over chopping", this->params()),
65  INT_MEMBER(tessedit_single_match, FALSE, "Top choice only from CP",
66  this->params()),
67  BOOL_MEMBER(classify_enable_learning, true, "Enable adaptive classifier",
68  this->params()),
69  INT_MEMBER(classify_debug_level, 0, "Classify debug level",
70  this->params()),
71  INT_MEMBER(classify_norm_method, character, "Normalization Method ...",
72  this->params()),
73  double_MEMBER(classify_char_norm_range, 0.2,
74  "Character Normalization Range ...", this->params()),
75  double_MEMBER(classify_min_norm_scale_x, 0.0, "Min char x-norm scale ...",
76  this->params()), /* PREV DEFAULT 0.1 */
77  double_MEMBER(classify_max_norm_scale_x, 0.325,
78  "Max char x-norm scale ...",
79  this->params()), /* PREV DEFAULT 0.3 */
80  double_MEMBER(classify_min_norm_scale_y, 0.0, "Min char y-norm scale ...",
81  this->params()), /* PREV DEFAULT 0.1 */
82  double_MEMBER(classify_max_norm_scale_y, 0.325,
83  "Max char y-norm scale ...",
84  this->params()), /* PREV DEFAULT 0.3 */
85  double_MEMBER(classify_max_rating_ratio, 1.5,
86  "Veto ratio between classifier ratings", this->params()),
87  double_MEMBER(classify_max_certainty_margin, 5.5,
88  "Veto difference between classifier certainties",
89  this->params()),
90  BOOL_MEMBER(tess_cn_matching, 0, "Character Normalized Matching",
91  this->params()),
92  BOOL_MEMBER(tess_bn_matching, 0, "Baseline Normalized Matching",
93  this->params()),
94  BOOL_MEMBER(classify_enable_adaptive_matcher, 1,
95  "Enable adaptive classifier", this->params()),
96  BOOL_MEMBER(classify_use_pre_adapted_templates, 0,
97  "Use pre-adapted classifier templates", this->params()),
98  BOOL_MEMBER(classify_save_adapted_templates, 0,
99  "Save adapted templates to a file", this->params()),
100  BOOL_MEMBER(classify_enable_adaptive_debugger, 0, "Enable match debugger",
101  this->params()),
102  BOOL_MEMBER(classify_nonlinear_norm, 0,
103  "Non-linear stroke-density normalization", this->params()),
104  INT_MEMBER(matcher_debug_level, 0, "Matcher Debug Level", this->params()),
105  INT_MEMBER(matcher_debug_flags, 0, "Matcher Debug Flags", this->params()),
106  INT_MEMBER(classify_learning_debug_level, 0, "Learning Debug Level: ",
107  this->params()),
108  double_MEMBER(matcher_good_threshold, 0.125, "Good Match (0-1)",
109  this->params()),
110  double_MEMBER(matcher_reliable_adaptive_result, 0.0, "Great Match (0-1)",
111  this->params()),
112  double_MEMBER(matcher_perfect_threshold, 0.02, "Perfect Match (0-1)",
113  this->params()),
114  double_MEMBER(matcher_bad_match_pad, 0.15, "Bad Match Pad (0-1)",
115  this->params()),
116  double_MEMBER(matcher_rating_margin, 0.1, "New template margin (0-1)",
117  this->params()),
118  double_MEMBER(matcher_avg_noise_size, 12.0, "Avg. noise blob length",
119  this->params()),
120  INT_MEMBER(matcher_permanent_classes_min, 1, "Min # of permanent classes",
121  this->params()),
122  INT_MEMBER(matcher_min_examples_for_prototyping, 3,
123  "Reliable Config Threshold", this->params()),
124  INT_MEMBER(matcher_sufficient_examples_for_prototyping, 5,
125  "Enable adaption even if the ambiguities have not been seen",
126  this->params()),
127  double_MEMBER(matcher_clustering_max_angle_delta, 0.015,
128  "Maximum angle delta for prototype clustering",
129  this->params()),
130  double_MEMBER(classify_misfit_junk_penalty, 0.0,
131  "Penalty to apply when a non-alnum is vertically out of "
132  "its expected textline position",
133  this->params()),
134  double_MEMBER(rating_scale, 1.5, "Rating scaling factor", this->params()),
135  double_MEMBER(certainty_scale, 20.0, "Certainty scaling factor",
136  this->params()),
137  double_MEMBER(tessedit_class_miss_scale, 0.00390625,
138  "Scale factor for features not used", this->params()),
140  classify_adapted_pruning_factor, 2.5,
141  "Prune poor adapted results this much worse than best result",
142  this->params()),
143  double_MEMBER(classify_adapted_pruning_threshold, -1.0,
144  "Threshold at which classify_adapted_pruning_factor starts",
145  this->params()),
146  INT_MEMBER(classify_adapt_proto_threshold, 230,
147  "Threshold for good protos during adaptive 0-255",
148  this->params()),
149  INT_MEMBER(classify_adapt_feature_threshold, 230,
150  "Threshold for good features during adaptive 0-255",
151  this->params()),
153  "Do not include character fragments in the"
154  " results of the classifier",
155  this->params()),
156  double_MEMBER(classify_character_fragments_garbage_certainty_threshold,
157  -3.0,
158  "Exclude fragments that do not look like whole"
159  " characters from training and adaption",
160  this->params()),
161  BOOL_MEMBER(classify_debug_character_fragments, FALSE,
162  "Bring up graphical debugging windows for fragments training",
163  this->params()),
164  BOOL_MEMBER(matcher_debug_separate_windows, FALSE,
165  "Use two different windows for debugging the matching: "
166  "One for the protos and one for the features.",
167  this->params()),
168  STRING_MEMBER(classify_learn_debug_str, "", "Class str to debug learning",
169  this->params()),
170  INT_MEMBER(classify_class_pruner_threshold, 229,
171  "Class Pruner Threshold 0-255", this->params()),
172  INT_MEMBER(classify_class_pruner_multiplier, 15,
173  "Class Pruner Multiplier 0-255: ", this->params()),
174  INT_MEMBER(classify_cp_cutoff_strength, 7,
175  "Class Pruner CutoffStrength: ", this->params()),
177  "Integer Matcher Multiplier 0-255: ", this->params()),
178  EnableLearning(true),
179  INT_MEMBER(il1_adaption_test, 0,
180  "Don't adapt to i/I at beginning of word", this->params()),
181  BOOL_MEMBER(classify_bln_numeric_mode, 0,
182  "Assume the input is numbers [0-9].", this->params()),
183  double_MEMBER(speckle_large_max_size, 0.30, "Max large speckle size",
184  this->params()),
185  double_MEMBER(speckle_rating_penalty, 10.0,
186  "Penalty to add to worst rating for noise", this->params()),
187  im_(&classify_debug_level),
188  shape_table_(nullptr),
189  dict_(this),
190  static_classifier_(nullptr) {
191  fontinfo_table_.set_compare_callback(
193  fontinfo_table_.set_clear_callback(
195  fontset_table_.set_compare_callback(
197  fontset_table_.set_clear_callback(
199  AdaptedTemplates = nullptr;
200  BackupAdaptedTemplates = nullptr;
201  PreTrainedTemplates = nullptr;
202  AllProtosOn = nullptr;
203  AllConfigsOn = nullptr;
204  AllConfigsOff = nullptr;
205  TempProtoMask = nullptr;
206  NormProtos = nullptr;
207 
208  NumAdaptationsFailed = 0;
209 
210  learn_debug_win_ = nullptr;
211  learn_fragmented_word_debug_win_ = nullptr;
212  learn_fragments_debug_win_ = nullptr;
213 }
214 
217  delete learn_debug_win_;
218  delete learn_fragmented_word_debug_win_;
219  delete learn_fragments_debug_win_;
220 }
221 
222 
223 // Takes ownership of the given classifier, and uses it for future calls
224 // to CharNormClassifier.
226  delete static_classifier_;
227  static_classifier_ = static_classifier;
228 }
229 
230 // Moved from speckle.cpp
231 // Adds a noise classification result that is a bit worse than the worst
232 // current result, or the worst possible result if no current results.
233 void Classify::AddLargeSpeckleTo(int blob_length, BLOB_CHOICE_LIST *choices) {
234  BLOB_CHOICE_IT bc_it(choices);
235  // If there is no classifier result, we will use the worst possible certainty
236  // and corresponding rating.
237  float certainty = -getDict().certainty_scale;
238  float rating = rating_scale * blob_length;
239  if (!choices->empty() && blob_length > 0) {
240  bc_it.move_to_last();
241  BLOB_CHOICE* worst_choice = bc_it.data();
242  // Add speckle_rating_penalty to worst rating, matching old value.
243  rating = worst_choice->rating() + speckle_rating_penalty;
244  // Compute the rating to correspond to the certainty. (Used to be kept
245  // the same, but that messes up the language model search.)
246  certainty = -rating * getDict().certainty_scale /
247  (rating_scale * blob_length);
248  }
249  BLOB_CHOICE* blob_choice = new BLOB_CHOICE(UNICHAR_SPACE, rating, certainty,
250  -1, 0.0f, FLT_MAX, 0,
252  bc_it.add_to_end(blob_choice);
253 }
254 
255 // Returns true if the blob is small enough to be a large speckle.
256 bool Classify::LargeSpeckle(const TBLOB &blob) {
257  double speckle_size = kBlnXHeight * speckle_large_max_size;
258  TBOX bbox = blob.bounding_box();
259  return bbox.width() < speckle_size && bbox.height() < speckle_size;
260 }
261 
262 } // namespace tesseract
263 
264 #endif // def DISABLED_LEGACY_ENGINE
bool LargeSpeckle(const TBLOB &blob)
Definition: classify.cpp:256
void SetStaticClassifier(ShapeClassifier *static_classifier)
Definition: classify.cpp:225
#define STRING_MEMBER(name, val, comment, vec)
Definition: params.h:294
#define TRUE
Definition: capi.h:51
NORM_PROTOS * NormProtos
Definition: classify.h:527
BIT_VECTOR AllProtosOn
Definition: classify.h:521
double certainty_scale
Definition: dict.h:611
Definition: rect.h:34
const int kBlnXHeight
Definition: normalis.h:24
#define INT_MEMBER(name, val, comment, vec)
Definition: params.h:288
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
bool CompareFontSet(const FontSet &fs1, const FontSet &fs2)
Definition: fontinfo.cpp:128
#define BOOL_MEMBER(name, val, comment, vec)
Definition: params.h:291
void AddLargeSpeckleTo(int blob_length, BLOB_CHOICE_LIST *choices)
Definition: classify.cpp:233
int16_t width() const
Definition: rect.h:115
#define double_MEMBER(name, val, comment, vec)
Definition: params.h:297
bool CompareFontInfo(const FontInfo &fi1, const FontInfo &fi2)
Definition: fontinfo.cpp:120
INT_TEMPLATES PreTrainedTemplates
Definition: classify.h:510
BIT_VECTOR AllConfigsOff
Definition: classify.h:523
#define FALSE
Definition: capi.h:52
double speckle_large_max_size
Definition: classify.h:542
void FontSetDeleteCallback(FontSet fs)
Definition: fontinfo.cpp:146
ADAPT_TEMPLATES BackupAdaptedTemplates
Definition: classify.h:518
ADAPT_TEMPLATES AdaptedTemplates
Definition: classify.h:514
TBOX bounding_box() const
Definition: blobs.cpp:478
void EndAdaptiveClassifier()
Definition: adaptmatch.cpp:460
void FontInfoDeleteCallback(FontInfo f)
Definition: fontinfo.cpp:139
float rating() const
Definition: ratngs.h:80
virtual ~Classify()
Definition: classify.cpp:215
UnicityTable< FontInfo > fontinfo_table_
Definition: classify.h:529
BIT_VECTOR AllConfigsOn
Definition: classify.h:522
BIT_VECTOR TempProtoMask
Definition: classify.h:524
virtual Dict & getDict()
Definition: classify.h:107
double speckle_rating_penalty
Definition: classify.h:544
bool disable_character_fragments
Definition: blobs.h:268
int classify_integer_matcher_multiplier
int16_t height() const
Definition: rect.h:108
UnicityTable< FontSet > fontset_table_
Definition: classify.h:537