All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::TesseractCubeCombiner Class Reference

#include <tesseract_cube_combiner.h>

Public Member Functions

 TesseractCubeCombiner (CubeRecoContext *cube_cntxt)
 
virtual ~TesseractCubeCombiner ()
 
float CombineResults (WERD_RES *tess_res, CubeObject *cube_obj)
 
float CombineResults (WERD_RES *tess_res, CubeObject *cube_obj, WordAltList *alt_list)
 
bool ComputeCombinerFeatures (const string &tess_res, int tess_confidence, CubeObject *cube_obj, WordAltList *cube_alt_list, vector< double > *features, bool *agreement)
 
bool ValidWord (const string &str)
 
bool LoadCombinerNet ()
 

Detailed Description

Definition at line 47 of file tesseract_cube_combiner.h.

Constructor & Destructor Documentation

tesseract::TesseractCubeCombiner::TesseractCubeCombiner ( CubeRecoContext cube_cntxt)
explicit

Definition at line 39 of file tesseract_cube_combiner.cpp.

39  {
40  cube_cntxt_ = cube_cntxt;
41  combiner_net_ = NULL;
42 }
#define NULL
Definition: host.h:144
tesseract::TesseractCubeCombiner::~TesseractCubeCombiner ( )
virtual

Definition at line 44 of file tesseract_cube_combiner.cpp.

44  {
45  if (combiner_net_ != NULL) {
46  delete combiner_net_;
47  combiner_net_ = NULL;
48  }
49 }
#define NULL
Definition: host.h:144

Member Function Documentation

float tesseract::TesseractCubeCombiner::CombineResults ( WERD_RES tess_res,
CubeObject cube_obj 
)

Definition at line 242 of file tesseract_cube_combiner.cpp.

243  {
244  // If no combiner is loaded or the cube object is undefined,
245  // tesseract wins with probability 1.0
246  if (combiner_net_ == NULL || cube_obj == NULL) {
247  tprintf("Cube WARNING (TesseractCubeCombiner::CombineResults): "
248  "Cube objects not initialized; defaulting to Tesseract\n");
249  return 1.0;
250  }
251 
252  // Retrieve the alternate list from the CubeObject's current state.
253  // If the alt list empty, tesseract wins with probability 1.0
254  WordAltList *cube_alt_list = cube_obj->AlternateList();
255  if (cube_alt_list == NULL)
256  cube_alt_list = cube_obj->RecognizeWord();
257  if (cube_alt_list == NULL || cube_alt_list->AltCount() <= 0) {
258  tprintf("Cube WARNING (TesseractCubeCombiner::CombineResults): "
259  "Cube returned no results; defaulting to Tesseract\n");
260  return 1.0;
261  }
262  return CombineResults(tess_res, cube_obj, cube_alt_list);
263 }
float CombineResults(WERD_RES *tess_res, CubeObject *cube_obj)
#define tprintf(...)
Definition: tprintf.h:31
#define NULL
Definition: host.h:144
float tesseract::TesseractCubeCombiner::CombineResults ( WERD_RES tess_res,
CubeObject cube_obj,
WordAltList alt_list 
)

Definition at line 270 of file tesseract_cube_combiner.cpp.

