tesseract  4.0.0-1-g2a2b
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> class GenericVector;
32 
33 namespace tesseract {
34 class Tesseract;
35 }
36 
37 // Max number of scripts in ICU + "NULL" + Japanese and Korean + Fraktur
38 const int kMaxNumberOfScripts = 116 + 1 + 2 + 1;
39 
40 struct OSBestResult {
42  oconfidence(0.0) {}
44  int script_id;
45  float sconfidence;
46  float oconfidence;
47 };
48 
49 struct OSResults {
50  OSResults() : unicharset(nullptr) {
51  for (int i = 0; i < 4; ++i) {
52  for (int j = 0; j < kMaxNumberOfScripts; ++j)
53  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  private:
91  OSResults* osr_;
92  const GenericVector<int>* allowed_scripts_;
93 };
94 
96  public:
97  ScriptDetector(const GenericVector<int>* allowed_scripts,
98  OSResults* osr, tesseract::Tesseract* tess);
99  void detect_blob(BLOB_CHOICE_LIST* scores);
100  bool must_stop(int orientation);
101  private:
102  OSResults* osr_;
103  static const char* korean_script_;
104  static const char* japanese_script_;
105  static const char* fraktur_script_;
106  int korean_id_;
107  int japanese_id_;
108  int katakana_id_;
109  int hiragana_id_;
110  int han_id_;
111  int hangul_id_;
112  int latin_id_;
113  int fraktur_id_;
114  tesseract::Tesseract* tess_;
115  const GenericVector<int>* allowed_scripts_;
116 };
117 
119  OSResults*,
121 
122 int os_detect(TO_BLOCK_LIST* port_blocks,
123  OSResults* osr,
124  tesseract::Tesseract* tess);
125 
126 int os_detect_blobs(const GenericVector<int>* allowed_scripts,
127  BLOBNBOX_CLIST* blob_list,
128  OSResults* osr,
129  tesseract::Tesseract* tess);
130 
133  tesseract::Tesseract* tess);
134 
135 // Helper method to convert an orientation index to its value in degrees.
136 // The value represents the amount of clockwise rotation in degrees that must be
137 // applied for the text to be upright (readable).
138 TESS_API int OrientationIdToValue(const int& id);
139 
140 #endif // TESSERACT_CCMAIN_OSDETECT_H_
OrientationDetector(const GenericVector< int > *allowed_scripts, OSResults *results)
Definition: osdetect.cpp:374
UNICHARSET * unicharset
Definition: osdetect.h:80
bool must_stop(int orientation)
Definition: osdetect.cpp:560
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *, tesseract::Tesseract *tess)
Definition: osdetect.cpp:329
void update_best_orientation()
Definition: osdetect.cpp:62
#define TESS_API
Definition: platform.h:74
void set_best_orientation(int orientation_id)
Definition: osdetect.cpp:84
OSBestResult()
Definition: osdetect.h:41
OSResults()
Definition: osdetect.h:50
int script_id
Definition: osdetect.h:44
float scripts_na[4][kMaxNumberOfScripts]
Definition: osdetect.h:78
void print_scores(void) const
Definition: osdetect.cpp:126
float orientations[4]
Definition: osdetect.h:76
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:278
float sconfidence
Definition: osdetect.h:45
OSBestResult best_result
Definition: osdetect.h:81
int orientation_id
Definition: osdetect.h:43
ScriptDetector(const GenericVector< int > *allowed_scripts, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:453
void update_best_script(int orientation_id)
Definition: osdetect.cpp:89
const int kMaxNumberOfScripts
Definition: osdetect.h:38
void detect_blob(BLOB_CHOICE_LIST *scores)
Definition: osdetect.cpp:471
void accumulate(const OSResults &osr)
Definition: osdetect.cpp:144
Definition: strngs.h:45
int orientation_and_script_detection(STRING &filename, OSResults *, tesseract::Tesseract *)
Definition: osdetect.cpp:190
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:233
bool detect_blob(BLOB_CHOICE_LIST *scores)
Definition: osdetect.cpp:382
float oconfidence
Definition: osdetect.h:46
TESS_API int get_best_script(int orientation_id) const
Definition: osdetect.cpp:112
TESS_API int OrientationIdToValue(const int &id)
Definition: osdetect.cpp:568