tesseract  5.0.0
ltrresultiterator.h
Go to the documentation of this file.
1 // 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 //
7 // (C) Copyright 2010, 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 //
19 
20 #ifndef TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H_
21 #define TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H_
22 
23 #include "export.h" // for TESS_API
24 #include "pageiterator.h" // for PageIterator
25 #include "publictypes.h" // for PageIteratorLevel
26 #include "unichar.h" // for StrongScriptDirection
27 
28 namespace tesseract {
29 
30 class BLOB_CHOICE_IT;
31 class PAGE_RES;
32 class WERD_RES;
33 
34 class Tesseract;
35 
36 // Class to iterate over tesseract results, providing access to all levels
37 // of the page hierarchy, without including any tesseract headers or having
38 // to handle any tesseract structures.
39 // WARNING! This class points to data held within the TessBaseAPI class, and
40 // therefore can only be used while the TessBaseAPI class still exists and
41 // has not been subjected to a call of Init, SetImage, Recognize, Clear, End
42 // DetectOS, or anything else that changes the internal PAGE_RES.
43 // See tesseract/publictypes.h for the definition of PageIteratorLevel.
44 // See also base class PageIterator, which contains the bulk of the interface.
45 // LTRResultIterator adds text-specific methods for access to OCR output.
46 
48  friend class ChoiceIterator;
49 
50 public:
51  // page_res and tesseract come directly from the BaseAPI.
52  // The rectangle parameters are copied indirectly from the Thresholder,
53  // via the BaseAPI. They represent the coordinates of some rectangle in an
54  // original image (in top-left-origin coordinates) and therefore the top-left
55  // needs to be added to any output boxes in order to specify coordinates
56  // in the original image. See TessBaseAPI::SetRectangle.
57  // The scale and scaled_yres are in case the Thresholder scaled the image
58  // rectangle prior to thresholding. Any coordinates in tesseract's image
59  // must be divided by scale before adding (rect_left, rect_top).
60  // The scaled_yres indicates the effective resolution of the binary image
61  // that tesseract has been given by the Thresholder.
62  // After the constructor, Begin has already been called.
63  LTRResultIterator(PAGE_RES *page_res, Tesseract *tesseract, int scale,
64  int scaled_yres, int rect_left, int rect_top,
65  int rect_width, int rect_height);
66 
67  ~LTRResultIterator() override;
68 
69  // LTRResultIterators may be copied! This makes it possible to iterate over
70  // all the objects at a lower level, while maintaining an iterator to
71  // objects at a higher level. These constructors DO NOT CALL Begin, so
72  // iterations will continue from the location of src.
73  // TODO: For now the copy constructor and operator= only need the base class
74  // versions, but if new data members are added, don't forget to add them!
75 
76  // ============= Moving around within the page ============.
77 
78  // See PageIterator.
79 
80  // ============= Accessing data ==============.
81 
82  // Returns the null terminated UTF-8 encoded text string for the current
83  // object at the given level. Use delete [] to free after use.
84  char *GetUTF8Text(PageIteratorLevel level) const;
85 
86  // Set the string inserted at the end of each text line. "\n" by default.
87  void SetLineSeparator(const char *new_line);
88 
89  // Set the string inserted at the end of each paragraph. "\n" by default.
90  void SetParagraphSeparator(const char *new_para);
91 
92  // Returns the mean confidence of the current object at the given level.
93  // The number should be interpreted as a percent probability. (0.0f-100.0f)
94  float Confidence(PageIteratorLevel level) const;
95 
96  // ============= Functions that refer to words only ============.
97 
98  // Returns the font attributes of the current word. If iterating at a higher
99  // level object than words, eg textlines, then this will return the
100  // attributes of the first word in that textline.
101  // The actual return value is a string representing a font name. It points
102  // to an internal table and SHOULD NOT BE DELETED. Lifespan is the same as
103  // the iterator itself, ie rendered invalid by various members of
104  // TessBaseAPI, including Init, SetImage, End or deleting the TessBaseAPI.
105  // Pointsize is returned in printers points (1/72 inch.)
106  const char *WordFontAttributes(bool *is_bold, bool *is_italic,
107  bool *is_underlined, bool *is_monospace,
108  bool *is_serif, bool *is_smallcaps,
109  int *pointsize, int *font_id) const;
110 
111  // Return the name of the language used to recognize this word.
112  // On error, nullptr. Do not delete this pointer.
113  const char *WordRecognitionLanguage() const;
114 
115  // Return the overall directionality of this word.
116  StrongScriptDirection WordDirection() const;
117 
118  // Returns true if the current word was found in a dictionary.
119  bool WordIsFromDictionary() const;
120 
121  // Returns the number of blanks before the current word.
122  int BlanksBeforeWord() const;
123 
124  // Returns true if the current word is numeric.
125  bool WordIsNumeric() const;
126 
127  // Returns true if the word contains blamer information.
128  bool HasBlamerInfo() const;
129 
130  // Returns the pointer to ParamsTrainingBundle stored in the BlamerBundle
131  // of the current word.
132  const void *GetParamsTrainingBundle() const;
133 
134  // Returns a pointer to the string with blamer information for this word.
135  // Assumes that the word's blamer_bundle is not nullptr.
136  const char *GetBlamerDebug() const;
137 
138  // Returns a pointer to the string with misadaption information for this word.
139  // Assumes that the word's blamer_bundle is not nullptr.
140  const char *GetBlamerMisadaptionDebug() const;
141 
142  // Returns true if a truth string was recorded for the current word.
143  bool HasTruthString() const;
144 
145  // Returns true if the given string is equivalent to the truth string for
146  // the current word.
147  bool EquivalentToTruth(const char *str) const;
148 
149  // Returns a null terminated UTF-8 encoded truth string for the current word.
150  // Use delete [] to free after use.
151  char *WordTruthUTF8Text() const;
152 
153  // Returns a null terminated UTF-8 encoded normalized OCR string for the
154  // current word. Use delete [] to free after use.
155  char *WordNormedUTF8Text() const;
156 
157  // Returns a pointer to serialized choice lattice.
158  // Fills lattice_size with the number of bytes in lattice data.
159  const char *WordLattice(int *lattice_size) const;
160 
161  // ============= Functions that refer to symbols only ============.
162 
163  // Returns true if the current symbol is a superscript.
164  // If iterating at a higher level object than symbols, eg words, then
165  // this will return the attributes of the first symbol in that word.
166  bool SymbolIsSuperscript() const;
167  // Returns true if the current symbol is a subscript.
168  // If iterating at a higher level object than symbols, eg words, then
169  // this will return the attributes of the first symbol in that word.
170  bool SymbolIsSubscript() const;
171  // Returns true if the current symbol is a dropcap.
172  // If iterating at a higher level object than symbols, eg words, then
173  // this will return the attributes of the first symbol in that word.
174  bool SymbolIsDropcap() const;
175 
176 protected:
177  const char *line_separator_;
178  const char *paragraph_separator_;
179 };
180 
181 // Class to iterate over the classifier choices for a single RIL_SYMBOL.
183 public:
184  // Construction is from a LTRResultIterator that points to the symbol of
185  // interest. The ChoiceIterator allows a one-shot iteration over the
186  // choices for this symbol and after that is is useless.
187  explicit ChoiceIterator(const LTRResultIterator &result_it);
188  ~ChoiceIterator();
189 
190  // Moves to the next choice for the symbol and returns false if there
191  // are none left.
192  bool Next();
193 
194  // ============= Accessing data ==============.
195 
196  // Returns the null terminated UTF-8 encoded text string for the current
197  // choice.
198  // NOTE: Unlike LTRResultIterator::GetUTF8Text, the return points to an
199  // internal structure and should NOT be delete[]ed to free after use.
200  const char *GetUTF8Text() const;
201 
202  // Returns the confidence of the current choice depending on the used language
203  // data. If only LSTM traineddata is used the value range is 0.0f - 1.0f. All
204  // choices for one symbol should roughly add up to 1.0f.
205  // If only traineddata of the legacy engine is used, the number should be
206  // interpreted as a percent probability. (0.0f-100.0f) In this case
207  // probabilities won't add up to 100. Each one stands on its own.
208  float Confidence() const;
209 
210  // Returns a vector containing all timesteps, which belong to the currently
211  // selected symbol. A timestep is a vector containing pairs of symbols and
212  // floating point numbers. The number states the probability for the
213  // corresponding symbol.
214  std::vector<std::vector<std::pair<const char *, float>>> *Timesteps() const;
215 
216 private:
217  // clears the remaining spaces out of the results and adapt the probabilities
218  void filterSpaces();
219  // Pointer to the WERD_RES object owned by the API.
220  WERD_RES *word_res_;
221  // Iterator over the blob choices.
222  BLOB_CHOICE_IT *choice_it_;
223  std::vector<std::pair<const char *, float>> *LSTM_choices_ = nullptr;
224  std::vector<std::pair<const char *, float>>::iterator LSTM_choice_it_;
225 
226  const int *tstep_index_;
227  // regulates the rating granularity
228  double rating_coefficient_;
229  // leading blanks
230  int blanks_before_word_;
231  // true when there is lstm engine related trained data
232  bool oemLSTM_;
233 };
234 
235 } // namespace tesseract.
236 
237 #endif // TESSERACT_CCMAIN_LTR_RESULT_ITERATOR_H_
StrongScriptDirection
Definition: unichar.h:43
#define TESS_API
Definition: export.h:34