272  {
273  // If no combiner is loaded or the cube object is undefined, or the
274  // alt list is empty, tesseract wins with probability 1.0
275  if (combiner_net_ == NULL || cube_obj == NULL ||
276  cube_alt_list == NULL || cube_alt_list->AltCount() <= 0) {
277  tprintf("Cube WARNING (TesseractCubeCombiner::CombineResults): "
278  "Cube result cannot be retrieved; defaulting to Tesseract\n");
279  return 1.0;
280  }
281 
282  // Tesseract result string, tesseract confidence, and cost of
283  // tesseract result according to cube
284  string tess_str = tess_res->best_choice->unichar_string().string();
285  // Map certainty [-20.0, 0.0] to confidence [0, 100]
286  int tess_confidence = MIN(100, MAX(1, static_cast<int>(
287  100 + (5 * tess_res->best_choice->certainty()))));
288 
289  // Compute the combiner features. If feature computation fails or
290  // answers are identical, tesseract wins with probability 1.0
291  vector<double> features;
292  bool agreement;
293  bool combiner_success = ComputeCombinerFeatures(tess_str, tess_confidence,
294  cube_obj, cube_alt_list,
295  &features, &agreement);
296  if (!combiner_success || agreement)
297  return 1.0;
298 
299  // Classify combiner feature vector and return output (probability
300  // of tesseract class).
301  double net_out[2];
302  if (!combiner_net_->FeedForward(&features[0], net_out))
303  return 1.0;
304  return net_out[1];
305 }
#define MAX(x, y)
Definition: ndminx.h:24
WERD_CHOICE * best_choice
Definition: pageres.h:219
#define tprintf(...)
Definition: tprintf.h:31
#define MIN(x, y)
Definition: ndminx.h:28
const STRING & unichar_string() const
Definition: ratngs.h:524
bool FeedForward(const Type *inputs, Type *outputs)
Definition: neural_net.cpp:79
float certainty() const
Definition: ratngs.h:327
bool ComputeCombinerFeatures(const string &tess_res, int tess_confidence, CubeObject *cube_obj, WordAltList *cube_alt_list, vector< double > *features, bool *agreement)
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:193
bool tesseract::TesseractCubeCombiner::ComputeCombinerFeatures ( const string &  tess_res,
int  tess_confidence,
CubeObject cube_obj,
WordAltList cube_alt_list,
vector< double > *  features,
bool *  agreement 
)

Definition at line 130 of file tesseract_cube_combiner.cpp.

135  {
136  features->clear();
137  *agreement = false;
138  if (cube_alt_list == NULL || cube_alt_list->AltCount() <= 0)
139  return false;
140 
141  // Get Cube's best string; return false if empty
142  char_32 *cube_best_str32 = cube_alt_list->Alt(0);
143  if (cube_best_str32 == NULL || CubeUtils::StrLen(cube_best_str32) < 1)
144  return false;
145  string cube_best_str;
146  int cube_best_cost = cube_alt_list->AltCost(0);
147  int cube_best_bigram_cost = 0;
148  bool cube_best_bigram_cost_valid = true;
149  if (cube_cntxt_->Bigrams())
150  cube_best_bigram_cost = cube_cntxt_->Bigrams()->
151  Cost(cube_best_str32, cube_cntxt_->CharacterSet());
152  else
153  cube_best_bigram_cost_valid = false;
154  CubeUtils::UTF32ToUTF8(cube_best_str32, &cube_best_str);
155 
156  // Get Tesseract's UTF32 string
157  string_32 tess_str32;
158  CubeUtils::UTF8ToUTF32(tess_str.c_str(), &tess_str32);
159 
160  // Compute agreement flag
161  *agreement = (tess_str.compare(cube_best_str) == 0);
162 
163  // Get Cube's second best string; if empty, return false
164  char_32 *cube_next_best_str32;
165  string cube_next_best_str;
166  int cube_next_best_cost = WORST_COST;
167  if (cube_alt_list->AltCount() > 1) {
168  cube_next_best_str32 = cube_alt_list->Alt(1);
169  if (cube_next_best_str32 == NULL ||
170  CubeUtils::StrLen(cube_next_best_str32) == 0) {
171  return false;
172  }
173  cube_next_best_cost = cube_alt_list->AltCost(1);
174  CubeUtils::UTF32ToUTF8(cube_next_best_str32, &cube_next_best_str);
175  }
176  // Rank of Tesseract's top result in Cube's alternate list
177  int tess_rank = 0;
178  for (tess_rank = 0; tess_rank < cube_alt_list->AltCount(); tess_rank++) {
179  string alt_str;
180  CubeUtils::UTF32ToUTF8(cube_alt_list->Alt(tess_rank), &alt_str);
181  if (alt_str == tess_str)
182  break;
183  }
184 
185  // Cube's cost for tesseract's result. Note that this modifies the
186  // state of cube_obj, including its alternate list by calling RecognizeWord()
187  int tess_cost = cube_obj->WordCost(tess_str.c_str());
188  // Cube's bigram cost of Tesseract's string
189  int tess_bigram_cost = 0;
190  int tess_bigram_cost_valid = true;
191  if (cube_cntxt_->Bigrams())
192  tess_bigram_cost = cube_cntxt_->Bigrams()->
193  Cost(tess_str32.c_str(), cube_cntxt_->CharacterSet());
194  else
195  tess_bigram_cost_valid = false;
196 
197  // Tesseract confidence
198  features->push_back(tess_confidence);
199  // Cube cost of Tesseract string
200  features->push_back(tess_cost);
201  // Cube Rank of Tesseract string
202  features->push_back(tess_rank);
203  // length of Tesseract OCR string
204  features->push_back(tess_str.length());
205  // Tesseract OCR string in dictionary
206  features->push_back(ValidWord(tess_str));
207  if (tess_bigram_cost_valid) {
208  // bigram cost of Tesseract string
209  features->push_back(tess_bigram_cost);
210  }
211  // Cube tess_cost of Cube best string
212  features->push_back(cube_best_cost);
213  // Cube tess_cost of Cube next best string
214  features->push_back(cube_next_best_cost);
215  // length of Cube string
216  features->push_back(cube_best_str.length());
217  // Cube string in dictionary
218  features->push_back(ValidWord(cube_best_str));
219  if (cube_best_bigram_cost_valid) {
220  // bigram cost of Cube string
221  features->push_back(cube_best_bigram_cost);
222  }
223  // case-insensitive string comparison, including punctuation
224  int compare_nocase_punc = CompareStrings(cube_best_str,
225  tess_str, false, true);
226  features->push_back(compare_nocase_punc == 0);
227  // case-sensitive string comparison, ignoring punctuation
228  int compare_case_nopunc = CompareStrings(cube_best_str,
229  tess_str, true, false);
230  features->push_back(compare_case_nopunc == 0);
231  // case-insensitive string comparison, ignoring punctuation
232  int compare_nocase_nopunc = CompareStrings(cube_best_str,
233  tess_str, true, true);
234  features->push_back(compare_nocase_nopunc == 0);
235  return true;
236 }
#define WORST_COST
Definition: cube_const.h:30
basic_string< char_32 > string_32
Definition: string_32.h:41
static void UTF8ToUTF32(const char *utf8_str, string_32 *str32)
Definition: cube_utils.cpp:266
CharBigrams * Bigrams() const
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
static void UTF32ToUTF8(const char_32 *utf32_str, string *str)
Definition: cube_utils.cpp:282
CharSet * CharacterSet() const
signed int char_32
Definition: string_32.h:40
#define NULL
Definition: host.h:144
bool tesseract::TesseractCubeCombiner::LoadCombinerNet ( )

