|
| 1 | +/********************************************************************** |
| 2 | + * File: lstmboxrenderer.cpp |
| 3 | + * Description: Renderer for creating box file for LSTM training. |
| 4 | + * based on the tsv renderer. |
| 5 | + * |
| 6 | + * (C) Copyright 2006, Google Inc. |
| 7 | + ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + ** you may not use this file except in compliance with the License. |
| 9 | + ** You may obtain a copy of the License at |
| 10 | + ** http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + ** Unless required by applicable law or agreed to in writing, software |
| 12 | + ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + ** See the License for the specific language governing permissions and |
| 15 | + ** limitations under the License. |
| 16 | + * |
| 17 | + **********************************************************************/ |
| 18 | + |
| 19 | + |
| 20 | +#include <locale> // for std::locale::classic |
| 21 | +#include <memory> // for std::unique_ptr |
| 22 | +#include <sstream> // for std::stringstream |
| 23 | +#include "baseapi.h" // for TessBaseAPI |
| 24 | +#include "renderer.h" |
| 25 | +#include "tesseractclass.h" // for Tesseract |
| 26 | + |
| 27 | +namespace tesseract { |
| 28 | + |
| 29 | +/** |
| 30 | + * Create a UTF8 box file for LSTM training from the internal data structures. |
| 31 | + * page_number is a 0-base page index that will appear in the box file. |
| 32 | + * Returned string must be freed with the delete [] operator. |
| 33 | + */ |
| 34 | + |
| 35 | +char* TessBaseAPI::GetLSTMBOXText(int page_number) { |
| 36 | + if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0)) |
| 37 | + return nullptr; |
| 38 | + |
| 39 | + STRING lstm_box_str(""); |
| 40 | + |
| 41 | + int page_num = page_number; |
| 42 | + bool first_word = true; |
| 43 | + |
| 44 | + LTRResultIterator* res_it = GetLTRIterator(); |
| 45 | + while (!res_it->Empty(RIL_BLOCK)) { |
| 46 | + if (res_it->Empty(RIL_SYMBOL)) { |
| 47 | + res_it->Next(RIL_SYMBOL); |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + int left, top, right, bottom; |
| 52 | + |
| 53 | + if (!first_word) { |
| 54 | + if (res_it->IsAtBeginningOf(RIL_WORD)) { |
| 55 | + lstm_box_str.add_str_int(" ", left); |
| 56 | + lstm_box_str.add_str_int(" ", image_height_ - bottom); |
| 57 | + lstm_box_str.add_str_int(" ", right + 2); |
| 58 | + lstm_box_str.add_str_int(" ", image_height_ - top); |
| 59 | + lstm_box_str.add_str_int(" ", page_num); // level 5 - word |
| 60 | + lstm_box_str += "\n"; // end of row for word |
| 61 | + } |
| 62 | + if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) { |
| 63 | + lstm_box_str.add_str_int("\t ", left); |
| 64 | + lstm_box_str.add_str_int(" ", image_height_ - bottom); |
| 65 | + lstm_box_str.add_str_int(" ", right + 5); |
| 66 | + lstm_box_str.add_str_int(" ", image_height_ - top); |
| 67 | + lstm_box_str.add_str_int(" ", page_num); // level 4 - line |
| 68 | + lstm_box_str += "\n"; // end of row for line |
| 69 | + } |
| 70 | + } |
| 71 | + first_word=false; |
| 72 | + res_it->BoundingBox(RIL_SYMBOL, &left, &top, &right, &bottom); |
| 73 | + |
| 74 | + do { |
| 75 | + lstm_box_str +=std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_SYMBOL)).get(); |
| 76 | + res_it->Next(RIL_SYMBOL); |
| 77 | + } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_SYMBOL)); |
| 78 | + |
| 79 | + lstm_box_str.add_str_int(" ", left); |
| 80 | + lstm_box_str.add_str_int(" ", image_height_ - bottom); |
| 81 | + lstm_box_str.add_str_int(" ", right); |
| 82 | + lstm_box_str.add_str_int(" ", image_height_ - top); |
| 83 | + lstm_box_str.add_str_int(" ", page_num); // level 6 - symbol |
| 84 | + lstm_box_str += "\n"; // end of row |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + char* ret = new char[lstm_box_str.length() + 1]; |
| 89 | + strcpy(ret, lstm_box_str.string()); |
| 90 | + delete res_it; |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +/********************************************************************** |
| 95 | + * LSTMBOX Renderer interface implementation |
| 96 | + **********************************************************************/ |
| 97 | +TessLSTMBOXRenderer::TessLSTMBOXRenderer(const char *outputbase) |
| 98 | + : TessResultRenderer(outputbase, "box") { |
| 99 | +} |
| 100 | + |
| 101 | +bool TessLSTMBOXRenderer::AddImageHandler(TessBaseAPI* api) { |
| 102 | + const std::unique_ptr<const char[]> lstmbox(api->GetLSTMBOXText(imagenum())); |
| 103 | + if (lstmbox == nullptr) return false; |
| 104 | + |
| 105 | + AppendString(lstmbox.get()); |
| 106 | + |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace tesseract. |
0 commit comments