ltrresultiterator.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ///////////////////////////////////////////////////////////////////////
  2. // File: ltrresultiterator.h
  3. // Description: Iterator for tesseract results in strict left-to-right
  4. // order that avoids using tesseract internal data structures.
  5. // Author: Ray Smith
  6. // Created: Fri Feb 26 11:01:06 PST 2010
  7. //
  8. // (C) Copyright 2010, Google Inc.
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. //
  19. ///////////////////////////////////////////////////////////////////////
  20. #ifndef TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H_
  21. #define TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H_
  22. #include "pageiterator.h" // for PageIterator
  23. #include "platform.h" // for TESS_API
  24. #include "publictypes.h" // for PageIteratorLevel
  25. #include "unichar.h" // for StrongScriptDirection
  26. class BLOB_CHOICE_IT;
  27. class PAGE_RES;
  28. class WERD_RES;
  29. namespace tesseract {
  30. class Tesseract;
  31. // Class to iterate over tesseract results, providing access to all levels
  32. // of the page hierarchy, without including any tesseract headers or having
  33. // to handle any tesseract structures.
  34. // WARNING! This class points to data held within the TessBaseAPI class, and
  35. // therefore can only be used while the TessBaseAPI class still exists and
  36. // has not been subjected to a call of Init, SetImage, Recognize, Clear, End
  37. // DetectOS, or anything else that changes the internal PAGE_RES.
  38. // See apitypes.h for the definition of PageIteratorLevel.
  39. // See also base class PageIterator, which contains the bulk of the interface.
  40. // LTRResultIterator adds text-specific methods for access to OCR output.
  41. class TESS_API LTRResultIterator : public PageIterator {
  42. friend class ChoiceIterator;
  43. public:
  44. // page_res and tesseract come directly from the BaseAPI.
  45. // The rectangle parameters are copied indirectly from the Thresholder,
  46. // via the BaseAPI. They represent the coordinates of some rectangle in an
  47. // original image (in top-left-origin coordinates) and therefore the top-left
  48. // needs to be added to any output boxes in order to specify coordinates
  49. // in the original image. See TessBaseAPI::SetRectangle.
  50. // The scale and scaled_yres are in case the Thresholder scaled the image
  51. // rectangle prior to thresholding. Any coordinates in tesseract's image
  52. // must be divided by scale before adding (rect_left, rect_top).
  53. // The scaled_yres indicates the effective resolution of the binary image
  54. // that tesseract has been given by the Thresholder.
  55. // After the constructor, Begin has already been called.
  56. LTRResultIterator(PAGE_RES* page_res, Tesseract* tesseract,
  57. int scale, int scaled_yres,
  58. int rect_left, int rect_top,
  59. int rect_width, int rect_height);
  60. ~LTRResultIterator() override;
  61. // LTRResultIterators may be copied! This makes it possible to iterate over
  62. // all the objects at a lower level, while maintaining an iterator to
  63. // objects at a higher level. These constructors DO NOT CALL Begin, so
  64. // iterations will continue from the location of src.
  65. // TODO: For now the copy constructor and operator= only need the base class
  66. // versions, but if new data members are added, don't forget to add them!
  67. // ============= Moving around within the page ============.
  68. // See PageIterator.
  69. // ============= Accessing data ==============.
  70. // Returns the null terminated UTF-8 encoded text string for the current
  71. // object at the given level. Use delete [] to free after use.
  72. char* GetUTF8Text(PageIteratorLevel level) const;
  73. // Set the string inserted at the end of each text line. "\n" by default.
  74. void SetLineSeparator(const char *new_line);
  75. // Set the string inserted at the end of each paragraph. "\n" by default.
  76. void SetParagraphSeparator(const char *new_para);
  77. // Returns the mean confidence of the current object at the given level.
  78. // The number should be interpreted as a percent probability. (0.0f-100.0f)
  79. float Confidence(PageIteratorLevel level) const;
  80. // Returns the attributes of the current row.
  81. void RowAttributes(float* row_height, float* descenders,
  82. float* ascenders) const;
  83. // ============= Functions that refer to words only ============.
  84. // Returns the font attributes of the current word. If iterating at a higher
  85. // level object than words, eg textlines, then this will return the
  86. // attributes of the first word in that textline.
  87. // The actual return value is a string representing a font name. It points
  88. // to an internal table and SHOULD NOT BE DELETED. Lifespan is the same as
  89. // the iterator itself, ie rendered invalid by various members of
  90. // TessBaseAPI, including Init, SetImage, End or deleting the TessBaseAPI.
  91. // Pointsize is returned in printers points (1/72 inch.)
  92. const char* WordFontAttributes(bool* is_bold,
  93. bool* is_italic,
  94. bool* is_underlined,
  95. bool* is_monospace,
  96. bool* is_serif,
  97. bool* is_smallcaps,
  98. int* pointsize,
  99. int* font_id) const;
  100. // Return the name of the language used to recognize this word.
  101. // On error, nullptr. Do not delete this pointer.
  102. const char* WordRecognitionLanguage() const;
  103. // Return the overall directionality of this word.
  104. StrongScriptDirection WordDirection() const;
  105. // Returns true if the current word was found in a dictionary.
  106. bool WordIsFromDictionary() const;
  107. // Returns the number of blanks before the current word.
  108. int BlanksBeforeWord() const;
  109. // Returns true if the current word is numeric.
  110. bool WordIsNumeric() const;
  111. // Returns true if the word contains blamer information.
  112. bool HasBlamerInfo() const;
  113. // Returns the pointer to ParamsTrainingBundle stored in the BlamerBundle
  114. // of the current word.
  115. const void *GetParamsTrainingBundle() const;
  116. // Returns a pointer to the string with blamer information for this word.
  117. // Assumes that the word's blamer_bundle is not nullptr.
  118. const char *GetBlamerDebug() const;
  119. // Returns a pointer to the string with misadaption information for this word.
  120. // Assumes that the word's blamer_bundle is not nullptr.
  121. const char *GetBlamerMisadaptionDebug() const;
  122. // Returns true if a truth string was recorded for the current word.
  123. bool HasTruthString() const;
  124. // Returns true if the given string is equivalent to the truth string for
  125. // the current word.
  126. bool EquivalentToTruth(const char *str) const;
  127. // Returns a null terminated UTF-8 encoded truth string for the current word.
  128. // Use delete [] to free after use.
  129. char* WordTruthUTF8Text() const;
  130. // Returns a null terminated UTF-8 encoded normalized OCR string for the
  131. // current word. Use delete [] to free after use.
  132. char* WordNormedUTF8Text() const;
  133. // Returns a pointer to serialized choice lattice.
  134. // Fills lattice_size with the number of bytes in lattice data.
  135. const char *WordLattice(int *lattice_size) const;
  136. // ============= Functions that refer to symbols only ============.
  137. // Returns true if the current symbol is a superscript.
  138. // If iterating at a higher level object than symbols, eg words, then
  139. // this will return the attributes of the first symbol in that word.
  140. bool SymbolIsSuperscript() const;
  141. // Returns true if the current symbol is a subscript.
  142. // If iterating at a higher level object than symbols, eg words, then
  143. // this will return the attributes of the first symbol in that word.
  144. bool SymbolIsSubscript() const;
  145. // Returns true if the current symbol is a dropcap.
  146. // If iterating at a higher level object than symbols, eg words, then
  147. // this will return the attributes of the first symbol in that word.
  148. bool SymbolIsDropcap() const;
  149. protected:
  150. const char *line_separator_;
  151. const char *paragraph_separator_;
  152. };
  153. // Class to iterate over the classifier choices for a single RIL_SYMBOL.
  154. class ChoiceIterator {
  155. public:
  156. // Construction is from a LTRResultIterator that points to the symbol of
  157. // interest. The ChoiceIterator allows a one-shot iteration over the
  158. // choices for this symbol and after that is is useless.
  159. explicit ChoiceIterator(const LTRResultIterator& result_it);
  160. ~ChoiceIterator();
  161. // Moves to the next choice for the symbol and returns false if there
  162. // are none left.
  163. bool Next();
  164. // ============= Accessing data ==============.
  165. // Returns the null terminated UTF-8 encoded text string for the current
  166. // choice.
  167. // NOTE: Unlike LTRResultIterator::GetUTF8Text, the return points to an
  168. // internal structure and should NOT be delete[]ed to free after use.
  169. const char* GetUTF8Text() const;
  170. // Returns the confidence of the current choice depending on the used language
  171. // data. If only LSTM traineddata is used the value range is 0.0f - 1.0f. All
  172. // choices for one symbol should roughly add up to 1.0f.
  173. // If only traineddata of the legacy engine is used, the number should be
  174. // interpreted as a percent probability. (0.0f-100.0f) In this case
  175. // probabilities won't add up to 100. Each one stands on its own.
  176. float Confidence() const;
  177. private:
  178. // Pointer to the WERD_RES object owned by the API.
  179. WERD_RES* word_res_;
  180. // Iterator over the blob choices.
  181. BLOB_CHOICE_IT* choice_it_;
  182. };
  183. } // namespace tesseract.
  184. #endif // TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H_