comparison mupdf-source/thirdparty/tesseract/src/ccmain/osdetect.cpp @ 2:b50eed0cc0ef upstream

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