2019-02-01 01:30:59 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: lstmboxrenderer.cpp
|
|
|
|
* Description: Renderer for creating box file for LSTM training.
|
|
|
|
* based on the tsv renderer.
|
|
|
|
*
|
|
|
|
* (C) Copyright 2006, 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
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "baseapi.h" // for TessBaseAPI
|
|
|
|
#include "renderer.h"
|
|
|
|
#include "tesseractclass.h" // for Tesseract
|
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a UTF8 box file for LSTM training from the internal data structures.
|
|
|
|
* page_number is a 0-base page index that will appear in the box file.
|
|
|
|
* Returned string must be freed with the delete [] operator.
|
|
|
|
*/
|
2019-02-19 20:53:31 +08:00
|
|
|
static void AddBoxToLSTM(int right, int bottom, int top,
|
2019-02-10 17:16:45 +08:00
|
|
|
int image_height_, int page_num,
|
|
|
|
STRING* text) {
|
|
|
|
text->add_str_int(" ", image_height_ - bottom);
|
|
|
|
text->add_str_int(" ", right + 5);
|
|
|
|
text->add_str_int(" ", image_height_ - top);
|
2019-02-19 20:53:31 +08:00
|
|
|
text->add_str_int(" ", page_num);
|
2019-02-10 17:16:45 +08:00
|
|
|
}
|
2019-02-19 20:53:31 +08:00
|
|
|
|
2019-03-02 17:11:47 +08:00
|
|
|
char* TessBaseAPI::GetLSTMBoxText(int page_number) {
|
2019-02-01 01:30:59 +08:00
|
|
|
if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
STRING lstm_box_str("");
|
|
|
|
int page_num = page_number;
|
|
|
|
bool first_word = true;
|
2019-02-10 17:16:45 +08:00
|
|
|
int left, top, right, bottom;
|
2019-02-19 20:53:31 +08:00
|
|
|
|
2019-02-01 01:30:59 +08:00
|
|
|
LTRResultIterator* res_it = GetLTRIterator();
|
|
|
|
while (!res_it->Empty(RIL_BLOCK)) {
|
|
|
|
if (res_it->Empty(RIL_SYMBOL)) {
|
|
|
|
res_it->Next(RIL_SYMBOL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!first_word) {
|
2019-02-10 17:16:45 +08:00
|
|
|
if (!(res_it->IsAtBeginningOf(RIL_TEXTLINE))) {
|
2019-02-01 01:30:59 +08:00
|
|
|
if (res_it->IsAtBeginningOf(RIL_WORD)) {
|
|
|
|
lstm_box_str.add_str_int(" ", left);
|
2019-02-10 17:16:45 +08:00
|
|
|
AddBoxToLSTM(right, bottom, top, image_height_, page_num, &lstm_box_str);
|
2019-02-01 01:30:59 +08:00
|
|
|
lstm_box_str += "\n"; // end of row for word
|
2019-02-10 17:16:45 +08:00
|
|
|
} // word
|
|
|
|
} else {
|
2019-02-01 01:30:59 +08:00
|
|
|
if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
|
|
|
|
lstm_box_str.add_str_int("\t ", left);
|
2019-02-10 17:16:45 +08:00
|
|
|
AddBoxToLSTM(right, bottom, top, image_height_, page_num, &lstm_box_str);
|
2019-02-01 01:30:59 +08:00
|
|
|
lstm_box_str += "\n"; // end of row for line
|
2019-02-10 17:16:45 +08:00
|
|
|
} // line
|
|
|
|
}
|
|
|
|
} // not first word
|
2019-02-01 01:30:59 +08:00
|
|
|
first_word=false;
|
2019-02-10 17:16:45 +08:00
|
|
|
// Use bounding box for whole line for everything
|
2019-02-01 16:29:03 +08:00
|
|
|
res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom);
|
2019-02-10 17:16:45 +08:00
|
|
|
do { lstm_box_str +=
|
2019-02-01 16:29:03 +08:00
|
|
|
std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_SYMBOL)).get();
|
2019-02-01 01:30:59 +08:00
|
|
|
res_it->Next(RIL_SYMBOL);
|
2019-02-01 16:29:03 +08:00
|
|
|
} while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_SYMBOL));
|
2019-02-01 01:30:59 +08:00
|
|
|
lstm_box_str.add_str_int(" ", left);
|
2019-02-10 17:16:45 +08:00
|
|
|
AddBoxToLSTM(right, bottom, top, image_height_, page_num, &lstm_box_str);
|
|
|
|
lstm_box_str += "\n"; // end of row for symbol
|
2019-02-01 01:30:59 +08:00
|
|
|
}
|
2019-02-10 17:16:45 +08:00
|
|
|
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
|
2019-02-01 01:30:59 +08:00
|
|
|
char* ret = new char[lstm_box_str.length() + 1];
|
|
|
|
strcpy(ret, lstm_box_str.string());
|
|
|
|
delete res_it;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
2019-03-02 17:11:47 +08:00
|
|
|
* LSTMBox Renderer interface implementation
|
2019-02-01 01:30:59 +08:00
|
|
|
**********************************************************************/
|
2019-03-02 17:11:47 +08:00
|
|
|
TessLSTMBoxRenderer::TessLSTMBoxRenderer(const char *outputbase)
|
2019-02-01 01:30:59 +08:00
|
|
|
: TessResultRenderer(outputbase, "box") {
|
|
|
|
}
|
|
|
|
|
2019-03-02 17:11:47 +08:00
|
|
|
bool TessLSTMBoxRenderer::AddImageHandler(TessBaseAPI* api) {
|
|
|
|
const std::unique_ptr<const char[]> lstmbox(api->GetLSTMBoxText(imagenum()));
|
2019-02-01 01:30:59 +08:00
|
|
|
if (lstmbox == nullptr) return false;
|
|
|
|
|
|
|
|
AppendString(lstmbox.get());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace tesseract.
|