21 #include "allheaders.h" 43 const STRING& output_basename,
44 BLOCK_LIST *block_list) {
45 STRING lstmf_name = output_basename +
".lstmf";
50 tprintf(
"Failed to read training data from %s!\n", lstmf_name.
string());
60 tprintf(
"Failed to read boxes from %s\n", input_imagename.
string());
66 tprintf(
"Failed to write training data to %s!\n", lstmf_name.
string());
75 BLOCK_LIST *block_list,
77 int box_count = boxes.
size();
82 while (end_box < texts.
size() && texts[end_box] ==
"\t") ++end_box;
83 for (
int start_box = end_box; start_box < box_count; start_box = end_box) {
85 TBOX line_box = boxes[start_box];
86 STRING line_str = texts[start_box];
87 for (end_box = start_box + 1; end_box < box_count && texts[end_box] !=
"\t";
89 line_box += boxes[end_box];
90 line_str += texts[end_box];
93 BLOCK* best_block = NULL;
95 BLOCK_IT b_it(block_list);
96 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
97 BLOCK* block = b_it.data();
104 if (overlap_box.
area() > best_overlap) {
105 best_overlap = overlap_box.
area();
111 if (best_block == NULL) {
112 tprintf(
"No block overlapping textline: %s\n", line_str.
string());
114 imagedata =
GetLineData(line_box, boxes, texts, start_box, end_box,
117 if (imagedata != NULL)
121 while (end_box < texts.
size() && texts[end_box] ==
"\t") ++end_box;
131 int start_box,
int end_box,
132 const BLOCK& block) {
136 if (image_data == NULL)
return NULL;
143 for (
int b = start_box; b < end_box; ++b) {
145 box.
rotate(block_rotation);
152 image_data->
AddBoxes(line_boxes, line_texts, page_numbers);
163 int padding,
TBOX* revised_box)
const {
165 wbox.
pad(padding, padding);
169 int num_rotations = 0;
183 int width = pixGetWidth(pix);
184 int height = pixGetHeight(pix);
185 TBOX image_box(0, 0, width, height);
187 *revised_box &= image_box;
188 if (revised_box->
null_box())
return NULL;
189 Box* clip_box = boxCreate(revised_box->
left(), height - revised_box->
top(),
191 Pix* box_pix = pixClipRectangle(pix, clip_box, NULL);
192 if (box_pix == NULL)
return NULL;
193 boxDestroy(&clip_box);
194 if (num_rotations > 0) {
195 Pix* rot_pix = pixRotateOrth(box_pix, num_rotations);
196 pixDestroy(&box_pix);
200 int depth = pixGetDepth(box_pix);
203 grey = pixConvertTo8(box_pix,
false);
204 pixDestroy(&box_pix);
207 bool vertical_text =
false;
208 if (num_rotations > 0) {
211 revised_box->
rotate(rotation);
212 if (num_rotations != 2)
213 vertical_text =
true;
215 return new ImageData(vertical_text, box_pix);
218 #ifndef ANDROID_BUILD 238 if (im_data == NULL)
return;
240 kWorstDictCertainty / kCertaintyScale,
254 const Dict* stopper_dict = lstm_recognizer_->
GetDict();
255 if (stopper_dict ==
nullptr) stopper_dict = &
getDict();
256 bool any_nonspace_delimited =
false;
257 for (
int w = 0; w < words->
size(); ++w) {
261 any_nonspace_delimited =
true;
265 for (
int w = 0; w < words->
size(); ++w) {
286 tprintf(
"Best choice certainty=%g, space=%g, scaled=%g, final=%g\n",
299 #endif // ANDROID_BUILD
Dict & getDict() override
void LSTMRecognizeWord(const BLOCK &block, ROW *row, WERD_RES *word, PointerVector< WERD_RES > *words)
TBOX intersection(const TBOX &box) const
ImageData * GetLineData(const TBOX &line_box, const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, int start_box, int end_box, const BLOCK &block)
bool LoadDocument(const char *filename, int start_page, inT64 max_memory, FileReader reader)
const float kCertaintyScale
tesseract::Tesseract * tesseract
Treat the image as a single word.
void set_page_number(int num)
void SearchWords(PointerVector< WERD_RES > *words)
WERD_CHOICE * best_choice
bool AcceptableResult(WERD_RES *word) const
void AddBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< int > &box_pages)
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
const char * string() const
void RecognizeLine(const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words)
void pad(int xpad, int ypad)
void TrainFromBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, BLOCK_LIST *block_list, DocumentData *training_data)
bool major_overlap(const TBOX &box) const
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
void move(const ICOORD vec)
void initialise(inT16 length)
int tessedit_pageseg_mode
ImageData * GetRectImage(const TBOX &box, const BLOCK &block, int padding, TBOX *revised_box) const
void set_certainty(float new_val)
GenericVector< int > best_state
POLY_BLOCK * poly_block() const
bool ContainsAnyNonSpaceDelimited() const
float base_line(float xpos) const
void rotate(const FCOORD &vec)
bool SaveDocument(const char *filename, FileWriter writer)
const UNICHARSET & GetUnicharset() const
TBOX bounding_box() const
const float kWorstDictCertainty
void SetupFake(const UNICHARSET &uch)
const ICOORD & botleft() const
void init_to_size(int size, T t)
FCOORD re_rotation() const
void TrainLineRecognizer(const STRING &input_imagename, const STRING &output_basename, BLOCK_LIST *block_list)
void AddPageToDocument(ImageData *page)
int state(int index) const
const Dict * GetDict() const