/src/tesseract/src/ccmain/osdetect.cpp
Line | Count | Source |
1 | | /////////////////////////////////////////////////////////////////////// |
2 | | // File: osdetect.cpp |
3 | | // Description: Orientation and script detection. |
4 | | // Author: Samuel Charron |
5 | | // Ranjith Unnikrishnan |
6 | | // |
7 | | // (C) Copyright 2008, Google Inc. |
8 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | // you may not use this file except in compliance with the License. |
10 | | // You may obtain a copy of the License at |
11 | | // http://www.apache.org/licenses/LICENSE-2.0 |
12 | | // Unless required by applicable law or agreed to in writing, software |
13 | | // distributed under the License is distributed on an "AS IS" BASIS, |
14 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | // See the License for the specific language governing permissions and |
16 | | // limitations under the License. |
17 | | // |
18 | | /////////////////////////////////////////////////////////////////////// |
19 | | |
20 | | #include <tesseract/osdetect.h> |
21 | | |
22 | | #include "blobbox.h" |
23 | | #include "blread.h" |
24 | | #include "colfind.h" |
25 | | #include "fontinfo.h" |
26 | | #include "imagefind.h" |
27 | | #include "linefind.h" |
28 | | #include "oldlist.h" |
29 | | #include "qrsequence.h" |
30 | | #include "ratngs.h" |
31 | | #include "tabvector.h" |
32 | | #include "tesseractclass.h" |
33 | | #include "textord.h" |
34 | | |
35 | | #include <algorithm> |
36 | | #include <cmath> // for std::fabs |
37 | | #include <memory> |
38 | | |
39 | | namespace tesseract { |
40 | | |
41 | | const float kSizeRatioToReject = 2.0; |
42 | | const int kMinAcceptableBlobHeight = 10; |
43 | | |
44 | | const float kScriptAcceptRatio = 1.3; |
45 | | |
46 | | const float kHanRatioInKorean = 0.7; |
47 | | const float kHanRatioInJapanese = 0.3; |
48 | | |
49 | | const float kNonAmbiguousMargin = 1.0; |
50 | | |
51 | 0 | void OSResults::update_best_orientation() { |
52 | 0 | float first = orientations[0]; |
53 | 0 | float second = orientations[1]; |
54 | 0 | best_result.orientation_id = 0; |
55 | 0 | if (orientations[0] < orientations[1]) { |
56 | 0 | first = orientations[1]; |
57 | 0 | second = orientations[0]; |
58 | 0 | best_result.orientation_id = 1; |
59 | 0 | } |
60 | 0 | for (int i = 2; i < 4; ++i) { |
61 | 0 | if (orientations[i] > first) { |
62 | 0 | second = first; |
63 | 0 | first = orientations[i]; |
64 | 0 | best_result.orientation_id = i; |
65 | 0 | } else if (orientations[i] > second) { |
66 | 0 | second = orientations[i]; |
67 | 0 | } |
68 | 0 | } |
69 | | // Store difference of top two orientation scores. |
70 | 0 | best_result.oconfidence = first - second; |
71 | 0 | } |
72 | | |
73 | 0 | void OSResults::set_best_orientation(int orientation_id) { |
74 | 0 | best_result.orientation_id = orientation_id; |
75 | 0 | best_result.oconfidence = 0; |
76 | 0 | } |
77 | | |
78 | 0 | void OSResults::update_best_script(int orientation) { |
79 | | // We skip index 0 to ignore the "Common" script. |
80 | 0 | float first = scripts_na[orientation][1]; |
81 | 0 | float second = scripts_na[orientation][2]; |
82 | 0 | best_result.script_id = 1; |
83 | 0 | if (scripts_na[orientation][1] < scripts_na[orientation][2]) { |
84 | 0 | first = scripts_na[orientation][2]; |
85 | 0 | second = scripts_na[orientation][1]; |
86 | 0 | best_result.script_id = 2; |
87 | 0 | } |
88 | 0 | for (int i = 3; i < kMaxNumberOfScripts; ++i) { |
89 | 0 | if (scripts_na[orientation][i] > first) { |
90 | 0 | best_result.script_id = i; |
91 | 0 | second = first; |
92 | 0 | first = scripts_na[orientation][i]; |
93 | 0 | } else if (scripts_na[orientation][i] > second) { |
94 | 0 | second = scripts_na[orientation][i]; |
95 | 0 | } |
96 | 0 | } |
97 | 0 | best_result.sconfidence = |
98 | 0 | (second == 0.0f) ? 2.0f : (first / second - 1.0) / (kScriptAcceptRatio - 1.0); |
99 | 0 | } |
100 | | |
101 | 0 | int OSResults::get_best_script(int orientation_id) const { |
102 | 0 | int max_id = -1; |
103 | 0 | for (int j = 0; j < kMaxNumberOfScripts; ++j) { |
104 | 0 | const char *script = unicharset->get_script_from_script_id(j); |
105 | 0 | if (strcmp(script, "Common") && strcmp(script, "NULL")) { |
106 | 0 | if (max_id == -1 || scripts_na[orientation_id][j] > scripts_na[orientation_id][max_id]) { |
107 | 0 | max_id = j; |
108 | 0 | } |
109 | 0 | } |
110 | 0 | } |
111 | 0 | return max_id; |
112 | 0 | } |
113 | | |
114 | | // Print the script scores for all possible orientations. |
115 | 0 | void OSResults::print_scores(void) const { |
116 | 0 | for (int i = 0; i < 4; ++i) { |
117 | 0 | tprintf("Orientation id #%d", i); |
118 | 0 | print_scores(i); |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | // Print the script scores for the given candidate orientation. |
123 | 0 | void OSResults::print_scores(int orientation_id) const { |
124 | 0 | for (int j = 0; j < kMaxNumberOfScripts; ++j) { |
125 | 0 | if (scripts_na[orientation_id][j]) { |
126 | 0 | tprintf("%12s\t: %f\n", unicharset->get_script_from_script_id(j), |
127 | 0 | scripts_na[orientation_id][j]); |
128 | 0 | } |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | // Accumulate scores with given OSResults instance and update the best script. |
133 | 0 | void OSResults::accumulate(const OSResults &osr) { |
134 | 0 | for (int i = 0; i < 4; ++i) { |
135 | 0 | orientations[i] += osr.orientations[i]; |
136 | 0 | for (int j = 0; j < kMaxNumberOfScripts; ++j) { |
137 | 0 | scripts_na[i][j] += osr.scripts_na[i][j]; |
138 | 0 | } |
139 | 0 | } |
140 | 0 | unicharset = osr.unicharset; |
141 | 0 | update_best_orientation(); |
142 | 0 | update_best_script(best_result.orientation_id); |
143 | 0 | } |
144 | | |
145 | | // Detect and erase horizontal/vertical lines and picture regions from the |
146 | | // image, so that non-text blobs are removed from consideration. |
147 | | static void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *blocks, |
148 | 0 | TO_BLOCK_LIST *to_blocks) { |
149 | 0 | Image pix = tess->pix_binary(); |
150 | 0 | ASSERT_HOST(pix != nullptr); |
151 | 0 | int vertical_x = 0; |
152 | 0 | int vertical_y = 1; |
153 | 0 | tesseract::TabVector_LIST v_lines; |
154 | 0 | tesseract::TabVector_LIST h_lines; |
155 | 0 | int resolution; |
156 | 0 | if (kMinCredibleResolution > pixGetXRes(pix)) { |
157 | 0 | resolution = kMinCredibleResolution; |
158 | 0 | tprintf("Warning. Invalid resolution %d dpi. Using %d instead.\n", pixGetXRes(pix), resolution); |
159 | 0 | } else { |
160 | 0 | resolution = pixGetXRes(pix); |
161 | 0 | } |
162 | |
|
163 | 0 | tesseract::LineFinder::FindAndRemoveLines(resolution, false, pix, &vertical_x, &vertical_y, |
164 | 0 | nullptr, &v_lines, &h_lines); |
165 | 0 | Image im_pix = tesseract::ImageFind::FindImages(pix, nullptr); |
166 | 0 | if (im_pix != nullptr) { |
167 | 0 | pixSubtract(pix, pix, im_pix); |
168 | 0 | im_pix.destroy(); |
169 | 0 | } |
170 | 0 | tess->mutable_textord()->find_components(tess->pix_binary(), blocks, to_blocks); |
171 | 0 | } |
172 | | |
173 | | // Find connected components in the page and process a subset until finished or |
174 | | // a stopping criterion is met. |
175 | | // Returns the number of blobs used in making the estimate. 0 implies failure. |
176 | | int orientation_and_script_detection(const char *filename, OSResults *osr, |
177 | 0 | tesseract::Tesseract *tess) { |
178 | 0 | std::string name = filename; // truncated name |
179 | |
|
180 | 0 | const char *lastdot = strrchr(name.c_str(), '.'); |
181 | 0 | if (lastdot != nullptr) { |
182 | 0 | name[lastdot - name.c_str()] = '\0'; |
183 | 0 | } |
184 | |
|
185 | 0 | ASSERT_HOST(tess->pix_binary() != nullptr); |
186 | 0 | int width = pixGetWidth(tess->pix_binary()); |
187 | 0 | int height = pixGetHeight(tess->pix_binary()); |
188 | |
|
189 | 0 | BLOCK_LIST blocks; |
190 | 0 | if (!read_unlv_file(name, width, height, &blocks)) { |
191 | 0 | FullPageBlock(width, height, &blocks); |
192 | 0 | } |
193 | | |
194 | | // Try to remove non-text regions from consideration. |
195 | 0 | TO_BLOCK_LIST land_blocks, port_blocks; |
196 | 0 | remove_nontext_regions(tess, &blocks, &port_blocks); |
197 | |
|
198 | 0 | if (port_blocks.empty()) { |
199 | | // page segmentation did not succeed, so we need to find_components first. |
200 | 0 | tess->mutable_textord()->find_components(tess->pix_binary(), &blocks, &port_blocks); |
201 | 0 | } else { |
202 | 0 | TBOX page_box(0, 0, width, height); |
203 | | // Filter_blobs sets up the TO_BLOCKs the same as find_components does. |
204 | 0 | tess->mutable_textord()->filter_blobs(page_box.topright(), &port_blocks, true); |
205 | 0 | } |
206 | |
|
207 | 0 | return os_detect(&port_blocks, osr, tess); |
208 | 0 | } |
209 | | |
210 | | // Filter and sample the blobs. |
211 | | // Returns a non-zero number of blobs if the page was successfully processed, or |
212 | | // zero if the page had too few characters to be reliable |
213 | 0 | int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess) { |
214 | | #if !defined(NDEBUG) |
215 | | int blobs_total = 0; |
216 | | #endif |
217 | 0 | TO_BLOCK_IT block_it; |
218 | 0 | block_it.set_to_list(port_blocks); |
219 | |
|
220 | 0 | BLOBNBOX_CLIST filtered_list; |
221 | 0 | BLOBNBOX_C_IT filtered_it(&filtered_list); |
222 | |
|
223 | 0 | for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) { |
224 | 0 | TO_BLOCK *to_block = block_it.data(); |
225 | 0 | if (to_block->block->pdblk.poly_block() && !to_block->block->pdblk.poly_block()->IsText()) { |
226 | 0 | continue; |
227 | 0 | } |
228 | 0 | BLOBNBOX_IT bbox_it; |
229 | 0 | bbox_it.set_to_list(&to_block->blobs); |
230 | 0 | for (bbox_it.mark_cycle_pt(); !bbox_it.cycled_list(); bbox_it.forward()) { |
231 | 0 | BLOBNBOX *bbox = bbox_it.data(); |
232 | 0 | C_BLOB *blob = bbox->cblob(); |
233 | 0 | TBOX box = blob->bounding_box(); |
234 | | #if !defined(NDEBUG) |
235 | | ++blobs_total; |
236 | | #endif |
237 | | |
238 | | // Catch illegal value of box width and avoid division by zero. |
239 | 0 | if (box.width() == 0) { |
240 | 0 | continue; |
241 | 0 | } |
242 | | // TODO: Can height and width be negative? If not, remove fabs. |
243 | 0 | float y_x = std::fabs((box.height() * 1.0f) / box.width()); |
244 | 0 | float x_y = 1.0f / y_x; |
245 | | // Select a >= 1.0 ratio |
246 | 0 | float ratio = x_y > y_x ? x_y : y_x; |
247 | | // Blob is ambiguous |
248 | 0 | if (ratio > kSizeRatioToReject) { |
249 | 0 | continue; |
250 | 0 | } |
251 | 0 | if (box.height() < kMinAcceptableBlobHeight) { |
252 | 0 | continue; |
253 | 0 | } |
254 | 0 | filtered_it.add_to_end(bbox); |
255 | 0 | } |
256 | 0 | } |
257 | 0 | return os_detect_blobs(nullptr, &filtered_list, osr, tess); |
258 | 0 | } |
259 | | |
260 | | // Detect orientation and script from a list of blobs. |
261 | | // Returns a non-zero number of blobs if the list was successfully processed, or |
262 | | // zero if the list had too few characters to be reliable. |
263 | | // If allowed_scripts is non-null and non-empty, it is a list of scripts that |
264 | | // constrains both orientation and script detection to consider only scripts |
265 | | // from the list. |
266 | | int os_detect_blobs(const std::vector<int> *allowed_scripts, BLOBNBOX_CLIST *blob_list, |
267 | 0 | OSResults *osr, tesseract::Tesseract *tess) { |
268 | 0 | OSResults osr_; |
269 | 0 | int minCharactersToTry = tess->min_characters_to_try; |
270 | 0 | int maxCharactersToTry = 5 * minCharactersToTry; |
271 | 0 | if (osr == nullptr) { |
272 | 0 | osr = &osr_; |
273 | 0 | } |
274 | |
|
275 | 0 | osr->unicharset = &tess->unicharset; |
276 | 0 | OrientationDetector o(allowed_scripts, osr); |
277 | 0 | ScriptDetector s(allowed_scripts, osr, tess); |
278 | |
|
279 | 0 | BLOBNBOX_C_IT filtered_it(blob_list); |
280 | 0 | int real_max = std::min(filtered_it.length(), maxCharactersToTry); |
281 | | // tprintf("Total blobs found = %d\n", blobs_total); |
282 | | // tprintf("Number of blobs post-filtering = %d\n", filtered_it.length()); |
283 | | // tprintf("Number of blobs to try = %d\n", real_max); |
284 | | |
285 | | // If there are too few characters, skip this page entirely. |
286 | 0 | if (real_max < minCharactersToTry / 2) { |
287 | 0 | tprintf("Too few characters. Skipping this page\n"); |
288 | 0 | return 0; |
289 | 0 | } |
290 | | |
291 | 0 | auto **blobs = new BLOBNBOX *[filtered_it.length()]; |
292 | 0 | int number_of_blobs = 0; |
293 | 0 | for (filtered_it.mark_cycle_pt(); !filtered_it.cycled_list(); filtered_it.forward()) { |
294 | 0 | blobs[number_of_blobs++] = filtered_it.data(); |
295 | 0 | } |
296 | 0 | QRSequenceGenerator sequence(number_of_blobs); |
297 | 0 | int num_blobs_evaluated = 0; |
298 | 0 | for (int i = 0; i < real_max; ++i) { |
299 | 0 | if (os_detect_blob(blobs[sequence.GetVal()], &o, &s, osr, tess) && i > minCharactersToTry) { |
300 | 0 | break; |
301 | 0 | } |
302 | 0 | ++num_blobs_evaluated; |
303 | 0 | } |
304 | 0 | delete[] blobs; |
305 | | |
306 | | // Make sure the best_result is up-to-date |
307 | 0 | int orientation = o.get_orientation(); |
308 | 0 | osr->update_best_script(orientation); |
309 | 0 | return num_blobs_evaluated; |
310 | 0 | } |
311 | | |
312 | | // Processes a single blob to estimate script and orientation. |
313 | | // Return true if estimate of orientation and script satisfies stopping |
314 | | // criteria. |
315 | | bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *osr, |
316 | 0 | tesseract::Tesseract *tess) { |
317 | 0 | tess->tess_cn_matching.set_value(true); // turn it on |
318 | 0 | tess->tess_bn_matching.set_value(false); |
319 | 0 | C_BLOB *blob = bbox->cblob(); |
320 | 0 | TBLOB *tblob = TBLOB::PolygonalCopy(tess->poly_allow_detailed_fx, blob); |
321 | 0 | TBOX box = tblob->bounding_box(); |
322 | 0 | FCOORD current_rotation(1.0f, 0.0f); |
323 | 0 | FCOORD rotation90(0.0f, 1.0f); |
324 | 0 | BLOB_CHOICE_LIST ratings[4]; |
325 | | // Test the 4 orientations |
326 | 0 | for (int i = 0; i < 4; ++i) { |
327 | | // Normalize the blob. Set the origin to the place we want to be the |
328 | | // bottom-middle after rotation. |
329 | | // Scaling is to make the rotated height the x-height. |
330 | 0 | float scaling = static_cast<float>(kBlnXHeight) / box.height(); |
331 | 0 | float x_origin = (box.left() + box.right()) / 2.0f; |
332 | 0 | float y_origin = (box.bottom() + box.top()) / 2.0f; |
333 | 0 | if (i == 0 || i == 2) { |
334 | | // Rotation is 0 or 180. |
335 | 0 | y_origin = i == 0 ? box.bottom() : box.top(); |
336 | 0 | } else { |
337 | | // Rotation is 90 or 270. |
338 | 0 | scaling = static_cast<float>(kBlnXHeight) / box.width(); |
339 | 0 | x_origin = i == 1 ? box.left() : box.right(); |
340 | 0 | } |
341 | 0 | std::unique_ptr<TBLOB> rotated_blob(new TBLOB(*tblob)); |
342 | 0 | rotated_blob->Normalize(nullptr, ¤t_rotation, nullptr, x_origin, y_origin, scaling, |
343 | 0 | scaling, 0.0f, static_cast<float>(kBlnBaselineOffset), false, nullptr); |
344 | 0 | tess->AdaptiveClassifier(rotated_blob.get(), ratings + i); |
345 | 0 | current_rotation.rotate(rotation90); |
346 | 0 | } |
347 | 0 | delete tblob; |
348 | |
|
349 | 0 | bool stop = o->detect_blob(ratings); |
350 | 0 | s->detect_blob(ratings); |
351 | 0 | int orientation = o->get_orientation(); |
352 | 0 | stop = s->must_stop(orientation) && stop; |
353 | 0 | return stop; |
354 | 0 | } |
355 | | |
356 | 0 | OrientationDetector::OrientationDetector(const std::vector<int> *allowed_scripts, OSResults *osr) { |
357 | 0 | osr_ = osr; |
358 | 0 | allowed_scripts_ = allowed_scripts; |
359 | 0 | } |
360 | | |
361 | | // Score the given blob and return true if it is now sure of the orientation |
362 | | // after adding this block. |
363 | 0 | bool OrientationDetector::detect_blob(BLOB_CHOICE_LIST *scores) { |
364 | 0 | float blob_o_score[4] = {0.0f, 0.0f, 0.0f, 0.0f}; |
365 | 0 | float total_blob_o_score = 0.0f; |
366 | |
|
367 | 0 | for (int i = 0; i < 4; ++i) { |
368 | 0 | BLOB_CHOICE_IT choice_it(scores + i); |
369 | 0 | if (!choice_it.empty()) { |
370 | 0 | BLOB_CHOICE *choice = nullptr; |
371 | 0 | if (allowed_scripts_ != nullptr && !allowed_scripts_->empty()) { |
372 | | // Find the top choice in an allowed script. |
373 | 0 | for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() && choice == nullptr; |
374 | 0 | choice_it.forward()) { |
375 | 0 | int choice_script = choice_it.data()->script_id(); |
376 | 0 | for (auto script : *allowed_scripts_) { |
377 | 0 | if (script == choice_script) { |
378 | 0 | choice = choice_it.data(); |
379 | 0 | break; |
380 | 0 | } |
381 | 0 | } |
382 | 0 | } |
383 | 0 | } else { |
384 | 0 | choice = choice_it.data(); |
385 | 0 | } |
386 | 0 | if (choice != nullptr) { |
387 | | // The certainty score ranges between [-20,0]. This is converted here to |
388 | | // [0,1], with 1 indicating best match. |
389 | 0 | blob_o_score[i] = 1 + 0.05 * choice->certainty(); |
390 | 0 | total_blob_o_score += blob_o_score[i]; |
391 | 0 | } |
392 | 0 | } |
393 | 0 | } |
394 | 0 | if (total_blob_o_score == 0.0) { |
395 | 0 | return false; |
396 | 0 | } |
397 | | // Fill in any blanks with the worst score of the others. This is better than |
398 | | // picking an arbitrary probability for it and way better than -inf. |
399 | 0 | float worst_score = 0.0f; |
400 | 0 | int num_good_scores = 0; |
401 | 0 | for (float f : blob_o_score) { |
402 | 0 | if (f > 0.0f) { |
403 | 0 | ++num_good_scores; |
404 | 0 | if (worst_score == 0.0f || f < worst_score) { |
405 | 0 | worst_score = f; |
406 | 0 | } |
407 | 0 | } |
408 | 0 | } |
409 | 0 | if (num_good_scores == 1) { |
410 | | // Lower worst if there is only one. |
411 | 0 | worst_score /= 2.0f; |
412 | 0 | } |
413 | 0 | for (float &f : blob_o_score) { |
414 | 0 | if (f == 0.0f) { |
415 | 0 | f = worst_score; |
416 | 0 | total_blob_o_score += worst_score; |
417 | 0 | } |
418 | 0 | } |
419 | | // Normalize the orientation scores for the blob and use them to |
420 | | // update the aggregated orientation score. |
421 | 0 | for (int i = 0; total_blob_o_score != 0 && i < 4; ++i) { |
422 | 0 | osr_->orientations[i] += std::log(blob_o_score[i] / total_blob_o_score); |
423 | 0 | } |
424 | | |
425 | | // TODO(ranjith) Add an early exit test, based on min_orientation_margin, |
426 | | // as used in pagesegmain.cpp. |
427 | 0 | return false; |
428 | 0 | } |
429 | | |
430 | 0 | int OrientationDetector::get_orientation() { |
431 | 0 | osr_->update_best_orientation(); |
432 | 0 | return osr_->best_result.orientation_id; |
433 | 0 | } |
434 | | |
435 | | ScriptDetector::ScriptDetector(const std::vector<int> *allowed_scripts, OSResults *osr, |
436 | 0 | tesseract::Tesseract *tess) { |
437 | 0 | osr_ = osr; |
438 | 0 | tess_ = tess; |
439 | 0 | allowed_scripts_ = allowed_scripts; |
440 | | // General scripts |
441 | 0 | katakana_id_ = tess_->unicharset.add_script("Katakana"); |
442 | 0 | hiragana_id_ = tess_->unicharset.add_script("Hiragana"); |
443 | 0 | han_id_ = tess_->unicharset.add_script("Han"); |
444 | 0 | hangul_id_ = tess_->unicharset.add_script("Hangul"); |
445 | 0 | latin_id_ = tess_->unicharset.add_script("Latin"); |
446 | | // Pseudo-scripts |
447 | 0 | fraktur_id_ = tess_->unicharset.add_script("Fraktur"); |
448 | 0 | japanese_id_ = tess_->unicharset.add_script("Japanese"); |
449 | 0 | korean_id_ = tess_->unicharset.add_script("Korean"); |
450 | 0 | } |
451 | | |
452 | | // Score the given blob and return true if it is now sure of the script after |
453 | | // adding this blob. |
454 | 0 | void ScriptDetector::detect_blob(BLOB_CHOICE_LIST *scores) { |
455 | 0 | for (int i = 0; i < 4; ++i) { |
456 | 0 | std::vector<bool> done(kMaxNumberOfScripts); |
457 | |
|
458 | 0 | BLOB_CHOICE_IT choice_it; |
459 | 0 | choice_it.set_to_list(scores + i); |
460 | |
|
461 | 0 | float prev_score = -1; |
462 | 0 | int script_count = 0; |
463 | 0 | int prev_id = -1; |
464 | 0 | int prev_fontinfo_id = -1; |
465 | 0 | const char *prev_unichar = ""; |
466 | 0 | const char *unichar = ""; |
467 | |
|
468 | 0 | for (choice_it.mark_cycle_pt(); !choice_it.cycled_list(); choice_it.forward()) { |
469 | 0 | BLOB_CHOICE *choice = choice_it.data(); |
470 | 0 | int id = choice->script_id(); |
471 | 0 | if (allowed_scripts_ != nullptr && !allowed_scripts_->empty()) { |
472 | | // Check that the choice is in an allowed script. |
473 | 0 | size_t s = 0; |
474 | 0 | for (s = 0; s < allowed_scripts_->size(); ++s) { |
475 | 0 | if ((*allowed_scripts_)[s] == id) { |
476 | 0 | break; |
477 | 0 | } |
478 | 0 | } |
479 | 0 | if (s == allowed_scripts_->size()) { |
480 | 0 | continue; // Not found in list. |
481 | 0 | } |
482 | 0 | } |
483 | | // Script already processed before. |
484 | 0 | if (done.at(id)) { |
485 | 0 | continue; |
486 | 0 | } |
487 | 0 | done[id] = true; |
488 | |
|
489 | 0 | unichar = tess_->unicharset.id_to_unichar(choice->unichar_id()); |
490 | | // Save data from the first match |
491 | 0 | if (prev_score < 0) { |
492 | 0 | prev_score = -choice->certainty(); |
493 | 0 | script_count = 1; |
494 | 0 | prev_id = id; |
495 | 0 | prev_unichar = unichar; |
496 | 0 | prev_fontinfo_id = choice->fontinfo_id(); |
497 | 0 | } else if (-choice->certainty() < prev_score + kNonAmbiguousMargin) { |
498 | 0 | ++script_count; |
499 | 0 | } |
500 | |
|
501 | 0 | if (strlen(prev_unichar) == 1) { |
502 | 0 | if (unichar[0] >= '0' && unichar[0] <= '9') { |
503 | 0 | break; |
504 | 0 | } |
505 | 0 | } |
506 | | |
507 | | // if script_count is >= 2, character is ambiguous, skip other matches |
508 | | // since they are useless. |
509 | 0 | if (script_count >= 2) { |
510 | 0 | break; |
511 | 0 | } |
512 | 0 | } |
513 | | // Character is non ambiguous |
514 | 0 | if (script_count == 1) { |
515 | | // Update the score of the winning script |
516 | 0 | osr_->scripts_na[i][prev_id] += 1.0; |
517 | | |
518 | | // Workaround for Fraktur |
519 | 0 | if (prev_id == latin_id_) { |
520 | 0 | if (prev_fontinfo_id >= 0) { |
521 | 0 | const tesseract::FontInfo &fi = tess_->get_fontinfo_table().at(prev_fontinfo_id); |
522 | | // printf("Font: %s i:%i b:%i f:%i s:%i k:%i (%s)\n", fi.name, |
523 | | // fi.is_italic(), fi.is_bold(), fi.is_fixed_pitch(), |
524 | | // fi.is_serif(), fi.is_fraktur(), |
525 | | // prev_unichar); |
526 | 0 | if (fi.is_fraktur()) { |
527 | 0 | osr_->scripts_na[i][prev_id] -= 1.0; |
528 | 0 | osr_->scripts_na[i][fraktur_id_] += 1.0; |
529 | 0 | } |
530 | 0 | } |
531 | 0 | } |
532 | | |
533 | | // Update Japanese / Korean pseudo-scripts |
534 | 0 | if (prev_id == katakana_id_) { |
535 | 0 | osr_->scripts_na[i][japanese_id_] += 1.0; |
536 | 0 | } |
537 | 0 | if (prev_id == hiragana_id_) { |
538 | 0 | osr_->scripts_na[i][japanese_id_] += 1.0; |
539 | 0 | } |
540 | 0 | if (prev_id == hangul_id_) { |
541 | 0 | osr_->scripts_na[i][korean_id_] += 1.0; |
542 | 0 | } |
543 | 0 | if (prev_id == han_id_) { |
544 | 0 | osr_->scripts_na[i][korean_id_] += kHanRatioInKorean; |
545 | 0 | osr_->scripts_na[i][japanese_id_] += kHanRatioInJapanese; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | } // iterate over each orientation |
549 | 0 | } |
550 | | |
551 | 0 | bool ScriptDetector::must_stop(int orientation) const { |
552 | 0 | osr_->update_best_script(orientation); |
553 | 0 | return osr_->best_result.sconfidence > 1; |
554 | 0 | } |
555 | | |
556 | | // Helper method to convert an orientation index to its value in degrees. |
557 | | // The value represents the amount of clockwise rotation in degrees that must be |
558 | | // applied for the text to be upright (readable). |
559 | 0 | int OrientationIdToValue(const int &id) { |
560 | 0 | switch (id) { |
561 | 0 | case 0: |
562 | 0 | return 0; |
563 | 0 | case 1: |
564 | 0 | return 270; |
565 | 0 | case 2: |
566 | 0 | return 180; |
567 | 0 | case 3: |
568 | 0 | return 90; |
569 | 0 | default: |
570 | 0 | return -1; |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | } // namespace tesseract |