Coverage Report

Created: 2024-02-28 06:46

/src/tesseract/include/tesseract/osdetect.h
Line
Count
Source
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
25
namespace tesseract {
26
27
class BLOBNBOX;
28
class BLOBNBOX_CLIST;
29
class BLOB_CHOICE_LIST;
30
class TO_BLOCK_LIST;
31
class UNICHARSET;
32
33
class Tesseract;
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 {
39
  OSBestResult()
40
16.6k
      : orientation_id(0), script_id(0), sconfidence(0.0), oconfidence(0.0) {}
41
  int orientation_id;
42
  int script_id;
43
  float sconfidence;
44
  float oconfidence;
45
};
46
47
struct OSResults {
48
16.6k
  OSResults() : unicharset(nullptr) {
49
83.4k
    for (int i = 0; i < 4; ++i) {
50
8.07M
      for (int j = 0; j < kMaxNumberOfScripts; ++j) {
51
8.01M
        scripts_na[i][j] = 0;
52
8.01M
      }
53
66.7k
      orientations[i] = 0;
54
66.7k
    }
55
16.6k
  }
56
  void update_best_orientation();
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.
77
  float scripts_na[4][kMaxNumberOfScripts];
78
79
  UNICHARSET *unicharset;
80
  OSBestResult best_result;
81
};
82
83
class OrientationDetector {
84
public:
85
  OrientationDetector(const std::vector<int> *allowed_scripts,
86
                      OSResults *results);
87
  bool detect_blob(BLOB_CHOICE_LIST *scores);
88
  int get_orientation();
89
90
private:
91
  OSResults *osr_;
92
  const std::vector<int> *allowed_scripts_;
93
};
94
95
class ScriptDetector {
96
public:
97
  ScriptDetector(const std::vector<int> *allowed_scripts, OSResults *osr,
98
                 tesseract::Tesseract *tess);
99
  void detect_blob(BLOB_CHOICE_LIST *scores);
100
  bool must_stop(int orientation) const;
101
102
private:
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_;
115
  tesseract::Tesseract *tess_;
116
  const std::vector<int> *allowed_scripts_;
117
};
118
119
int orientation_and_script_detection(const char *filename, OSResults *,
120
                                     tesseract::Tesseract *);
121
122
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr,
123
              tesseract::Tesseract *tess);
124
125
int os_detect_blobs(const std::vector<int> *allowed_scripts,
126
                    BLOBNBOX_CLIST *blob_list, OSResults *osr,
127
                    tesseract::Tesseract *tess);
128
129
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s,
130
                    OSResults *, tesseract::Tesseract *tess);
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).
135
TESS_API int OrientationIdToValue(const int &id);
136
137
} // namespace tesseract
138
139
#endif // TESSERACT_CCMAIN_OSDETECT_H_