Definition at line 51 of file tesseract_cube_combiner.cpp.

51  {
52  ASSERT_HOST(cube_cntxt_);
53  // Compute the path of the combiner net
54  string data_path;
55  cube_cntxt_->GetDataFilePath(&data_path);
56  string net_file_name = data_path + cube_cntxt_->Lang() +
57  ".tesseract_cube.nn";
58 
59  // Return false if file does not exist
60  FILE *fp = fopen(net_file_name.c_str(), "rb");
61  if (fp == NULL)
62  return false;
63  else
64  fclose(fp);
65 
66  // Load and validate net
67  combiner_net_ = NeuralNet::FromFile(net_file_name);
68  if (combiner_net_ == NULL) {
69  tprintf("Could not read combiner net file %s", net_file_name.c_str());
70  return false;
71  } else if (combiner_net_->out_cnt() != 2) {
72  tprintf("Invalid combiner net file %s! Output count != 2\n",
73  net_file_name.c_str());
74  delete combiner_net_;
75  combiner_net_ = NULL;
76  return false;
77  }
78  return true;
79 }
#define tprintf(...)
Definition: tprintf.h:31
#define ASSERT_HOST(x)
Definition: errcode.h:84
int out_cnt() const
Definition: neural_net.h:41
#define NULL
Definition: host.h:144
bool GetDataFilePath(string *path) const
const string & Lang() const
static NeuralNet * FromFile(const string file_name)
Definition: neural_net.cpp:204
bool tesseract::TesseractCubeCombiner::ValidWord ( const string &  str)

Definition at line 122 of file tesseract_cube_combiner.cpp.

122  {
123  return (cube_cntxt_->TesseractObject()->getDict().valid_word(str.c_str())
124  > 0);
125 }
tesseract::Tesseract * TesseractObject() const
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
Definition: dict.cpp:705
Dict & getDict()
Definition: classify.h:65

The documentation for this class was generated from the following files: