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