osdetect.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: osdetect.h
  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. #ifndef TESSERACT_CCMAIN_OSDETECT_H_
  20. #define TESSERACT_CCMAIN_OSDETECT_H_
  21. #include "platform.h" // for TESS_API
  22. class BLOBNBOX;
  23. class BLOBNBOX_CLIST;
  24. class BLOB_CHOICE_LIST;
  25. class STRING;
  26. class TO_BLOCK_LIST;
  27. class UNICHARSET;
  28. template <typename T> class GenericVector;
  29. namespace tesseract {
  30. class Tesseract;
  31. }
  32. // Max number of scripts in ICU + "NULL" + Japanese and Korean + Fraktur
  33. const int kMaxNumberOfScripts = 116 + 1 + 2 + 1;
  34. struct OSBestResult {
  35. OSBestResult() : orientation_id(0), script_id(0), sconfidence(0.0),
  36. oconfidence(0.0) {}
  37. int orientation_id;
  38. int script_id;
  39. float sconfidence;
  40. float oconfidence;
  41. };
  42. struct OSResults {
  43. OSResults() : unicharset(nullptr) {
  44. for (int i = 0; i < 4; ++i) {
  45. for (int j = 0; j < kMaxNumberOfScripts; ++j)
  46. scripts_na[i][j] = 0;
  47. orientations[i] = 0;
  48. }
  49. }
  50. void update_best_orientation();
  51. // Set the estimate of the orientation to the given id.
  52. void set_best_orientation(int orientation_id);
  53. // Update/Compute the best estimate of the script assuming the given
  54. // orientation id.
  55. void update_best_script(int orientation_id);
  56. // Return the index of the script with the highest score for this orientation.
  57. TESS_API int get_best_script(int orientation_id) const;
  58. // Accumulate scores with given OSResults instance and update the best script.
  59. void accumulate(const OSResults& osr);
  60. // Print statistics.
  61. void print_scores(void) const;
  62. void print_scores(int orientation_id) const;
  63. // Array holding scores for each orientation id [0,3].
  64. // Orientation ids [0..3] map to [0, 270, 180, 90] degree orientations of the
  65. // page respectively, where the values refer to the amount of clockwise
  66. // rotation to be applied to the page for the text to be upright and readable.
  67. float orientations[4];
  68. // Script confidence scores for each of 4 possible orientations.
  69. float scripts_na[4][kMaxNumberOfScripts];
  70. UNICHARSET* unicharset;
  71. OSBestResult best_result;
  72. };
  73. class OrientationDetector {
  74. public:
  75. OrientationDetector(const GenericVector<int>* allowed_scripts,
  76. OSResults* results);
  77. bool detect_blob(BLOB_CHOICE_LIST* scores);
  78. int get_orientation();
  79. private:
  80. OSResults* osr_;
  81. const GenericVector<int>* allowed_scripts_;
  82. };
  83. class ScriptDetector {
  84. public:
  85. ScriptDetector(const GenericVector<int>* allowed_scripts,
  86. OSResults* osr, tesseract::Tesseract* tess);
  87. void detect_blob(BLOB_CHOICE_LIST* scores);
  88. bool must_stop(int orientation);
  89. private:
  90. OSResults* osr_;
  91. static const char* korean_script_;
  92. static const char* japanese_script_;
  93. static const char* fraktur_script_;
  94. int korean_id_;
  95. int japanese_id_;
  96. int katakana_id_;
  97. int hiragana_id_;
  98. int han_id_;
  99. int hangul_id_;
  100. int latin_id_;
  101. int fraktur_id_;
  102. tesseract::Tesseract* tess_;
  103. const GenericVector<int>* allowed_scripts_;
  104. };
  105. int orientation_and_script_detection(STRING& filename,
  106. OSResults*,
  107. tesseract::Tesseract*);
  108. int os_detect(TO_BLOCK_LIST* port_blocks,
  109. OSResults* osr,
  110. tesseract::Tesseract* tess);
  111. int os_detect_blobs(const GenericVector<int>* allowed_scripts,
  112. BLOBNBOX_CLIST* blob_list,
  113. OSResults* osr,
  114. tesseract::Tesseract* tess);
  115. bool os_detect_blob(BLOBNBOX* bbox, OrientationDetector* o,
  116. ScriptDetector* s, OSResults*,
  117. tesseract::Tesseract* tess);
  118. // Helper method to convert an orientation index to its value in degrees.
  119. // The value represents the amount of clockwise rotation in degrees that must be
  120. // applied for the text to be upright (readable).
  121. TESS_API int OrientationIdToValue(const int& id);
  122. #endif // TESSERACT_CCMAIN_OSDETECT_H_