tesseract
3.03
|
00001 /****************************************************************** 00002 * File: cube_control.cpp 00003 * Description: Tesseract class methods for invoking cube convolutional 00004 * neural network word recognizer. 00005 * Author: Raquel Romano 00006 * Created: September 2009 00007 * 00008 **********************************************************************/ 00009 00010 // Include automatically generated configuration file if running autoconf. 00011 #ifdef HAVE_CONFIG_H 00012 #include "config_auto.h" 00013 #endif 00014 00015 #include "allheaders.h" 00016 00017 #include "cube_object.h" 00018 #include "cube_reco_context.h" 00019 #include "tesseractclass.h" 00020 #include "tesseract_cube_combiner.h" 00021 00022 namespace tesseract { 00023 00024 /********************************************************************** 00025 * convert_prob_to_tess_certainty 00026 * 00027 * Normalize a probability in the range [0.0, 1.0] to a tesseract 00028 * certainty in the range [-20.0, 0.0] 00029 **********************************************************************/ 00030 static float convert_prob_to_tess_certainty(float prob) { 00031 return (prob - 1.0) * 20.0; 00032 } 00033 00034 /********************************************************************** 00035 * char_box_to_tbox 00036 * 00037 * Create a TBOX from a character bounding box. If nonzero, the 00038 * x_offset accounts for any additional padding of the word box that 00039 * should be taken into account. 00040 * 00041 **********************************************************************/ 00042 TBOX char_box_to_tbox(Box* char_box, TBOX word_box, int x_offset) { 00043 l_int32 left; 00044 l_int32 top; 00045 l_int32 width; 00046 l_int32 height; 00047 l_int32 right; 00048 l_int32 bottom; 00049 00050 boxGetGeometry(char_box, &left, &top, &width, &height); 00051 left += word_box.left() - x_offset; 00052 right = left + width; 00053 top = word_box.bottom() + word_box.height() - top; 00054 bottom = top - height; 00055 return TBOX(left, bottom, right, top); 00056 } 00057 00058 /********************************************************************** 00059 * extract_cube_state 00060 * 00061 * Extract CharSamp objects and character bounding boxes from the 00062 * CubeObject's state. The caller should free both structres. 00063 * 00064 **********************************************************************/ 00065 bool Tesseract::extract_cube_state(CubeObject* cube_obj, 00066 int* num_chars, 00067 Boxa** char_boxes, 00068 CharSamp*** char_samples) { 00069 if (!cube_obj) { 00070 if (cube_debug_level > 0) { 00071 tprintf("Cube WARNING (extract_cube_state): Invalid cube object " 00072 "passed to extract_cube_state\n"); 00073 } 00074 return false; 00075 } 00076 00077 // Note that the CubeObject accessors return either the deslanted or 00078 // regular objects search object or beam search object, whichever 00079 // was used in the last call to Recognize() 00080 CubeSearchObject* cube_search_obj = cube_obj->SrchObj(); 00081 if (!cube_search_obj) { 00082 if (cube_debug_level > 0) { 00083 tprintf("Cube WARNING (Extract_cube_state): Could not retrieve " 00084 "cube's search object in extract_cube_state.\n"); 00085 } 00086 return false; 00087 } 00088 BeamSearch *beam_search_obj = cube_obj->BeamObj(); 00089 if (!beam_search_obj) { 00090 if (cube_debug_level > 0) { 00091 tprintf("Cube WARNING (Extract_cube_state): Could not retrieve " 00092 "cube's beam search object in extract_cube_state.\n"); 00093 } 00094 return false; 00095 } 00096 00097 // Get the character samples and bounding boxes by backtracking 00098 // through the beam search path 00099 int best_node_index = beam_search_obj->BestPresortedNodeIndex(); 00100 *char_samples = beam_search_obj->BackTrack( 00101 cube_search_obj, best_node_index, num_chars, NULL, char_boxes); 00102 if (!*char_samples) 00103 return false; 00104 return true; 00105 } 00106 00107 /********************************************************************** 00108 * create_cube_box_word 00109 * 00110 * Fill the given BoxWord with boxes from character bounding 00111 * boxes. The char_boxes have local coordinates w.r.t. the 00112 * word bounding box, i.e., the left-most character bbox of each word 00113 * has (0,0) left-top coord, but the BoxWord must be defined in page 00114 * coordinates. 00115 **********************************************************************/ 00116 bool Tesseract::create_cube_box_word(Boxa *char_boxes, 00117 int num_chars, 00118 TBOX word_box, 00119 BoxWord* box_word) { 00120 if (!box_word) { 00121 if (cube_debug_level > 0) { 00122 tprintf("Cube WARNING (create_cube_box_word): Invalid box_word.\n"); 00123 } 00124 return false; 00125 } 00126 00127 // Find the x-coordinate of left-most char_box, which could be 00128 // nonzero if the word image was padded before recognition took place. 00129 int x_offset = -1; 00130 for (int i = 0; i < num_chars; ++i) { 00131 Box* char_box = boxaGetBox(char_boxes, i, L_CLONE); 00132 if (x_offset < 0 || char_box->x < x_offset) { 00133 x_offset = char_box->x; 00134 } 00135 boxDestroy(&char_box); 00136 } 00137 00138 for (int i = 0; i < num_chars; ++i) { 00139 Box* char_box = boxaGetBox(char_boxes, i, L_CLONE); 00140 TBOX tbox = char_box_to_tbox(char_box, word_box, x_offset); 00141 boxDestroy(&char_box); 00142 box_word->InsertBox(i, tbox); 00143 } 00144 return true; 00145 } 00146 00147 /********************************************************************** 00148 * init_cube_objects 00149 * 00150 * Instantiates Tesseract object's CubeRecoContext and TesseractCubeCombiner. 00151 * Returns false if cube context could not be created or if load_combiner is 00152 * true, but the combiner could not be loaded. 00153 **********************************************************************/ 00154 bool Tesseract::init_cube_objects(bool load_combiner, 00155 TessdataManager *tessdata_manager) { 00156 ASSERT_HOST(cube_cntxt_ == NULL); 00157 ASSERT_HOST(tess_cube_combiner_ == NULL); 00158 00159 // Create the cube context object 00160 cube_cntxt_ = CubeRecoContext::Create(this, tessdata_manager, &unicharset); 00161 if (cube_cntxt_ == NULL) { 00162 if (cube_debug_level > 0) { 00163 tprintf("Cube WARNING (Tesseract::init_cube_objects()): Failed to " 00164 "instantiate CubeRecoContext\n"); 00165 } 00166 return false; 00167 } 00168 00169 // Create the combiner object and load the combiner net for target languages. 00170 if (load_combiner) { 00171 tess_cube_combiner_ = new tesseract::TesseractCubeCombiner(cube_cntxt_); 00172 if (!tess_cube_combiner_ || !tess_cube_combiner_->LoadCombinerNet()) { 00173 delete cube_cntxt_; 00174 cube_cntxt_ = NULL; 00175 if (tess_cube_combiner_ != NULL) { 00176 delete tess_cube_combiner_; 00177 tess_cube_combiner_ = NULL; 00178 } 00179 if (cube_debug_level > 0) 00180 tprintf("Cube ERROR (Failed to instantiate TesseractCubeCombiner\n"); 00181 return false; 00182 } 00183 } 00184 return true; 00185 } 00186 00187 /********************************************************************** 00188 * run_cube_combiner 00189 * 00190 * Iterates through tesseract's results and calls cube on each word, 00191 * combining the results with the existing tesseract result. 00192 **********************************************************************/ 00193 void Tesseract::run_cube_combiner(PAGE_RES *page_res) { 00194 if (page_res == NULL || tess_cube_combiner_ == NULL) 00195 return; 00196 PAGE_RES_IT page_res_it(page_res); 00197 // Iterate through the word results and call cube on each word. 00198 for (page_res_it.restart_page(); page_res_it.word () != NULL; 00199 page_res_it.forward()) { 00200 BLOCK* block = page_res_it.block()->block; 00201 if (block->poly_block() != NULL && !block->poly_block()->IsText()) 00202 continue; // Don't deal with non-text blocks. 00203 WERD_RES* word = page_res_it.word(); 00204 // Skip cube entirely if tesseract's certainty is greater than threshold. 00205 int combiner_run_thresh = convert_prob_to_tess_certainty( 00206 cube_cntxt_->Params()->CombinerRunThresh()); 00207 if (word->best_choice->certainty() >= combiner_run_thresh) { 00208 continue; 00209 } 00210 // Use the same language as Tesseract used for the word. 00211 Tesseract* lang_tess = word->tesseract; 00212 00213 // Setup a trial WERD_RES in which to classify with cube. 00214 WERD_RES cube_word; 00215 cube_word.InitForRetryRecognition(*word); 00216 cube_word.SetupForRecognition(lang_tess->unicharset, this, BestPix(), 00217 OEM_CUBE_ONLY, 00218 NULL, false, false, false, 00219 page_res_it.row()->row, 00220 page_res_it.block()->block); 00221 CubeObject *cube_obj = lang_tess->cube_recognize_word( 00222 page_res_it.block()->block, &cube_word); 00223 if (cube_obj != NULL) 00224 lang_tess->cube_combine_word(cube_obj, &cube_word, word); 00225 delete cube_obj; 00226 } 00227 } 00228 00229 /********************************************************************** 00230 * cube_word_pass1 00231 * 00232 * Recognizes a single word using (only) cube. Compatible with 00233 * Tesseract's classify_word_pass1/classify_word_pass2. 00234 **********************************************************************/ 00235 void Tesseract::cube_word_pass1(BLOCK* block, ROW *row, WERD_RES *word) { 00236 CubeObject *cube_obj = cube_recognize_word(block, word); 00237 delete cube_obj; 00238 } 00239 00240 /********************************************************************** 00241 * cube_recognize_word 00242 * 00243 * Cube recognizer to recognize a single word as with classify_word_pass1 00244 * but also returns the cube object in case the combiner is needed. 00245 **********************************************************************/ 00246 CubeObject* Tesseract::cube_recognize_word(BLOCK* block, WERD_RES* word) { 00247 if (!cube_binary_ || !cube_cntxt_) { 00248 if (cube_debug_level > 0 && !cube_binary_) 00249 tprintf("Tesseract::run_cube(): NULL binary image.\n"); 00250 word->SetupFake(unicharset); 00251 return NULL; 00252 } 00253 TBOX word_box = word->word->bounding_box(); 00254 if (block != NULL && (block->re_rotation().x() != 1.0f || 00255 block->re_rotation().y() != 0.0f)) { 00256 // TODO(rays) We have to rotate the bounding box to get the true coords. 00257 // This will be achieved in the future via DENORM. 00258 // In the mean time, cube can't process this word. 00259 if (cube_debug_level > 0) { 00260 tprintf("Cube can't process rotated word at:"); 00261 word_box.print(); 00262 } 00263 word->SetupFake(unicharset); 00264 return NULL; 00265 } 00266 CubeObject* cube_obj = new tesseract::CubeObject( 00267 cube_cntxt_, cube_binary_, word_box.left(), 00268 pixGetHeight(cube_binary_) - word_box.top(), 00269 word_box.width(), word_box.height()); 00270 if (!cube_recognize(cube_obj, block, word)) { 00271 delete cube_obj; 00272 return NULL; 00273 } 00274 return cube_obj; 00275 } 00276 00277 /********************************************************************** 00278 * cube_combine_word 00279 * 00280 * Combines the cube and tesseract results for a single word, leaving the 00281 * result in tess_word. 00282 **********************************************************************/ 00283 void Tesseract::cube_combine_word(CubeObject* cube_obj, WERD_RES* cube_word, 00284 WERD_RES* tess_word) { 00285 float combiner_prob = tess_cube_combiner_->CombineResults(tess_word, 00286 cube_obj); 00287 // If combiner probability is greater than tess/cube combiner 00288 // classifier threshold, i.e. tesseract wins, then just return the 00289 // tesseract result unchanged, as the combiner knows nothing about how 00290 // correct the answer is. If cube and tesseract agree, then improve the 00291 // scores before returning. 00292 WERD_CHOICE* tess_best = tess_word->best_choice; 00293 WERD_CHOICE* cube_best = cube_word->best_choice; 00294 if (cube_debug_level || classify_debug_level) { 00295 tprintf("Combiner prob = %g vs threshold %g\n", 00296 combiner_prob, cube_cntxt_->Params()->CombinerClassifierThresh()); 00297 } 00298 if (combiner_prob >= 00299 cube_cntxt_->Params()->CombinerClassifierThresh()) { 00300 if (tess_best->unichar_string() == cube_best->unichar_string()) { 00301 // Cube and tess agree, so improve the scores. 00302 tess_best->set_rating(tess_best->rating() / 2); 00303 tess_best->set_certainty(tess_best->certainty() / 2); 00304 } 00305 return; 00306 } 00307 // Cube wins. 00308 // It is better for the language combiner to have all tesseract scores, 00309 // so put them in the cube result. 00310 cube_best->set_rating(tess_best->rating()); 00311 cube_best->set_certainty(tess_best->certainty()); 00312 if (cube_debug_level || classify_debug_level) { 00313 tprintf("Cube INFO: tesseract result replaced by cube: %s -> %s\n", 00314 tess_best->unichar_string().string(), 00315 cube_best->unichar_string().string()); 00316 } 00317 tess_word->ConsumeWordResults(cube_word); 00318 } 00319 00320 /********************************************************************** 00321 * cube_recognize 00322 * 00323 * Call cube on the current word, and write the result to word. 00324 * Sets up a fake result and returns false if something goes wrong. 00325 **********************************************************************/ 00326 bool Tesseract::cube_recognize(CubeObject *cube_obj, BLOCK* block, 00327 WERD_RES *word) { 00328 // Run cube 00329 WordAltList *cube_alt_list = cube_obj->RecognizeWord(); 00330 if (!cube_alt_list || cube_alt_list->AltCount() <= 0) { 00331 if (cube_debug_level > 0) { 00332 tprintf("Cube returned nothing for word at:"); 00333 word->word->bounding_box().print(); 00334 } 00335 word->SetupFake(unicharset); 00336 return false; 00337 } 00338 00339 // Get cube's best result and its probability, mapped to tesseract's 00340 // certainty range 00341 char_32 *cube_best_32 = cube_alt_list->Alt(0); 00342 double cube_prob = CubeUtils::Cost2Prob(cube_alt_list->AltCost(0)); 00343 float cube_certainty = convert_prob_to_tess_certainty(cube_prob); 00344 string cube_best_str; 00345 CubeUtils::UTF32ToUTF8(cube_best_32, &cube_best_str); 00346 00347 // Retrieve Cube's character bounding boxes and CharSamples, 00348 // corresponding to the most recent call to RecognizeWord(). 00349 Boxa *char_boxes = NULL; 00350 CharSamp **char_samples = NULL;; 00351 int num_chars; 00352 if (!extract_cube_state(cube_obj, &num_chars, &char_boxes, &char_samples) 00353 && cube_debug_level > 0) { 00354 tprintf("Cube WARNING (Tesseract::cube_recognize): Cannot extract " 00355 "cube state.\n"); 00356 word->SetupFake(unicharset); 00357 return false; 00358 } 00359 00360 // Convert cube's character bounding boxes to a BoxWord. 00361 BoxWord cube_box_word; 00362 TBOX tess_word_box = word->word->bounding_box(); 00363 if (word->denorm.block() != NULL) 00364 tess_word_box.rotate(word->denorm.block()->re_rotation()); 00365 bool box_word_success = create_cube_box_word(char_boxes, num_chars, 00366 tess_word_box, 00367 &cube_box_word); 00368 boxaDestroy(&char_boxes); 00369 if (!box_word_success) { 00370 if (cube_debug_level > 0) { 00371 tprintf("Cube WARNING (Tesseract::cube_recognize): Could not " 00372 "create cube BoxWord\n"); 00373 } 00374 word->SetupFake(unicharset); 00375 return false; 00376 } 00377 00378 // Fill tesseract result's fields with cube results 00379 fill_werd_res(cube_box_word, cube_best_str.c_str(), word); 00380 00381 // Create cube's best choice. 00382 BLOB_CHOICE** choices = new BLOB_CHOICE*[num_chars]; 00383 for (int i = 0; i < num_chars; ++i) { 00384 UNICHAR_ID uch_id = 00385 cube_cntxt_->CharacterSet()->UnicharID(char_samples[i]->StrLabel()); 00386 choices[i] = new BLOB_CHOICE(uch_id, 0.0, cube_certainty, -1, -1, 00387 0, 0, 0, 0, BCC_STATIC_CLASSIFIER); 00388 } 00389 word->FakeClassifyWord(num_chars, choices); 00390 // within a word, cube recognizes the word in reading order. 00391 word->best_choice->set_unichars_in_script_order(true); 00392 delete [] choices; 00393 delete [] char_samples; 00394 00395 // Some sanity checks 00396 ASSERT_HOST(word->best_choice->length() == word->reject_map.length()); 00397 00398 if (cube_debug_level || classify_debug_level) { 00399 tprintf("Cube result: %s r=%g, c=%g\n", 00400 word->best_choice->unichar_string().string(), 00401 word->best_choice->rating(), 00402 word->best_choice->certainty()); 00403 } 00404 return true; 00405 } 00406 00407 /********************************************************************** 00408 * fill_werd_res 00409 * 00410 * Fill Tesseract's word result fields with cube's. 00411 * 00412 **********************************************************************/ 00413 void Tesseract::fill_werd_res(const BoxWord& cube_box_word, 00414 const char* cube_best_str, 00415 WERD_RES* tess_werd_res) { 00416 delete tess_werd_res->box_word; 00417 tess_werd_res->box_word = new BoxWord(cube_box_word); 00418 tess_werd_res->box_word->ClipToOriginalWord(tess_werd_res->denorm.block(), 00419 tess_werd_res->word); 00420 // Fill text and remaining fields 00421 tess_werd_res->word->set_text(cube_best_str); 00422 tess_werd_res->tess_failed = FALSE; 00423 tess_werd_res->tess_accepted = tess_acceptable_word(tess_werd_res); 00424 // There is no output word, so we can' call AdaptableWord, but then I don't 00425 // think we need to. Fudge the result with accepted. 00426 tess_werd_res->tess_would_adapt = tess_werd_res->tess_accepted; 00427 00428 // Set word to done, i.e., ignore all of tesseract's tests for rejection 00429 tess_werd_res->done = tess_werd_res->tess_accepted; 00430 } 00431 00432 } // namespace tesseract