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