15 #include "allheaders.h"
30 static float convert_prob_to_tess_certainty(
float prob) {
31 return (prob - 1.0) * 20.0;
50 boxGetGeometry(char_box, &left, &top, &width, &height);
51 left += word_box.
left() - x_offset;
54 bottom = top - height;
55 return TBOX(left, bottom, right, top);
71 tprintf(
"Cube WARNING (extract_cube_state): Invalid cube object "
72 "passed to extract_cube_state\n");
81 if (!cube_search_obj) {
83 tprintf(
"Cube WARNING (Extract_cube_state): Could not retrieve "
84 "cube's search object in extract_cube_state.\n");
89 if (!beam_search_obj) {
91 tprintf(
"Cube WARNING (Extract_cube_state): Could not retrieve "
92 "cube's beam search object in extract_cube_state.\n");
100 *char_samples = beam_search_obj->
BackTrack(
101 cube_search_obj, best_node_index, num_chars,
NULL, char_boxes);
122 tprintf(
"Cube WARNING (create_cube_box_word): Invalid box_word.\n");
130 for (
int i = 0; i < num_chars; ++i) {
131 Box* char_box = boxaGetBox(char_boxes, i, L_CLONE);
132 if (x_offset < 0 || char_box->x < x_offset) {
133 x_offset = char_box->x;
135 boxDestroy(&char_box);
138 for (
int i = 0; i < num_chars; ++i) {
139 Box* char_box = boxaGetBox(char_boxes, i, L_CLONE);
141 boxDestroy(&char_box);
165 for (
int i = 0; i < num_chars; ++i) {
166 uch_id = cube_char_set->
UnicharID(char_samples[i]->StrLabel());
167 if (uch_id != INVALID_UNICHAR_ID)
169 uch_id, 1, 0.0, certainty);
173 BLOB_CHOICE_LIST *choices_list;
174 BLOB_CHOICE_IT choices_list_it;
175 BLOB_CHOICE_LIST_CLIST *blob_choices =
new BLOB_CHOICE_LIST_CLIST();
176 BLOB_CHOICE_LIST_C_IT blob_choices_it;
177 blob_choices_it.set_to_list(blob_choices);
179 for (
int i = 0; i < werd_choice->
length(); ++i) {
181 choices_list =
new BLOB_CHOICE_LIST();
182 choices_list_it.set_to_list(choices_list);
185 0.0, certainty, -1, -1, 0, 0, 0,
false);
186 choices_list_it.add_after_then_move(blob_choice);
188 blob_choices_it.add_to_end(choices_list);
209 if (cube_cntxt_ ==
NULL) {
211 tprintf(
"Cube WARNING (Tesseract::init_cube_objects()): Failed to "
212 "instantiate CubeRecoContext\n");
223 if (tess_cube_combiner_ !=
NULL) {
224 delete tess_cube_combiner_;
225 tess_cube_combiner_ =
NULL;
228 tprintf(
"Cube ERROR (Failed to instantiate TesseractCubeCombiner\n");
242 if (page_res ==
NULL || tess_cube_combiner_ ==
NULL)
250 int combiner_run_thresh = convert_prob_to_tess_certainty(
263 if (cube_obj !=
NULL)
287 if (!cube_binary_ || !cube_cntxt_) {
289 tprintf(
"Tesseract::run_cube(): NULL binary image.\n");
300 tprintf(
"Cube can't process rotated word at:");
307 cube_cntxt_, cube_binary_, word_box.
left(),
308 pixGetHeight(cube_binary_) - word_box.
top(),
325 float combiner_prob = tess_cube_combiner_->
CombineResults(tess_word,
335 tprintf(
"Combiner prob = %g vs threshold %g\n",
353 tprintf(
"Cube INFO: tesseract result replaced by cube: %s -> %s\n",
374 if (!cube_alt_list || cube_alt_list->
AltCount() <= 0) {
376 tprintf(
"Cube returned nothing for word at:");
385 char_32 *cube_best_32 = cube_alt_list->
Alt(0);
387 float cube_certainty = convert_prob_to_tess_certainty(cube_prob);
388 string cube_best_str;
393 Boxa *char_boxes =
NULL;
398 tprintf(
"Cube WARNING (Tesseract::cube_recognize): Cannot extract "
412 boxaDestroy(&char_boxes);
413 if (!box_word_success) {
415 tprintf(
"Cube WARNING (Tesseract::cube_recognize): Could not "
416 "create cube BoxWord\n");
423 WERD_CHOICE* cube_werd_choice = create_werd_choice(
424 char_samples, num_chars, cube_best_str.c_str(), cube_certainty,
426 delete []char_samples;
428 if (!cube_werd_choice) {
430 tprintf(
"Cube WARNING (Tesseract::cube_recognize): Could not "
431 "create cube WERD_CHOICE\n");
437 tprintf(
"Cube result: %s r=%g, c=%g\n",
439 cube_werd_choice->
rating(),
444 fill_werd_res(cube_box_word, cube_werd_choice, cube_best_str.c_str(), word);
456 const char* cube_best_str,
465 tess_werd_res->
word);
void fill_werd_res(const BoxWord &cube_box_word, WERD_CHOICE *cube_werd_choice, const char *cube_best_str, WERD_RES *tess_werd_res)
void set_text(const char *new_text)
const STRING & unichar_string() const
float CombineResults(WERD_RES *tess_res, CubeObject *cube_obj)
void set_rating(float new_val)
BLOB_CHOICE_LIST_CLIST * blob_choices()
void cube_word_pass1(BLOCK *block, ROW *row, WERD_RES *word)
static CubeRecoContext * Create(Tesseract *tess_obj, TessdataManager *tessdata_manager, UNICHARSET *tess_unicharset)
void ConsumeWordResults(WERD_RES *word)
bool set_unichars_in_script_order(bool in_script_order)
bool SetupForCubeRecognition(const UNICHARSET &unicharset_in, tesseract::Tesseract *tesseract, const BLOCK *block)
int UnicharID(const char_32 *str) const
tesseract::Tesseract * tesseract
WERD_RES * restart_page()
TuningParams * Params() const
double CombinerRunThresh() const
void ClipToOriginalWord(const BLOCK *block, WERD *original_word)
double CombinerClassifierThresh() const
bool init_cube_objects(bool load_combiner, TessdataManager *tessdata_manager)
bool cube_recognize(CubeObject *cube_obj, BLOCK *block, WERD_RES *word)
bool extract_cube_state(CubeObject *cube_obj, int *num_chars, Boxa **char_boxes, CharSamp ***char_samples)
CubeSearchObject * SrchObj() const
void run_cube_combiner(PAGE_RES *page_res)
BLOCK_RES * block() const
char_32 * Alt(int alt_idx)
BOOL8 tess_acceptable_word(WERD_CHOICE *word_choice, WERD_CHOICE *raw_choice)
static double Cost2Prob(int cost)
int AltCost(int alt_idx) const
CubeObject * cube_recognize_word(BLOCK *block, WERD_RES *word)
const char * string() const
CharSamp ** BackTrack(SearchObject *srch_obj, int node_index, int *char_cnt, char_32 **str32, Boxa **char_boxes) const
TBOX char_box_to_tbox(Box *char_box, TBOX word_box, int x_offset)
DLLSYM void tprintf(const char *format,...)
void cube_combine_word(CubeObject *cube_obj, WERD_RES *cube_word, WERD_RES *tess_word)
void set_certainty(float new_val)
bool create_cube_box_word(Boxa *char_boxes, int num_chars, TBOX word_box, BoxWord *box_word)
CharSet * CharacterSet() const
void InsertBox(int index, const TBOX &box)
tesseract::BoxWord * box_word
void SetupFake(const UNICHARSET &uch)
const BLOCK * block() const
void rotate(const FCOORD &vec)
void set_blob_choices(BLOB_CHOICE_LIST_CLIST *blob_choices)
WordAltList * RecognizeWord(LangModel *lang_mod=NULL)
static void UTF32ToUTF8(const char_32 *utf32_str, string *str)
void InitForRetryRecognition(const WERD_RES &source)
FCOORD re_rotation() const
int BestPresortedNodeIndex() const
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, char fragment_length, float rating, float certainty)
const UNICHAR_ID unichar_id(int index) const
void initialise(inT16 length)
BeamSearch * BeamObj() const
WERD_CHOICE * best_choice