tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
osdetect.h
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 {
41  OSBestResult() : orientation_id(0), script_id(0), sconfidence(0.0),
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  }
57  void update_best_orientation();
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.
78  float scripts_na[4][kMaxNumberOfScripts];
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:
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:
103  static const char* korean_script_;
104  static const char* japanese_script_;
105  static const char* fraktur_script_;
110  int han_id_;
116 };
117 
118 int orientation_and_script_detection(STRING& filename,
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 
131 bool os_detect_blob(BLOBNBOX* bbox, OrientationDetector* o,
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_
OSResults * osr_
Definition: osdetect.h:102
OSBestResult best_result
Definition: osdetect.h:81
static const char * korean_script_
Definition: osdetect.h:103
int script_id
Definition: osdetect.h:44
tesseract::Tesseract * tess_
Definition: osdetect.h:114
Definition: unicharset.h:146
Definition: osdetect.h:49
int han_id_
Definition: osdetect.h:110
Definition: baseapi.cpp:94
static const char * fraktur_script_
Definition: osdetect.h:105
UNICHARSET * unicharset
Definition: osdetect.h:80
int japanese_id_
Definition: osdetect.h:107
int fraktur_id_
Definition: osdetect.h:113
OSResults * osr_
Definition: osdetect.h:91
Definition: blobbox.h:144
Definition: osdetect.h:95
Definition: baseapi.h:37
OSBestResult()
Definition: osdetect.h:41
const GenericVector< int > * allowed_scripts_
Definition: osdetect.h:92
Definition: osdetect.h:84
const GenericVector< int > * allowed_scripts_
Definition: osdetect.h:115
int katakana_id_
Definition: osdetect.h:108
Definition: strngs.h:45
int hangul_id_
Definition: osdetect.h:111
OSResults()
Definition: osdetect.h:50
Definition: osdetect.h:40
Definition: tesseractclass.h:173
int hiragana_id_
Definition: osdetect.h:109
float sconfidence
Definition: osdetect.h:45
int korean_id_
Definition: osdetect.h:106
float oconfidence
Definition: osdetect.h:46
int orientation_id
Definition: osdetect.h:43
int latin_id_
Definition: osdetect.h:112
static const char * japanese_script_
Definition: osdetect.h:104