21 #include "allheaders.h" 44 const STRING& output_basename,
45 BLOCK_LIST *block_list) {
46 STRING lstmf_name = output_basename +
".lstmf";
51 tprintf(
"Failed to read training data from %s!\n", lstmf_name.
string());
61 tprintf(
"Failed to read boxes from %s\n", input_imagename.
string());
67 tprintf(
"Failed to write training data to %s!\n", lstmf_name.
string());
76 BLOCK_LIST *block_list,
78 int box_count = boxes.
size();
83 while (end_box < texts.
size() && texts[end_box] ==
"\t") ++end_box;
84 for (
int start_box = end_box; start_box < box_count; start_box = end_box) {
86 TBOX line_box = boxes[start_box];
87 STRING line_str = texts[start_box];
88 for (end_box = start_box + 1; end_box < box_count && texts[end_box] !=
"\t";
90 line_box += boxes[end_box];
91 line_str += texts[end_box];
94 BLOCK* best_block =
nullptr;
96 BLOCK_IT b_it(block_list);
97 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
98 BLOCK* block = b_it.data();
105 if (overlap_box.
area() > best_overlap) {
106 best_overlap = overlap_box.
area();
112 if (best_block ==
nullptr) {
113 tprintf(
"No block overlapping textline: %s\n", line_str.
string());
115 imagedata =
GetLineData(line_box, boxes, texts, start_box, end_box,
118 if (imagedata !=
nullptr)
122 while (end_box < texts.
size() && texts[end_box] ==
"\t") ++end_box;
132 int start_box,
int end_box,
133 const BLOCK& block) {
137 if (image_data ==
nullptr)
return nullptr;
144 for (
int b = start_box; b < end_box; ++b) {
146 box.
rotate(block_rotation);
153 image_data->
AddBoxes(line_boxes, line_texts, page_numbers);
164 int padding,
TBOX* revised_box)
const {
166 wbox.
pad(padding, padding);
170 int num_rotations = 0;
184 int width = pixGetWidth(pix);
185 int height = pixGetHeight(pix);
186 TBOX image_box(0, 0, width, height);
188 *revised_box &= image_box;
189 if (revised_box->
null_box())
return nullptr;
190 Box* clip_box = boxCreate(revised_box->
left(), height - revised_box->
top(),
192 Pix* box_pix = pixClipRectangle(pix, clip_box,
nullptr);
193 if (box_pix ==
nullptr)
return nullptr;
194 boxDestroy(&clip_box);
195 if (num_rotations > 0) {
196 Pix* rot_pix = pixRotateOrth(box_pix, num_rotations);
197 pixDestroy(&box_pix);
201 int depth = pixGetDepth(box_pix);
204 grey = pixConvertTo8(box_pix,
false);
205 pixDestroy(&box_pix);
208 bool vertical_text =
false;
209 if (num_rotations > 0) {
212 revised_box->
rotate(rotation);
213 if (num_rotations != 2)
214 vertical_text =
true;
216 return new ImageData(vertical_text, box_pix);
219 #ifndef ANDROID_BUILD 239 if (im_data ==
nullptr)
return;
255 const Dict* stopper_dict = lstm_recognizer_->
GetDict();
256 if (stopper_dict ==
nullptr) stopper_dict = &
getDict();
257 bool any_nonspace_delimited =
false;
258 for (
int w = 0; w < words->
size(); ++w) {
262 any_nonspace_delimited =
true;
266 for (
int w = 0; w < words->
size(); ++w) {
287 tprintf(
"Best choice certainty=%g, space=%g, scaled=%g, final=%g\n",
300 #endif // ANDROID_BUILD void AddPageToDocument(ImageData *page)
const UNICHARSET & GetUnicharset() const
void TrainFromBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, BLOCK_LIST *block_list, DocumentData *training_data)
void rotate(const FCOORD &vec)
Dict & getDict() override
FCOORD re_rotation() const
void LSTMRecognizeWord(const BLOCK &block, ROW *row, WERD_RES *word, PointerVector< WERD_RES > *words)
TBOX intersection(const TBOX &box) const
const char * string() const
int state(int index) const
TBOX bounding_box() const
float base_line(float xpos) const
const float kCertaintyScale
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
ImageData * GetRectImage(const TBOX &box, const BLOCK &block, int padding, TBOX *revised_box) const
const Dict * GetDict() const
void move(const ICOORD vec)
bool ContainsAnyNonSpaceDelimited() const
void SearchWords(PointerVector< WERD_RES > *words)
Treat the image as a single word.
void TrainLineRecognizer(const STRING &input_imagename, const STRING &output_basename, BLOCK_LIST *block_list)
void init_to_size(int size, const T &t)
bool major_overlap(const TBOX &box) const
void RecognizeLine(const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
GenericVector< int > best_state
POLY_BLOCK * poly_block() const
ImageData * GetLineData(const TBOX &line_box, const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, int start_box, int end_box, const BLOCK &block)
DLLSYM void tprintf(const char *format,...)
void AddBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< int > &box_pages)
const ICOORD & botleft() const
void SetupFake(const UNICHARSET &uch)
tesseract::Tesseract * tesseract
bool LoadDocument(const char *filename, int start_page, int64_t max_memory, FileReader reader)
bool SaveDocument(const char *filename, FileWriter writer)
int tessedit_pageseg_mode
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
const float kWorstDictCertainty
bool AcceptableResult(WERD_RES *word) const
WERD_CHOICE * best_choice
void set_certainty(float new_val)
void pad(int xpad, int ypad)
void set_page_number(int num)
void initialise(int16_t length)