22 #include "config_auto.h" 35 #undef __STRICT_ANSI__ 51 #include "allheaders.h" 112 : tesseract_(nullptr),
113 osd_tesseract_(nullptr),
114 equ_detect_(nullptr),
119 thresholder_(nullptr),
120 paragraph_models_(nullptr),
121 block_list_(nullptr),
123 input_file_(nullptr),
124 output_file_(nullptr),
128 recognition_done_(false),
145 #if defined(GIT_REV) && (defined(DEBUG) || defined(_DEBUG)) 160 #if USE_DEVICE_SELECTION 166 #if USE_DEVICE_SELECTION 167 ds_device device = OpenclDevice::getDeviceSelection();
168 if (device.type == DS_DEVICE_OPENCL_DEVICE) {
169 *data =
new cl_device_id;
170 memcpy(*data, &device.oclDeviceID,
sizeof(cl_device_id));
171 return sizeof(cl_device_id);
186 struct sigaction action;
187 memset(&action, 0,
sizeof(action));
189 action.sa_flags = SA_RESETHAND;
190 sigaction(SIGSEGV, &action, NULL);
191 sigaction(SIGFPE, &action, NULL);
192 sigaction(SIGBUS, &action, NULL);
195 tprintf(
"CatchSignals has no non-linux implementation!\n");
231 IntParam *p = ParamUtils::FindParam<IntParam>(
233 if (p == NULL)
return false;
234 *value = (
inT32)(*p);
239 BoolParam *p = ParamUtils::FindParam<BoolParam>(
241 if (p == NULL)
return false;
242 *value = (
BOOL8)(*p);
247 StringParam *p = ParamUtils::FindParam<StringParam>(
249 return (p != NULL) ? p->
string() : NULL;
253 DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
255 if (p == NULL)
return false;
256 *value = (double)(*p);
282 bool set_only_non_debug_params) {
283 return Init(datapath, 0, language, oem, configs, configs_size, vars_vec,
284 vars_values, set_only_non_debug_params,
nullptr);
294 bool set_only_non_debug_params,
FileReader reader) {
297 if (language ==
nullptr) language =
"eng";
298 STRING datapath = data_size == 0 ? data : language;
317 bool reset_classifier =
true;
319 reset_classifier =
false;
321 if (reader !=
nullptr)
reader_ = reader;
323 if (data_size != 0) {
329 language,
oem, configs, configs_size, vars_vec, vars_values,
330 set_only_non_debug_params, &mgr) != 0) {
351 if (reset_classifier) {
383 for (
int i = 0; i < num_subs; ++i)
397 char fname[_MAX_FNAME];
398 WIN32_FIND_DATA data;
400 HANDLE handle = FindFirstFile(pattern.
string(), &data);
402 for (; result; result = FindNextFile(handle, &data)) {
403 _splitpath(data.cFileName, NULL, NULL, fname, NULL);
410 struct dirent *dirent;
417 while ((dirent = readdir(dir))) {
419 if (dirent->d_name[0] !=
'.') {
420 if (strstr(dirent->d_name, extension.
string()) != NULL) {
421 dot = strrchr(dirent->d_name,
'.');
423 if (strncmp(dot, extension.
string(),
424 strlen(extension.
string())) == 0) {
513 int width,
int height) {
514 if (
tesseract_ == NULL || width < kMinRectSize || height < kMinRectSize)
519 int bits_per_pixel = bytes_per_pixel == 0 ? 1 : bytes_per_pixel * 8;
520 SetImage(imagedata, bytes_per_line * 8 / bits_per_pixel, height + top,
521 bytes_per_pixel, bytes_per_line);
546 int width,
int height,
547 int bytes_per_pixel,
int bytes_per_line) {
550 bytes_per_pixel, bytes_per_line);
559 tprintf(
"Please call SetImage before SetSourceResolution.\n");
620 Pixa** pixa,
int** blockids,
int** paraids) {
622 pixa, blockids, paraids);
665 bool text_only,
bool raw_image,
666 const int raw_padding,
667 Pixa** pixa,
int** blockids,
676 int component_count = 0;
677 int left, top, right, bottom;
684 &left, &top, &right, &bottom);
690 level, &left, &top, &right, &bottom);
693 if (get_bbox->
Run() &&
696 }
while (page_it->
Next(level));
698 Boxa* boxa = boxaCreate(component_count);
700 *pixa = pixaCreate(component_count);
701 if (blockids != NULL)
702 *blockids =
new int[component_count];
704 *paraids =
new int[component_count];
708 int component_index = 0;
711 if (get_bbox->
Run() &&
713 Box* lbox = boxCreate(left, top, right - left, bottom - top);
714 boxaAddBox(boxa, lbox, L_INSERT);
723 pixaAddPix(*pixa, pix, L_INSERT);
724 pixaAddBox(*pixa, lbox, L_CLONE);
726 if (paraids != NULL) {
727 (*paraids)[component_index] = paraid;
731 if (blockids != NULL) {
732 (*blockids)[component_index] = blockid;
740 }
while (page_it->
Next(level));
757 FILE *fp = fopen(filename,
"wb");
759 int width = pixGetWidth(pix);
760 int height = pixGetHeight(pix);
761 l_uint32* data = pixGetData(pix);
762 fprintf(fp,
"P5 %d %d 255\n", width, height);
763 for (
int y = 0; y < height; ++y, data += pixGetWpl(pix)) {
764 for (
int x = 0; x < width; ++x) {
765 uint8_t b = GET_DATA_BIT(data, x) ? 0 : 255;
766 fwrite(&b, 1, 1, fp);
855 #ifndef GRAPHICS_DISABLED 857 #endif // GRAPHICS_DISABLED 872 fclose(training_output_file);
875 bool wait_for_text =
true;
892 tprintf(
"Please call SetImage before attempting recognition.");
909 while (page_res_it.
word() != NULL) {
913 page_res_it.
row()->
row, word_res);
942 bool TessBaseAPI::ProcessPagesFileList(FILE *flist,
944 const char* retry_config,
945 int timeout_millisec,
947 int tessedit_page_number) {
948 if (!flist && !buf)
return false;
949 int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
954 buf->
split(
'\n', &lines);
955 if (lines.
empty())
return false;
959 for (
int i = 0; i < page; i++) {
961 if (fgets(pagename,
sizeof(pagename), flist) == NULL)
break;
973 if (fgets(pagename,
sizeof(pagename), flist) == NULL)
break;
975 if (page >= lines.
size())
break;
976 snprintf(pagename,
sizeof(pagename),
"%s", lines[page].c_str());
979 Pix *pix = pixRead(pagename);
981 tprintf(
"Image file %s cannot be read!\n", pagename);
984 tprintf(
"Page %d : %s\n", page, pagename);
985 bool r =
ProcessPage(pix, page, pagename, retry_config,
986 timeout_millisec, renderer);
988 if (!r)
return false;
989 if (tessedit_page_number >= 0)
break;
1000 bool TessBaseAPI::ProcessPagesMultipageTiff(
const l_uint8 *data,
1003 const char* retry_config,
1004 int timeout_millisec,
1006 int tessedit_page_number) {
1007 #ifndef ANDROID_BUILD 1009 int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
1012 if (tessedit_page_number >= 0)
1013 page = tessedit_page_number;
1014 pix = (data) ? pixReadMemFromMultipageTiff(data, size, &offset)
1015 : pixReadFromMultipageTiff(filename, &offset);
1016 if (pix == NULL)
break;
1017 tprintf(
"Page %d\n", page + 1);
1019 snprintf(page_str, kMaxIntSize - 1,
"%d", page);
1021 bool r =
ProcessPage(pix, page, filename, retry_config,
1022 timeout_millisec, renderer);
1024 if (!r)
return false;
1025 if (tessedit_page_number >= 0)
break;
1037 int timeout_millisec,
1063 const char* retry_config,
1064 int timeout_millisec,
1067 bool stdInput = !strcmp(filename,
"stdin") || !strcmp(filename,
"-");
1070 if (_setmode(_fileno(stdin), _O_BINARY) == -1)
1071 tprintf(
"ERROR: cin to binary: %s", strerror(errno));
1076 return ProcessPagesFileList(stdin, NULL, retry_config,
1077 timeout_millisec, renderer,
1085 const l_uint8 *data = NULL;
1087 buf.assign((std::istreambuf_iterator<char>(std::cin)),
1088 (std::istreambuf_iterator<char>()));
1089 data =
reinterpret_cast<const l_uint8 *
>(buf.data());
1094 int r = (stdInput) ?
1095 findFileFormatBuffer(data, &format) :
1096 findFileFormat(filename, &format);
1099 if (r != 0 || format == IFF_UNKNOWN) {
1104 std::ifstream t(filename);
1105 std::string
u((std::istreambuf_iterator<char>(t)),
1106 std::istreambuf_iterator<char>());
1109 return ProcessPagesFileList(NULL, &s, retry_config,
1110 timeout_millisec, renderer,
1115 bool tiff = (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
1116 format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
1117 format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
1118 format == IFF_TIFF_ZIP);
1123 pix = (stdInput) ? pixReadMem(data, buf.size()) : pixRead(filename);
1137 ProcessPagesMultipageTiff(data, buf.size(),
filename, retry_config,
1138 timeout_millisec, renderer,
1141 timeout_millisec, renderer);
1147 if (!r || (renderer && !renderer->EndDocument())) {
1155 const char* retry_config,
int timeout_millisec,
1160 bool failed =
false;
1173 }
else if (timeout_millisec > 0) {
1188 #ifndef ANDROID_BUILD 1190 pixWrite(
"tessinput.tif", page_pix, IFF_TIFF_G4);
1191 #endif // ANDROID_BUILD 1194 if (failed && retry_config != NULL && retry_config[0] !=
'\0') {
1196 FILE* fp = fopen(kOldVarsFile,
"wb");
1207 if (renderer && !failed) {
1208 failed = !renderer->
AddImage(
this);
1272 text += para_text.get();
1274 char* result =
new char[text.
length() + 1];
1288 it->
Orientation(&orientation, &writing_direction, &textline_order,
1301 static void AddBaselineCoordsTohOCR(
const PageIterator *it,
1306 hocr_str->
add_str_int(
"; textangle ", 360 - orientation * 90);
1310 int left, top, right, bottom;
1311 it->
BoundingBox(level, &left, &top, &right, &bottom);
1315 if (!it->
Baseline(level, &x1, &y1, &x2, &y2))
1333 p1 = (y2 - y1) / static_cast<double>(x2 - x1);
1334 p0 = y1 -
static_cast<double>(p1 * x1);
1340 static void AddIdTohOCR(
STRING* hocr_str,
const std::string base,
int num1,
1342 const size_t BUFSIZE = 64;
1343 char id_buffer[BUFSIZE];
1345 snprintf(id_buffer, BUFSIZE - 1,
"%s_%d_%d", base.c_str(), num1, num2);
1347 snprintf(id_buffer, BUFSIZE - 1,
"%s_%d", base.c_str(), num1);
1349 id_buffer[BUFSIZE - 1] =
'\0';
1350 *hocr_str +=
" id='";
1351 *hocr_str += id_buffer;
1357 int left, top, right, bottom;
1358 it->
BoundingBox(level, &left, &top, &right, &bottom);
1367 AddBaselineCoordsTohOCR(it, level, hocr_str);
1369 float row_height, descenders, ascenders;
1381 int left, top, right, bottom;
1382 it->
BoundingBox(level, &left, &top, &right, &bottom);
1415 int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1416 int page_id = page_number + 1;
1417 bool para_is_ltr =
true;
1418 const char* paragraph_lang = NULL;
1419 bool font_info =
false;
1431 wchar_t *uni16_str =
new WCHAR[str16_len];
1433 uni16_str, str16_len);
1434 int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, NULL, 0,
1436 char *utf8_str =
new char[utf8_len];
1437 WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, utf8_str,
1438 utf8_len, NULL, NULL);
1444 hocr_str +=
" <div class='ocr_page'";
1445 AddIdTohOCR(&hocr_str,
"page", page_id, -1);
1446 hocr_str +=
" title='image \"";
1450 hocr_str +=
"unknown";
1469 hocr_str +=
" <div class='ocr_carea'";
1470 AddIdTohOCR(&hocr_str,
"block", page_id, bcnt);
1471 AddBoxTohOCR(res_it,
RIL_BLOCK, &hocr_str);
1474 hocr_str +=
"\n <p class='ocr_par'";
1477 hocr_str +=
" dir='rtl'";
1479 AddIdTohOCR(&hocr_str,
"par", page_id, pcnt);
1481 if (paragraph_lang) {
1482 hocr_str +=
" lang='";
1483 hocr_str += paragraph_lang;
1486 AddBoxTohOCR(res_it,
RIL_PARA, &hocr_str);
1489 hocr_str +=
"\n <span class='ocr_line'";
1490 AddIdTohOCR(&hocr_str,
"line", page_id, lcnt);
1495 hocr_str +=
"<span class='ocrx_word'";
1496 AddIdTohOCR(&hocr_str,
"word", page_id, wcnt);
1497 int left, top, right, bottom;
1498 bool bold, italic, underlined, monospace, serif, smallcaps;
1499 int pointsize, font_id;
1500 const char *font_name;
1503 &monospace, &serif, &smallcaps,
1504 &pointsize, &font_id);
1512 hocr_str +=
"; x_font ";
1519 if (lang && (!paragraph_lang || strcmp(lang, paragraph_lang))) {
1520 hocr_str +=
" lang='";
1527 if (!para_is_ltr) hocr_str +=
" dir='ltr'";
1530 if (para_is_ltr) hocr_str +=
" dir='rtl'";
1541 if (bold) hocr_str +=
"<strong>";
1542 if (italic) hocr_str +=
"<em>";
1545 if (grapheme && grapheme[0] != 0) {
1550 if (italic) hocr_str +=
"</em>";
1551 if (bold) hocr_str +=
"</strong>";
1552 hocr_str +=
"</span> ";
1555 if (last_word_in_line) {
1556 hocr_str +=
"\n </span>";
1559 if (last_word_in_para) {
1560 hocr_str +=
"\n </p>\n";
1564 if (last_word_in_block) {
1565 hocr_str +=
" </div>\n";
1569 hocr_str +=
" </div>\n";
1571 char *ret =
new char[hocr_str.
length() + 1];
1572 strcpy(ret, hocr_str.
string());
1586 int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1587 int page_id = page_number + 1;
1591 int page_num = page_id, block_num = 0, par_num = 0, line_num = 0,
1603 tsv_str +=
"\t-1\t\n";
1614 block_num++, par_num = 0, line_num = 0, word_num = 0;
1620 AddBoxToTSV(res_it,
RIL_BLOCK, &tsv_str);
1621 tsv_str +=
"\t-1\t\n";
1624 par_num++, line_num = 0, word_num = 0;
1630 AddBoxToTSV(res_it,
RIL_PARA, &tsv_str);
1631 tsv_str +=
"\t-1\t\n";
1634 line_num++, word_num = 0;
1641 tsv_str +=
"\t-1\t\n";
1645 int left, top, right, bottom;
1673 char* ret =
new char[tsv_str.
length() + 1];
1674 strcpy(ret, tsv_str.
string());
1715 int total_length = blob_count * kBytesPerBoxFileLine + utf8_length +
1717 char* result =
new char[total_length];
1719 int output_length = 0;
1722 int left, top, right, bottom;
1727 for (
int i = 0; text[i] !=
'\0'; ++i) {
1731 snprintf(result + output_length, total_length - output_length,
1732 "%s %d %d %d %d %d\n",
1735 output_length += strlen(result + output_length);
1737 if (output_length + kMaxBytesPerLine > total_length)
1751 0x20ac, 0x201c, 0x201d, 0x2018, 0x2019, 0x2022, 0x2014, 0
1755 0x00a2, 0x0022, 0x0022, 0x0027, 0x0027, 0x00b7, 0x002d, 0
1767 bool tilde_crunch_written =
false;
1768 bool last_char_was_newline =
true;
1769 bool last_char_was_tilde =
false;
1773 char* result =
new char[total_length];
1781 (!tilde_crunch_written ||
1792 last_char_was_tilde =
false;
1794 if (!last_char_was_tilde) {
1796 last_char_was_tilde =
true;
1798 tilde_crunch_written =
true;
1799 last_char_was_newline =
false;
1804 tilde_crunch_written =
false;
1808 int length = lengths.
length();
1812 if (last_char_was_tilde &&
1817 offset = lengths[i++];
1819 if (i < length && wordstr[offset] != 0) {
1820 if (!last_char_was_newline)
1823 last_char_was_newline =
false;
1824 for (; i < length; offset += lengths[i++]) {
1825 if (wordstr[offset] ==
' ' ||
1826 wordstr[offset] == kTesseractReject) {
1828 last_char_was_tilde =
true;
1831 *ptr++ = kUNLVSuspect;
1832 UNICHAR ch(wordstr + offset, lengths[i]);
1834 for (
int j = 0; kUniChs[j] != 0; ++j) {
1835 if (kUniChs[j] == uni_ch) {
1836 uni_ch = kLatinChs[j];
1840 if (uni_ch <= 0xff) {
1841 *ptr++ =
static_cast<char>(uni_ch);
1842 last_char_was_tilde =
false;
1845 last_char_was_tilde =
true;
1854 tilde_crunch_written =
false;
1855 last_char_was_newline =
true;
1856 last_char_was_tilde =
false;
1874 const char** script_name,
1875 float* script_conf) {
1886 if (orient_deg) *orient_deg = orient_id * 90;
1891 *script_name = script;
1907 const char* script_name;
1917 const int kOsdBufsize = 255;
1918 char* osd_buf =
new char[kOsdBufsize];
1919 snprintf(osd_buf, kOsdBufsize,
1921 "Orientation in degrees: %d\n" 1923 "Orientation confidence: %.2f\n" 1925 "Script confidence: %.2f\n",
1926 page_number, orient_deg, rotate, orient_conf, script_name,
1935 if (!conf)
return 0;
1938 while (*pt >= 0) sum += *pt++;
1939 if (pt != conf) sum /= pt - conf;
1954 int* conf =
new int[n_word+1];
1959 int w_conf =
static_cast<int>(100 + 5 * choice->
certainty());
1961 if (w_conf < 0) w_conf = 0;
1962 if (w_conf > 100) w_conf = 100;
1963 conf[n_word++] = w_conf;
1982 bool success =
true;
1986 const std::unique_ptr<const char[]> text(
GetUTF8Text());
1988 tprintf(
"Trying to adapt \"%s\" to \"%s\"\n", text.get(), wordstr);
1993 if (word_res != NULL) {
2001 for (t = 0; text[t] !=
'\0'; ++t) {
2002 if (text[t] ==
'\n' || text[t] ==
' ')
2004 while (wordstr[w] ==
' ') ++w;
2005 if (text[t] != wordstr[w])
2009 if (text[t] !=
'\0' || wordstr[w] !=
'\0') {
2017 if (pr_it.
word() == NULL)
2020 word_res = pr_it.
word();
2117 if (x2 <= x1) x2 = x1 + 1;
2119 *out_slope =
static_cast<float>(y2 - y1) / (x2 - x1);
2120 *out_offset =
static_cast<int>(y1 - *out_slope * x1);
2123 int left, top, right, bottom;
2133 *out_offset += bottom -
MAX(left_y, right_y);
2136 *out_slope = -*out_slope;
2163 for (
int i = 0; i < num_subs; ++i) {
2177 tprintf(
"Please call Init before attempting to set an image.");
2198 if (y_res < kMinCredibleResolution || y_res > kMaxCredibleResolution) {
2201 tprintf(
"Warning. Invalid resolution %d dpi. Using %d instead.\n", y_res,
2202 kMinCredibleResolution);
2227 tprintf(
"Estimated resolution %d out of range! Corrected to %d\n",
2238 tprintf(
"Please call SetImage before attempting recognition.");
2267 osd_tess ==
nullptr) {
2275 nullptr,
nullptr,
false, &mgr) == 0) {
2280 tprintf(
"Warning: Auto orientation and script detection requested," 2281 " but osd language failed to load\n");
2330 int total_length = 2;
2331 int total_blobs = 0;
2337 if (choice != NULL) {
2338 total_blobs += choice->
length() + 2;
2346 if (blob_count != NULL)
2347 *blob_count = total_blobs;
2348 return total_length;
2387 bool** vertical_writing) {
2388 delete[] *block_orientation;
2389 *block_orientation = NULL;
2390 delete[] *vertical_writing;
2391 *vertical_writing = NULL;
2394 block_it.move_to_first();
2396 for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
2397 if (!block_it.data()->poly_block()->IsText()) {
2403 tprintf(
"WARNING: Found no blocks\n");
2406 *block_orientation =
new int[num_blocks];
2407 *vertical_writing =
new bool[num_blocks];
2408 block_it.move_to_first();
2410 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
2411 block_it.forward()) {
2412 if (!block_it.data()->poly_block()->IsText()) {
2415 FCOORD re_rotation = block_it.data()->re_rotation();
2416 float re_theta = re_rotation.
angle();
2417 FCOORD classify_rotation = block_it.data()->classify_rotation();
2418 float classify_theta = classify_rotation.
angle();
2419 double rot_theta = - (re_theta - classify_theta) * 2.0 /
PI;
2420 if (rot_theta < 0) rot_theta += 4;
2421 int num_rotations =
static_cast<int>(rot_theta + 0.5);
2422 (*block_orientation)[i] = num_rotations;
2425 (*vertical_writing)[i] = classify_rotation.
y() != 0.0f;
2455 inT32 xstarts[] = {-32000};
2456 double quad_coeffs[] = {0, 0, baseline};
2461 ascender - (baseline + xheight),
2462 descender - baseline,
2469 int width = pixGetWidth(pix);
2470 int height = pixGetHeight(pix);
2471 BLOCK block(
"a character",
TRUE, 0, 0, 0, 0, width, height);
2478 C_BLOB_IT c_blob_it(list);
2479 if (c_blob_it.empty())
2482 C_OUTLINE_IT ol_it(c_blob_it.data()->out_list());
2483 for (c_blob_it.forward();
2484 !c_blob_it.at_first();
2485 c_blob_it.forward()) {
2486 C_BLOB *c_blob = c_blob_it.data();
2487 ol_it.add_list_after(c_blob->
out_list());
2500 float x_center = (box.
left() + box.
right()) / 2.0f;
2503 tblob->
Normalize(NULL, NULL, NULL, x_center, baseline, scale, scale,
2512 float descender,
float ascender,
2513 bool numeric_mode, Pix* pix) {
2540 float best_rating = -100;
2544 BLOB_CHOICE_LIST choices;
2546 BLOB_CHOICE_IT choice_it;
2547 choice_it.set_to_list(&choices);
2548 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
2549 choice_it.forward()) {
2550 if (choice_it.data()->rating() > best_rating) {
2551 best_rating = choice_it.data()->rating();
2574 pass1_result =
new PAGE_RES(
false, block_list,
2577 return pass1_result;
2581 int debug_level = 0;
2589 result_it, &models);
2601 TESS_CHAR(
float _cost,
const char *repr,
int len = -1) : cost(_cost) {
2602 length = (len == -1 ? strlen(repr) : len);
2603 unicode_repr =
new char[length + 1];
2604 strncpy(unicode_repr, repr, length);
2610 delete [] unicode_repr;
2617 static void add_space(TESS_CHAR_IT* it) {
2619 it->add_after_then_move(t);
2623 static float rating_to_cost(
float rating) {
2624 rating = 100 + rating;
2628 if (rating < 0) rating = 0;
2636 static void extract_result(TESS_CHAR_IT* out,
2640 while (page_res_it.
word() != NULL) {
2648 int n = strlen(len);
2649 for (
int i = 0; i < n; i++) {
2653 out->add_after_then_move(tc);
2674 TESS_CHAR_LIST tess_chars;
2675 TESS_CHAR_IT tess_chars_it(&tess_chars);
2676 extract_result(&tess_chars_it, page_res);
2677 tess_chars_it.move_to_first();
2678 int n = tess_chars.length();
2680 *lengths =
new int[n];
2681 *costs =
new float[n];
2687 for (tess_chars_it.mark_cycle_pt();
2688 !tess_chars_it.cycled_list();
2689 tess_chars_it.forward(), i++) {
2691 text_len += (*lengths)[i] = tc->
length;
2692 (*costs)[i] = tc->
cost;
2696 (*y1)[i] = tc->
box.
top();
2698 char *p = *text =
new char[text_len];
2700 tess_chars_it.move_to_first();
2701 for (tess_chars_it.mark_cycle_pt();
2702 !tess_chars_it.cycled_list();
2703 tess_chars_it.forward()) {
2721 int* feature_outline_index) {
2727 &cn_features, &fx_info, &outline_counts);
2732 *num_features = cn_features.
size();
2733 memcpy(int_features, &cn_features[0], *num_features *
sizeof(cn_features[0]));
2735 if (feature_outline_index != NULL) {
2737 for (
int i = 0; i < outline_counts.
size(); ++i) {
2738 while (f < outline_counts[i])
2739 feature_outline_index[f++] = i;
2747 int left,
int top,
int right,
int bottom) {
2748 TBOX box(left, bottom, right, top);
2749 BLOCK_IT b_it(blocks);
2750 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
2751 BLOCK* block = b_it.data();
2755 for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
2756 ROW* row = r_it.data();
2760 for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
2761 WERD* word = w_it.data();
2772 int num_max_matches,
2775 int* num_matches_returned) {
2776 BLOB_CHOICE_LIST* choices =
new BLOB_CHOICE_LIST;
2778 BLOB_CHOICE_IT choices_it(choices);
2779 int& index = *num_matches_returned;
2781 for (choices_it.mark_cycle_pt();
2782 !choices_it.cycled_list() && index < num_max_matches;
2783 choices_it.forward()) {
2786 ratings[index] = choice->
rating();
2789 *num_matches_returned = index;
2813 for (ptr = text; *ptr; ptr++) {
2815 case '<': ret +=
"<";
break;
2816 case '>': ret +=
">";
break;
2817 case '&': ret +=
"&";
break;
2818 case '"': ret +=
""";
break;
2819 case '\'': ret +=
"'";
break;
2820 default: ret += *ptr;
double min_orientation_margin
STRING * input_file_
Name used by training code.
void set_deadline_msecs(inT32 deadline_msecs)
static TESS_LOCAL int TesseractExtractResult(char **text, int **lengths, float **costs, int **x0, int **y0, int **x1, int **y1, PAGE_RES *page_res)
void DetectParagraphs(int debug_level, GenericVector< RowInfo > *row_infos, GenericVector< PARA *> *row_owners, PARA_LIST *paragraphs, GenericVector< ParagraphModel *> *models)
void ReSegmentByClassification(PAGE_RES *page_res)
PolyBlockType BlockType() const
void add_str_int(const char *str, int number)
FileReader reader_
Reads files from any filesystem.
const char * WordFontAttributes(bool *is_bold, bool *is_italic, bool *is_underlined, bool *is_monospace, bool *is_serif, bool *is_smallcaps, int *pointsize, int *font_id) const
GenericVector< StringParam * > string_params
static ROW * FindRowForBox(BLOCK_LIST *blocks, int left, int top, int right, int bottom)
bool Empty(PageIteratorLevel level) const
C_OUTLINE_LIST * out_list()
int IsValidWord(const char *word)
TBOX intersection(const TBOX &box) const
double matcher_good_threshold
const UNICHARSET & getUnicharset() const
#define PERF_COUNT_START(FUNCT_NAME)
virtual bool Next(PageIteratorLevel level)
const int kMaxCredibleResolution
bool textord_equation_detect
TESS_CHAR(float _cost, const char *repr, int len=-1)
virtual bool IsAtBeginningOf(PageIteratorLevel level) const
const TBOX & BlobBox(int index) const
void ReadConfigFile(const char *filename)
const char * GetUnichar(int unichar_id)
TESS_LOCAL LTRResultIterator * GetLTRIterator()
char * TesseractRect(const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height)
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
void(Wordrec::* FillLatticeFunc)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Pix ** mutable_pix_binary()
int num_sub_langs() const
bool classify_bln_numeric_mode
TESS_API int get_best_script(int orientation_id) const
static const char * Version()
bool contains_unichar(const char *const unichar_repr) const
static ROW * MakeTessOCRRow(float baseline, float xheight, float descender, float ascender)
void signal_exit(int signal_code)
WERD_CHOICE * best_choice
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
void recog_training_segmented(const STRING &fname, PAGE_RES *page_res, volatile ETEXT_DESC *monitor, FILE *output_file)
const STRING & unichar_lengths() const
int GetSourceYResolution()
int GetSourceYResolution() const
virtual Pix * GetPixRectThresholds()
const int kMaxBytesPerLine
virtual char * GetUTF8Text(PageIteratorLevel level) const
int RecognizeForChopTest(ETEXT_DESC *monitor)
bool IsEmpty() const
Return true if no image has been set.
STRING * output_file_
Name used by debug code.
void ApplyBoxTraining(const STRING &fontname, PAGE_RES *page_res)
BLOCK_LIST * block_list_
The page layout.
char * GetOsdText(int page_number)
char * GetHOCRText(ETEXT_DESC *monitor, int page_number)
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
Returns false on error.
bool LoadMemBuffer(const char *name, const char *data, int size)
char * GetTSVText(int page_number)
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params)
static TBLOB * MakeTBLOB(Pix *pix)
bool GetTextDirection(int *out_offset, float *out_slope)
void SetProbabilityInContextFunc(ProbabilityInContextFunc f)
float Confidence(PageIteratorLevel level) const
bool PSM_OSD_ENABLED(int pageseg_mode)
bool ProcessPagesInternal(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Tesseract * get_sub_lang(int index) const
void SetFillLatticeFunc(FillLatticeFunc f)
float angle() const
find angle
virtual Pix * GetPixRectGrey()
int GetThresholdedImageScaleFactor() const
const char * get_script_from_script_id(int id) const
void ResetAdaptiveClassifier()
ImageThresholder * thresholder_
Image thresholding module.
static size_t getOpenCLDevice(void **device)
void SetEquationDetect(EquationDetect *detector)
void set_source_resolution(int ppi)
bool SetVariable(const char *name, const char *value)
void SetRectangle(int left, int top, int width, int height)
void SavePixForCrash(int resolution, Pix *pix)
Assume a single uniform block of text. (Default.)
void InitForAnalysePage()
Pix * GetBinaryImage(PageIteratorLevel level) const
void BestChoiceToCorrectText()
const char * string() const
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
void set_text(const char *new_text)
void ReadDebugConfigFile(const char *filename)
int tessedit_pageseg_mode
PAGE_RES * page_res_
The page-level data.
const int kBlnBaselineOffset
void * cancel_this
called whenever progress increases
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
void RunAdaptiveClassifier(TBLOB *blob, int num_max_matches, int *unichar_ids, float *ratings, int *num_matches_returned)
PageIterator * AnalyseLayout()
void SetRectangle(int left, int top, int width, int height)
void set_unlv_suspects(WERD_RES *word)
TESS_LOCAL PAGE_RES * RecognitionPass1(BLOCK_LIST *block_list)
FILE * init_recog_training(const STRING &fname)
#define BOOL_VAR(name, val, comment)
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
Boxa * GetConnectedComponents(Pixa **cc)
int IntCastRounded(double x)
TESS_LOCAL bool InternalSetImage()
bool tessedit_write_images
TESS_LOCAL void DetectParagraphs(bool after_text_recognition)
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
void ExtractFontName(const STRING &filename, STRING *fontname)
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
TBOX bounding_box() const
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
tesseract::BoxWord * box_word
Tesseract * tesseract_
The underlying data object.
bool tessedit_make_boxes_from_boxes
bool DetectOrientationScript(int *orient_deg, float *orient_conf, const char **script_name, float *script_conf)
void TidyUp(PAGE_RES *page_res)
BOOL8 flag(WERD_FLAGS mask) const
const char * id_to_unichar(UNICHAR_ID id) const
void PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, OSResults *osr)
Pix * pix_original() const
void ResetDocumentDictionary()
MutableIterator * GetMutableIterator()
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
bool GetBoolVariable(const char *name, bool *value) const
void SetSourceResolution(int ppi)
TruthCallback * truth_cb_
const int kBytesPer64BitNumber
WERD_RES * restart_page()
const char * GetInitLanguagesAsString() const
bool IsValidCharacter(const char *utf8_character)
double(Dict::* probability_in_context_)(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
Probability in context function used by the ngram permuter.
const int kBytesPerBoxFileLine
TESS_LOCAL void AdaptToCharacter(const char *unichar_repr, int length, float baseline, float xheight, float descender, float ascender)
int Recognize(ETEXT_DESC *monitor)
const Dawg * GetDawg(int index) const
Return i-th dawg pointer recorded in the dawgs_ vector.
const char * GetStringVariable(const char *name) const
static void NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode)
void chomp_string(char *str)
int NumDawgs() const
Return the number of dawgs in the dawgs_ vector.
void GetBlockTextOrientations(int **block_orientation, bool **vertical_writing)
TBLOB * make_tesseract_blob(float baseline, float xheight, float descender, float ascender, bool numeric_mode, Pix *pix)
BLOCK_LIST * FindLinesCreateBlockList()
EquationDetect * equ_detect_
The equation detector.
void GetFeaturesForBlob(TBLOB *blob, INT_FEATURE_STRUCT *int_features, int *num_features, int *feature_outline_index)
Automatic page segmentation, but no OSD, or OCR.
double(Dict::* ProbabilityInContextFunc)(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
TESS_LOCAL int TextLength(int *blob_count)
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Pix * GetImage(PageIteratorLevel level, int padding, Pix *original_img, int *left, int *top) const
int GetScaledEstimatedResolution() const
C_BLOB_LIST * blob_list()
get blobs
void RowAttributes(float *row_height, float *descenders, float *ascenders) const
bool tessedit_train_from_boxes
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
virtual TESS_LOCAL bool Threshold(Pix **pix)
void add_str_double(const char *str, double number)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
static void DeleteBlockList(BLOCK_LIST *block_list)
void SetInputImage(Pix *pix)
Tesseract * tesseract() const
TESS_LOCAL int FindLines()
int init_tesseract(const char *arg0, const char *textbase, const char *language, OcrEngineMode oem, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_init_params, TessdataManager *mgr)
void AdaptToChar(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, FLOAT32 Threshold, ADAPT_TEMPLATES adaptive_templates)
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
Pix * GetThresholdedImage()
ResultIterator * GetIterator()
bool SetDebugVariable(const char *name, const char *value)
void Normalize(const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift, bool inverse, Pix *pix)
Boxa * GetWords(Pixa **pixa)
OcrEngineMode oem() const
bool GetDoubleVariable(const char *name, double *value) const
bool DetectOS(OSResults *)
tesseract::ParamsVectors * GlobalParams()
ADAPT_TEMPLATES AdaptedTemplates
int orientation_and_script_detection(STRING &filename, OSResults *osr, tesseract::Tesseract *tess)
bool major_overlap(const TBOX &box) const
CANCEL_FUNC cancel
for errcode use
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
const Dawg * GetDawg(int i) const
TBOX bounding_box() const
StrongScriptDirection WordDirection() const
STRING * language_
Last initialized language.
static ResultIterator * StartOfParagraph(const LTRResultIterator &resit)
int * AllWordConfidences()
bool IsBinary() const
Returns true if the source image is binary.
bool Baseline(PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
CRUNCH_MODE unlv_crunch_mode
STRING HOcrEscape(const char *text)
void SetDictFunc(DictFunc f)
bool tessedit_ambigs_training
const char * kOldVarsFile
void SetSourceYResolution(int ppi)
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
void SetBlackAndWhitelist()
virtual bool Next(PageIteratorLevel level)
Tesseract * osd_tesseract_
For orientation & script detection.
void DumpPGM(const char *filename)
static void ExtractFeatures(const TBLOB &blob, bool nonlinear_norm, GenericVector< INT_FEATURE_STRUCT > *bl_features, GenericVector< INT_FEATURE_STRUCT > *cn_features, INT_FX_RESULT_STRUCT *results, GenericVector< int > *outline_cn_counts)
int(Dict::* DictFunc)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
#define INVALID_HANDLE_VALUE
const STRING & unichar_string() const
const int kNumbersPerBlob
void(Wordrec::* fill_lattice_)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
void read_config_file(const char *filename, SetParamConstraint constraint)
Boxa * GetTextlines(const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
void SetPageSegMode(PageSegMode mode)
STRING * datapath_
Current location of tessdata.
void SetOutputName(const char *name)
const char * GetInputName()
static void ClearPersistentCache()
char * GetBoxText(int page_number)
#define MAX_NUM_INT_FEATURES
GenericVector< IntParam * > int_params
void set_pix_thresholds(Pix *thresholds)
static void ResetToDefaults(ParamsVectors *member_params)
PageSegMode GetPageSegMode() const
void GetLoadedLanguagesAsVector(GenericVector< STRING > *langs) const
bool AddImage(TessBaseAPI *api)
const char * c_str() const
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
int OrientationIdToValue(const int &id)
void PrintVariables(FILE *fp) const
void TrainLineRecognizer(const STRING &input_imagename, const STRING &output_basename, BLOCK_LIST *block_list)
#define PERF_COUNT_SUB(SUB)
const char * GetDatapath()
UNICHAR_ID unichar_id() const
bool PTIsTextType(PolyBlockType type)
void InitAdaptiveClassifier(TessdataManager *mgr)
int GetScaleFactor() const
bool WriteTRFile(const STRING &filename)
PAGE_RES * SetupApplyBoxes(const GenericVector< TBOX > &boxes, BLOCK_LIST *block_list)
const char kTesseractReject
TESS_LOCAL PAGE_RES * RecognitionPass2(BLOCK_LIST *block_list, PAGE_RES *pass1_result)
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
void pgeditor_main(int width, int height, PAGE_RES *page_res)
bool ProcessPage(Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
bool ParagraphIsLtr() const
PAGE_RES * ApplyBoxes(const STRING &fname, bool find_segmentation, BLOCK_LIST *block_list)
bool GetIntVariable(const char *name, int *value) const
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
void set_min_orientation_margin(double margin)
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
static DawgCache * GlobalDawgCache()
const int kBytesPerNumber
bool recognition_done_
page_res_ contains recognition data.
void set_pix_grey(Pix *grey_pix)
GenericVector< DoubleParam * > double_params
bool GetVariableAsString(const char *name, STRING *val)
bool BeginDocument(const char *title)
const int kMinCredibleResolution
void extract_edges(Pix *pix, BLOCK *block)
float base_line(float xpos) const
const char * string() const
void LearnWord(const char *fontname, WERD_RES *word)
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
void delete_data_pointers()
bool tessedit_train_line_recognizer
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
TBOX bounding_box() const
void MaximallyChopWord(const GenericVector< TBOX > &boxes, BLOCK *block, ROW *row, WERD_RES *word_res)
Orientation and script detection only.
bool tessedit_resegment_from_boxes
void CorrectClassifyWords(PAGE_RES *page_res)
void SetInputName(const char *name)
#define TESSERACT_VERSION_STR
bool recog_all_words(PAGE_RES *page_res, ETEXT_DESC *monitor, const TBOX *target_word_box, const char *word_config, int dopasses)
int InitLangMod(const char *datapath, const char *language)
GenericVector< ParagraphModel * > * paragraph_models_
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
ROW_LIST * row_list()
get rows
void ClearAdaptiveClassifier()
Boxa * GetStrips(Pixa **pixa, int **blockids)
Boxa * GetRegions(Pixa **pixa)
int(Dict::* letter_is_okay_)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
void set_pix_original(Pix *original_pix)
const char * WordRecognitionLanguage() const
bool tessedit_resegment_from_line_boxes
void split(const char c, GenericVector< STRING > *splited)
BLOCK_RES * block() const
virtual void Run(A1, A2, A3, A4)=0
WERD_CHOICE * prev_word_best_choice_
static void CatchSignals()
bool interactive_display_mode
bool AdaptToWordStr(PageSegMode mode, const char *wordstr)
int init_tesseract_lm(const char *arg0, const char *textbase, const char *language, TessdataManager *mgr)
int GetScaledYResolution() const
char * GetUTF8Text(PageIteratorLevel level) const
GenericVector< BoolParam * > bool_params
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const