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