21 #pragma warning(disable:4244) // Conversion warnings 30 #include "allheaders.h" 82 static void clear_any_old_text(BLOCK_LIST *block_list) {
83 BLOCK_IT block_it(block_list);
84 for (block_it.mark_cycle_pt();
85 !block_it.cycled_list(); block_it.forward()) {
86 ROW_IT row_it(block_it.data()->row_list());
87 for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
88 WERD_IT word_it(row_it.data()->word_list());
89 for (word_it.mark_cycle_pt();
90 !word_it.cycled_list(); word_it.forward()) {
91 word_it.data()->set_text(
"");
118 bool find_segmentation,
119 BLOCK_LIST *block_list) {
127 int box_count = boxes.
size();
128 int box_failures = 0;
136 PAGE_RES* page_res = find_segmentation ?
138 clear_any_old_text(block_list);
140 for (
int i = 0; i < boxes.
size() - 1; i++) {
141 bool foundit =
false;
142 if (page_res != NULL) {
145 full_texts[i].
string());
148 boxes[i + 1], full_texts[i].
string());
157 "FAILURE! Couldn't find a matching blob");
161 if (page_res == NULL) {
169 tprintf(
" Boxes read from boxfile: %6d\n", box_count);
170 if (box_failures > 0)
171 tprintf(
" Boxes failed resegmentation: %6d\n", box_failures);
178 static double MedianXHeight(BLOCK_LIST *block_list) {
179 BLOCK_IT block_it(block_list);
180 STATS xheights(0, block_it.data()->bounding_box().height());
181 for (block_it.mark_cycle_pt();
182 !block_it.cycled_list(); block_it.forward()) {
183 ROW_IT row_it(block_it.data()->row_list());
184 for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
188 return xheights.median();
194 double median_xheight = MedianXHeight(block_list);
197 BLOCK_IT b_it(block_list);
198 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
199 BLOCK* block = b_it.data();
201 for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward ()) {
202 ROW* row = r_it.data();
203 float diff = fabs(row->
x_height() - median_xheight);
204 if (diff > max_deviation) {
206 tprintf(
"row xheight=%g, but median xheight = %g\n",
218 BLOCK_LIST *block_list) {
221 BLOCK_IT b_it(block_list);
222 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
223 BLOCK* block = b_it.data();
225 for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward ()) {
226 ROW* row = r_it.data();
228 for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
229 WERD* word = w_it.data();
231 delete w_it.extract();
242 while ((word_res = pr_it.
word()) != NULL) {
244 pr_it.
row()->
row, word_res);
266 tprintf(
"Maximally chopping word at:");
271 float rating =
static_cast<float>(
MAX_INT8);
286 const double e = exp(1.0);
288 int right_chop_index = 0;
293 &blob_number)) != NULL) {
295 BLOB_CHOICE* left_choice = blob_choices[blob_number];
296 rating = left_choice->
rating() / e;
301 rating - 0.125f, -rating, -1,
303 blob_choices.
insert(right_choice, blob_number + 1);
321 static double BoxMissMetric(
const TBOX& box1,
const TBOX& box2) {
323 double miss_metric = box1.
area()- overlap_area;
324 miss_metric /= box1.
area();
325 miss_metric *= box2.
area() - overlap_area;
326 miss_metric /= box2.
area();
341 const TBOX& box,
const TBOX& next_box,
342 const char* correct_text) {
344 tprintf(
"\nAPPLY_BOX: in ResegmentCharBox() for %s\n", correct_text);
348 for (word_res = page_res_it.
word(); word_res != NULL;
349 word_res = page_res_it.
forward()) {
357 for (
int i = 0; i < word_len; ++i) {
360 for (blob_count = 0; i + blob_count < word_len; ++blob_count) {
366 double current_box_miss_metric = BoxMissMetric(blob_box, box);
367 double next_box_miss_metric = BoxMissMetric(blob_box, next_box);
371 tprintf(
"Current miss metric = %g, next = %g\n",
372 current_box_miss_metric, next_box_miss_metric);
374 if (current_box_miss_metric > next_box_miss_metric)
376 char_box += blob_box;
378 if (blob_count > 0) {
380 tprintf(
"Index [%d, %d) seem good.\n", i, i + blob_count);
383 (box.
x_gap(next_box) < -3 ||
384 (prev_box != NULL && prev_box->
x_gap(box) < -3))) {
395 tprintf(
"%d Blobs match: blob box:", blob_count);
404 for (
int j = 1; j < blob_count; ++j) {
439 const TBOX& box,
const TBOX& next_box,
440 const char* correct_text) {
442 tprintf(
"\nAPPLY_BOX: in ResegmentWordBox() for %s\n", correct_text);
444 WERD* new_word = NULL;
445 BLOCK_IT b_it(block_list);
446 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
447 BLOCK* block = b_it.data();
451 for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
452 ROW* row = r_it.data();
456 for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
457 WERD* word = w_it.data();
462 if (word->
text() != NULL && word->
text()[0] !=
'\0')
467 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list();
469 C_BLOB* blob = blob_it.data();
473 double current_box_miss_metric = BoxMissMetric(blob_box, box);
474 double next_box_miss_metric = BoxMissMetric(blob_box, next_box);
478 tprintf(
"Current miss metric = %g, next = %g\n",
479 current_box_miss_metric, next_box_miss_metric);
481 if (current_box_miss_metric > next_box_miss_metric)
491 if (new_word == NULL) {
495 w_it.add_to_end(new_word);
497 C_BLOB_IT new_blob_it(new_word->
cblob_list());
498 new_blob_it.add_to_end(blob_it.extract());
504 return new_word != NULL;
512 for (; (word_res = pr_it.
word()) != NULL; pr_it.
forward()) {
514 if (word->
text() == NULL || word->
text()[0] ==
'\0')
519 tprintf(
"APPLY_BOX: FAILURE: can't find class_id for '%s'\n",
525 tprintf(
"APPLY_BOX: FAILURE: can't find segmentation for '%s'\n",
537 for (
int step = 0; *utf8 !=
'\0'; utf8 += step) {
538 const char* next_space = strchr(utf8,
' ');
539 if (next_space == NULL)
540 next_space = utf8 + strlen(utf8);
541 step = next_space - utf8;
543 if (class_id == INVALID_UNICHAR_ID) {
546 while (utf8[step] ==
' ')
565 for (
int i = 0; i < word_length; ++i) {
566 for (
int j = 1; j <=
kMaxGroupSize && i + j <= word_length; ++j) {
568 word_res->
seam_array, i, i + j - 1,
"Applybox",
582 float best_rating = 0.0f;
584 &search_segmentation, &best_rating, &word_res->
best_state);
585 for (
int i = 0; i < word_length; ++i)
586 choices[i].delete_data_pointers();
608 for (
int i = 0; i < target_text.
size(); ++i) {
630 int choices_pos,
int choices_length,
637 for (
int length = 1; length <= choices[choices_pos].
size(); ++length) {
639 float choice_rating = 0.0f;
641 BLOB_CHOICE_IT choice_it(choices[choices_pos][length - 1]);
642 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
643 choice_it.forward()) {
645 choice_rating = choice->
rating();
647 if (class_id == target_text[text_index]) {
651 if (class_id < table.size() && table[class_id] != NULL) {
652 AmbigSpec_IT spec_it(table[class_id]);
653 for (spec_it.mark_cycle_pt(); !spec_it.cycled_list();
655 const AmbigSpec *ambig_spec = spec_it.data();
657 if (ambig_spec->wrong_ngram[1] == INVALID_UNICHAR_ID &&
658 ambig_spec->correct_ngram_id == target_text[text_index])
661 if (!spec_it.cycled_list())
665 if (choice_it.cycled_list())
668 if (choices_pos + length == choices_length &&
669 text_index + 1 == target_text.
size()) {
672 tprintf(
"Complete match, rating = %g, best=%g, seglength=%d, best=%d\n",
673 rating + choice_rating, *best_rating, segmentation->
size(),
674 best_segmentation->
size());
676 if (best_segmentation->
empty() || rating + choice_rating < *best_rating) {
677 *best_segmentation = *segmentation;
678 *best_rating = rating + choice_rating;
680 }
else if (choices_pos + length < choices_length &&
681 text_index + 1 < target_text.
size()) {
683 tprintf(
"Match found for %d=%s:%s, at %d+%d, recursing...\n",
684 target_text[text_index],
686 choice_it.data()->unichar_id() == target_text[text_index]
688 choices_pos, length);
690 SearchForText(choices, choices_pos + length, choices_length, target_text,
691 text_index + 1, rating + choice_rating, segmentation,
692 best_rating, best_segmentation);
694 tprintf(
"End recursion for %d=%s\n", target_text[text_index],
707 int ok_blob_count = 0;
708 int bad_blob_count = 0;
709 int ok_word_count = 0;
710 int unlabelled_words = 0;
713 for (; (word_res = pr_it.
word()) != NULL; pr_it.
forward()) {
718 for (
int c = 0; c < blob_count; ++c) {
727 INVALID_UNICHAR_ID, word_res->
best_state[c], 1.0f, -1.0f);
729 if (ok_in_word > 0) {
730 ok_blob_count += ok_in_word;
737 tprintf(
"APPLY_BOXES: Unlabelled word at :");
745 for (; (word_res = pr_it.
word()) != NULL; pr_it.
forward()) {
753 tprintf(
" Found %d good blobs.\n", ok_blob_count);
754 if (bad_blob_count > 0) {
755 tprintf(
" Leaving %d unlabelled blobs in %d words.\n",
756 bad_blob_count, ok_word_count);
758 if (unlabelled_words > 0)
759 tprintf(
" %d remaining unlabelled words deleted.\n", unlabelled_words);
765 const char *box_ch,
const char *err_msg) {
766 tprintf(
"APPLY_BOXES: boxfile line %d/%s ((%d,%d),(%d,%d)): %s\n",
767 boxfile_lineno + 1, box_ch,
774 for (
WERD_RES *word_res = pr_it.
word(); word_res != NULL;
799 for (
WERD_RES *word_res = pr_it.
word(); word_res != NULL;
804 tprintf(
"Generated training data for %d words\n", word_count);
PAGE_RES * SetupApplyBoxes(const GenericVector< TBOX > &boxes, BLOCK_LIST *block_list)
void InsertSeam(int blob_number, SEAM *seam)
virtual BLOB_CHOICE_LIST * classify_piece(const GenericVector< SEAM *> &seams, inT16 start, inT16 end, const char *description, TWERD *word, BlamerBundle *blamer_bundle)
void MaximallyChopWord(const GenericVector< TBOX > &boxes, BLOCK *block, ROW *row, WERD_RES *word_res)
TBOX bounding_box() const
int tessedit_ocr_engine_mode
const double kMaxXHeightDeviationFraction
int IntCastRounded(double x)
const TBOX & bounding_box() const
GenericVector< TBLOB * > blobs
bool FindSegmentation(const GenericVector< UNICHAR_ID > &target_text, WERD_RES *word_res)
bool poly_allow_detailed_fx
void insert(T t, int index)
bool ResegmentCharBox(PAGE_RES *page_res, const TBOX *prev_box, const TBOX &box, const TBOX &next_box, const char *correct_text)
void set_rating(float newrat)
void add(inT32 value, inT32 count)
void set_permuter(uinT8 perm)
TBOX bounding_box() const
ROW_LIST * row_list()
get rows
void set_x_height(float new_xheight)
bool almost_equal(const TBOX &box, int tolerance) const
void LearnWord(const char *fontname, WERD_RES *word)
bool ConvertStringToUnichars(const char *utf8, GenericVector< UNICHAR_ID > *class_ids)
void SearchForText(const GenericVector< BLOB_CHOICE_LIST *> *choices, int choices_pos, int choices_length, const GenericVector< UNICHAR_ID > &target_text, int text_index, float rating, GenericVector< int > *segmentation, float *best_rating, GenericVector< int > *best_segmentation)
WERD_RES * restart_page()
bool LogNewCookedChoice(int max_num_choices, bool debug, WERD_CHOICE *word_choice)
UNICHAR_ID unichar_id() const
void PreenXHeights(BLOCK_LIST *block_list)
GenericVector< SEAM * > seam_array
tesseract::BoxWord * box_word
const char * string() const
bool HasAnySplits() const
ROW_RES * next_row() const
PAGE_RES * ApplyBoxes(const STRING &fname, bool find_segmentation, BLOCK_LIST *block_list)
const UnicharAmbigsVector & dang_ambigs() const
void CorrectClassifyWords(PAGE_RES *page_res)
void ApplyBoxTraining(const STRING &fontname, PAGE_RES *page_res)
void set_certainty(float newrat)
BlamerBundle * blamer_bundle
const UnicharAmbigs & getUnicharAmbigs() const
bool SetupForRecognition(const UNICHARSET &unicharset_in, tesseract::Tesseract *tesseract, Pix *pix, int norm_mode, const TBOX *norm_box, bool numeric_mode, bool use_body_size, bool allow_detailed_fx, ROW *row, const BLOCK *block)
void TidyUp(PAGE_RES *page_res)
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
bool assume_fixed_pitch_char_segment
bool LogNewRawChoice(WERD_CHOICE *word_choice)
int x_gap(const TBOX &box) const
ROW_RES * prev_row() const
BLOCK_RES * block() const
void MergeBoxes(int start, int end)
UNICHAR_ID TESS_API unichar_to_id(const char *const unichar_repr) const
GenericVector< STRING > correct_text
void FakeClassifyWord(int blob_count, BLOB_CHOICE **choices)
const UNICHARSET * uch_set
const TBOX & BlobBox(int index) const
const char * text() const
bool ResegmentWordBox(BLOCK_LIST *block_list, const TBOX &box, const TBOX &next_box, const char *correct_text)
bool major_overlap(const TBOX &box) const
GenericVector< AmbigSpec_LIST * > UnicharAmbigsVector
bool classify_bln_numeric_mode
void ReSegmentByClassification(PAGE_RES *page_res)
void ReportFailedBox(int boxfile_lineno, TBOX box, const char *box_ch, const char *err_msg)
TBOX intersection(const TBOX &box) const
GenericVector< int > best_state
const char * id_to_unichar(UNICHAR_ID id) const
void set_flag(WERD_FLAGS mask, BOOL8 value)
void CloneChoppedToRebuild()
C_BLOB_LIST * cblob_list()
SEAM * chop_one_blob(const GenericVector< TBOX > &boxes, const GenericVector< BLOB_CHOICE *> &blob_choices, WERD_RES *word_res, int *blob_number)
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
bool textord_use_cjk_fp_model
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
TBOX bounding_box() const
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
void set_text(const char *new_text)