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