diff --git a/src/api/lstmboxrenderer.cpp b/src/api/lstmboxrenderer.cpp index fa77248dee..6ddb8f09ac 100644 --- a/src/api/lstmboxrenderer.cpp +++ b/src/api/lstmboxrenderer.cpp @@ -3,7 +3,7 @@ * Description: Renderer for creating box file for LSTM training. * based on the tsv renderer. * - * (C) Copyright 2006, Google Inc. + * (C) Copyright 2019, Google Inc. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at @@ -35,14 +35,13 @@ static void AddBoxToLSTM(int right, int bottom, int top, int image_height, text->add_str_int(" ", page_num); } -char* TessBaseAPI::GetLSTMBoxText(int page_number) { +char* TessBaseAPI::GetLSTMBoxText(int page_number=0) { if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0)) return nullptr; STRING lstm_box_str(""); - int page_num = page_number; bool first_word = true; - int left, top, right, bottom; + int left = 0, top = 0, right = 0, bottom = 0; LTRResultIterator* res_it = GetLTRIterator(); while (!res_it->Empty(RIL_BLOCK)) { @@ -54,14 +53,14 @@ char* TessBaseAPI::GetLSTMBoxText(int page_number) { if (!(res_it->IsAtBeginningOf(RIL_TEXTLINE))) { if (res_it->IsAtBeginningOf(RIL_WORD)) { lstm_box_str.add_str_int(" ", left); - AddBoxToLSTM(right, bottom, top, image_height_, page_num, + AddBoxToLSTM(right, bottom, top, image_height_, page_number, &lstm_box_str); lstm_box_str += "\n"; // end of row for word } // word } else { if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) { lstm_box_str.add_str_int("\t ", left); - AddBoxToLSTM(right, bottom, top, image_height_, page_num, + AddBoxToLSTM(right, bottom, top, image_height_, page_number, &lstm_box_str); lstm_box_str += "\n"; // end of row for line } // line @@ -76,12 +75,14 @@ char* TessBaseAPI::GetLSTMBoxText(int page_number) { res_it->Next(RIL_SYMBOL); } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_SYMBOL)); lstm_box_str.add_str_int(" ", left); - AddBoxToLSTM(right, bottom, top, image_height_, page_num, &lstm_box_str); + AddBoxToLSTM(right, bottom, top, image_height_, page_number, &lstm_box_str); lstm_box_str += "\n"; // end of row for symbol } - lstm_box_str.add_str_int("\t ", left); - AddBoxToLSTM(right, bottom, top, image_height_, page_num, &lstm_box_str); - lstm_box_str += "\n"; // end of PAGE + if (!first_word) { // if first_word is true => empty page + lstm_box_str.add_str_int("\t ", left); + AddBoxToLSTM(right, bottom, top, image_height_, page_number, &lstm_box_str); + lstm_box_str += "\n"; // end of PAGE + } char* ret = new char[lstm_box_str.length() + 1]; strcpy(ret, lstm_box_str.string()); delete res_it; diff --git a/src/api/wordstrboxrenderer.cpp b/src/api/wordstrboxrenderer.cpp index 73878901bc..ef0dbf86c3 100644 --- a/src/api/wordstrboxrenderer.cpp +++ b/src/api/wordstrboxrenderer.cpp @@ -3,7 +3,7 @@ * Description: Renderer for creating box file with WordStr strings. * based on the tsv renderer. * - * (C) Copyright 2006, Google Inc. + * (C) Copyright 2019, Google Inc. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at @@ -28,13 +28,13 @@ namespace tesseract { * file. Returned string must be freed with the delete [] operator. */ -char* TessBaseAPI::GetWordStrBoxText(int page_number) { +char* TessBaseAPI::GetWordStrBoxText(int page_number=0) { if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0)) return nullptr; STRING wordstr_box_str(""); - int left, top, right, bottom; - int page_num = page_number; + int left = 0, top = 0, right = 0, bottom = 0; + bool first_line = true; LTRResultIterator* res_it = GetLTRIterator(); @@ -44,24 +44,24 @@ char* TessBaseAPI::GetWordStrBoxText(int page_number) { continue; } + // Use bounding box for whole line for WordStr + res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom); if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) { if (!first_line) { wordstr_box_str.add_str_int("\n\t ", right + 1); wordstr_box_str.add_str_int(" ", image_height_ - bottom); wordstr_box_str.add_str_int(" ", right + 5); wordstr_box_str.add_str_int(" ", image_height_ - top); - wordstr_box_str.add_str_int(" ", page_num); // row for tab for EOL + wordstr_box_str.add_str_int(" ", page_number); // row for tab for EOL wordstr_box_str += "\n"; } else { first_line = false; } - // Use bounding box for whole line for WordStr - res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom); wordstr_box_str.add_str_int("WordStr ", left); wordstr_box_str.add_str_int(" ", image_height_ - bottom); wordstr_box_str.add_str_int(" ", right); wordstr_box_str.add_str_int(" ", image_height_ - top); - wordstr_box_str.add_str_int(" ", page_num); // word + wordstr_box_str.add_str_int(" ", page_number); // word wordstr_box_str += " #"; } do { @@ -71,12 +71,15 @@ char* TessBaseAPI::GetWordStrBoxText(int page_number) { res_it->Next(RIL_WORD); } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD)); } - wordstr_box_str.add_str_int("\n\t ", right + 1); - wordstr_box_str.add_str_int(" ", image_height_ - bottom); - wordstr_box_str.add_str_int(" ", right + 5); - wordstr_box_str.add_str_int(" ", image_height_ - top); - wordstr_box_str.add_str_int(" ", page_num); // row for tab for EOL - wordstr_box_str += "\n"; + + if (left != 0 && top != 0 && right != 0 && bottom != 0) { + wordstr_box_str.add_str_int("\n\t ", right + 1); + wordstr_box_str.add_str_int(" ", image_height_ - bottom); + wordstr_box_str.add_str_int(" ", right + 5); + wordstr_box_str.add_str_int(" ", image_height_ - top); + wordstr_box_str.add_str_int(" ", page_number); // row for tab for EOL + wordstr_box_str += "\n"; + } char* ret = new char[wordstr_box_str.length() + 1]; strcpy(ret, wordstr_box_str.string()); delete res_it;