tesseract  5.0.0
tesseract::ScriptDetector Class Reference

#include <osdetect.h>

Public Member Functions

 ScriptDetector (const std::vector< int > *allowed_scripts, OSResults *osr, tesseract::Tesseract *tess)
 
void detect_blob (BLOB_CHOICE_LIST *scores)
 
bool must_stop (int orientation) const
 

Detailed Description

Definition at line 97 of file osdetect.h.

Constructor & Destructor Documentation

◆ ScriptDetector()

tesseract::ScriptDetector::ScriptDetector ( const std::vector< int > *  allowed_scripts,
OSResults osr,
tesseract::Tesseract tess 
)

Definition at line 444 of file osdetect.cpp.

445  {
446  osr_ = osr;
447  tess_ = tess;
448  allowed_scripts_ = allowed_scripts;
449  katakana_id_ = tess_->unicharset.add_script(katakana_script);
450  hiragana_id_ = tess_->unicharset.add_script(hiragana_script);
451  han_id_ = tess_->unicharset.add_script(han_script);
452  hangul_id_ = tess_->unicharset.add_script(hangul_script);
453  japanese_id_ = tess_->unicharset.add_script(japanese_script_);
454  korean_id_ = tess_->unicharset.add_script(korean_script_);
455  latin_id_ = tess_->unicharset.add_script(latin_script);
456  fraktur_id_ = tess_->unicharset.add_script(fraktur_script_);
457 }
UNICHARSET unicharset
Definition: ccutil.h:61
int add_script(const char *script)

Member Function Documentation

◆ detect_blob()

void tesseract::ScriptDetector::detect_blob ( BLOB_CHOICE_LIST *  scores)

Definition at line 461 of file osdetect.cpp.

461  {
462  for (int i = 0; i < 4; ++i) {
463  bool done[kMaxNumberOfScripts] = {false};
464 
465  BLOB_CHOICE_IT choice_it;
466  choice_it.set_to_list(scores + i);
467 
468  float prev_score = -1;
469  int script_count = 0;
470  int prev_id = -1;
471  int prev_fontinfo_id = -1;
472  const char *prev_unichar = "";
473  const char *unichar = "";
474 
475  for (choice_it.mark_cycle_pt(); !choice_it.cycled_list(); choice_it.forward()) {
476  BLOB_CHOICE *choice = choice_it.data();
477  int id = choice->script_id();
478  if (allowed_scripts_ != nullptr && !allowed_scripts_->empty()) {
479  // Check that the choice is in an allowed script.
480  size_t s = 0;
481  for (s = 0; s < allowed_scripts_->size(); ++s) {
482  if ((*allowed_scripts_)[s] == id) {
483  break;
484  }
485  }
486  if (s == allowed_scripts_->size()) {
487  continue; // Not found in list.
488  }
489  }
490  // Script already processed before.
491  if (done[id]) {
492  continue;
493  }
494  done[id] = true;
495 
496  unichar = tess_->unicharset.id_to_unichar(choice->unichar_id());
497  // Save data from the first match
498  if (prev_score < 0) {
499  prev_score = -choice->certainty();
500  script_count = 1;
501  prev_id = id;
502  prev_unichar = unichar;
503  prev_fontinfo_id = choice->fontinfo_id();
504  } else if (-choice->certainty() < prev_score + kNonAmbiguousMargin) {
505  ++script_count;
506  }
507 
508  if (strlen(prev_unichar) == 1) {
509  if (unichar[0] >= '0' && unichar[0] <= '9') {
510  break;
511  }
512  }
513 
514  // if script_count is >= 2, character is ambiguous, skip other matches
515  // since they are useless.
516  if (script_count >= 2) {
517  break;
518  }
519  }
520  // Character is non ambiguous
521  if (script_count == 1) {
522  // Update the score of the winning script
523  osr_->scripts_na[i][prev_id] += 1.0;
524 
525  // Workaround for Fraktur
526  if (prev_id == latin_id_) {
527  if (prev_fontinfo_id >= 0) {
528  const tesseract::FontInfo &fi = tess_->get_fontinfo_table().at(prev_fontinfo_id);
529  // printf("Font: %s i:%i b:%i f:%i s:%i k:%i (%s)\n", fi.name,
530  // fi.is_italic(), fi.is_bold(), fi.is_fixed_pitch(),
531  // fi.is_serif(), fi.is_fraktur(),
532  // prev_unichar);
533  if (fi.is_fraktur()) {
534  osr_->scripts_na[i][prev_id] -= 1.0;
535  osr_->scripts_na[i][fraktur_id_] += 1.0;
536  }
537  }
538  }
539 
540  // Update Japanese / Korean pseudo-scripts
541  if (prev_id == katakana_id_) {
542  osr_->scripts_na[i][japanese_id_] += 1.0;
543  }
544  if (prev_id == hiragana_id_) {
545  osr_->scripts_na[i][japanese_id_] += 1.0;
546  }
547  if (prev_id == hangul_id_) {
548  osr_->scripts_na[i][korean_id_] += 1.0;
549  }
550  if (prev_id == han_id_) {
551  osr_->scripts_na[i][korean_id_] += kHanRatioInKorean;
552  osr_->scripts_na[i][japanese_id_] += kHanRatioInJapanese;
553  }
554  }
555  } // iterate over each orientation
556 }
const float kNonAmbiguousMargin
Definition: osdetect.cpp:49
const int kMaxNumberOfScripts
Definition: osdetect.h:38
const float kHanRatioInJapanese
Definition: osdetect.cpp:47
const float kHanRatioInKorean
Definition: osdetect.cpp:46
float scripts_na[4][kMaxNumberOfScripts]
Definition: osdetect.h:79
bool is_fraktur() const
Definition: fontinfo.h:130
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:279
UnicityTable< FontInfo > & get_fontinfo_table()
Definition: classify.h:324

◆ must_stop()

bool tesseract::ScriptDetector::must_stop ( int  orientation) const

Definition at line 558 of file osdetect.cpp.

558  {
559  osr_->update_best_script(orientation);
560  return osr_->best_result.sconfidence > 1;
561 }
OSBestResult best_result
Definition: osdetect.h:82
void update_best_script(int orientation_id)
Definition: osdetect.cpp:90

The documentation for this class was generated from the following files: