tesseract
5.0.0-alpha-619-ge9db
|
Go to the documentation of this file.
19 #define _USE_MATH_DEFINES // for M_PI
23 #include "config_auto.h"
37 #include <sys/types.h>
53 #include <curl/curl.h>
55 #include "allheaders.h"
56 #ifndef DISABLED_LEGACY_ENGINE
60 #include "config_auto.h"
71 #ifndef DISABLED_LEGACY_ENGINE
77 #if defined(USE_OPENCL)
98 static BOOL_VAR(stream_filelist,
false,
"Stream a filelist from stdin");
99 static STRING_VAR(document_title,
"",
"Title of output document (used for hOCR and PDF output)");
115 static const char* kInputFile =
"noname.tif";
119 static const char* kOldVarsFile =
"failed_vars.txt";
125 static void addAvailableLanguages(
const STRING &datadir,
const STRING &base,
128 const STRING base2 = (base.
c_str()[0] ==
'\0') ? base : base +
"/";
129 const size_t extlen =
sizeof(kTrainedDataSuffix);
131 WIN32_FIND_DATA data;
132 HANDLE handle = FindFirstFile((datadir + base2 +
"*").c_str(), &data);
133 if (handle != INVALID_HANDLE_VALUE) {
136 char *name = data.cFileName;
138 if (name[0] !=
'.') {
139 if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
140 FILE_ATTRIBUTE_DIRECTORY) {
141 addAvailableLanguages(datadir, base2 + name, langs);
143 size_t len = strlen(name);
144 if (len > extlen && name[len - extlen] ==
'.' &&
145 strcmp(&name[len - extlen + 1], kTrainedDataSuffix) == 0) {
146 name[len - extlen] =
'\0';
151 result = FindNextFile(handle, &data);
156 DIR* dir = opendir((datadir + base).c_str());
157 if (dir !=
nullptr) {
159 while ((de = readdir(dir))) {
160 char *name = de->d_name;
162 if (name[0] !=
'.') {
164 if (stat((datadir + base2 + name).c_str(), &st) == 0 &&
165 (st.st_mode & S_IFDIR) == S_IFDIR) {
166 addAvailableLanguages(datadir, base2 + name, langs);
168 size_t len = strlen(name);
169 if (len > extlen && name[len - extlen] ==
'.' &&
170 strcmp(&name[len - extlen + 1], kTrainedDataSuffix) == 0) {
171 name[len - extlen] =
'\0';
183 static int CompareSTRING(
const void* p1,
const void* p2) {
184 const auto* s1 = static_cast<const STRING*>(p1);
185 const auto* s2 = static_cast<const STRING*>(p2);
186 return strcmp(s1->c_str(), s2->c_str());
190 : tesseract_(nullptr),
191 osd_tesseract_(nullptr),
192 equ_detect_(nullptr),
197 thresholder_(nullptr),
198 paragraph_models_(nullptr),
199 block_list_(nullptr),
201 input_file_(nullptr),
202 output_file_(nullptr),
206 recognition_done_(false),
222 std::locale::global(std::locale(
""));
234 return PACKAGE_VERSION;
246 ds_device device = OpenclDevice::getDeviceSelection();
247 if (device.type == DS_DEVICE_OPENCL_DEVICE) {
248 *data =
new cl_device_id;
249 memcpy(*data, &device.oclDeviceID,
sizeof(cl_device_id));
250 return sizeof(cl_device_id);
290 auto *p = ParamUtils::FindParam<IntParam>(
292 if (p ==
nullptr)
return false;
293 *value = (int32_t)(*p);
298 auto *p = ParamUtils::FindParam<BoolParam>(
300 if (p ==
nullptr)
return false;
306 auto *p = ParamUtils::FindParam<StringParam>(
308 return (p !=
nullptr) ? p->c_str() :
nullptr;
312 auto *p = ParamUtils::FindParam<DoubleParam>(
314 if (p ==
nullptr)
return false;
315 *value = (double)(*p);
341 bool set_only_non_debug_params) {
342 return Init(datapath, 0, language,
oem, configs, configs_size, vars_vec,
343 vars_values, set_only_non_debug_params,
nullptr);
353 bool set_only_non_debug_params,
FileReader reader) {
355 if (language ==
nullptr) language =
"eng";
356 STRING datapath = data_size == 0 ? data : language;
373 bool reset_classifier =
true;
375 reset_classifier =
false;
377 if (reader !=
nullptr)
reader_ = reader;
379 if (data_size != 0) {
385 language,
oem, configs, configs_size, vars_vec, vars_values,
386 set_only_non_debug_params, &mgr) != 0) {
406 #ifndef DISABLED_LEGACY_ENGINE
408 if (reset_classifier) {
411 #endif // ndef DISABLED_LEGACY_ENGINE
439 for (
int i = 0; i < num_subs; ++i)
452 langs->
sort(CompareSTRING);
457 #ifndef DISABLED_LEGACY_ENGINE
472 #endif // ndef DISABLED_LEGACY_ENGINE
481 #ifndef DISABLED_LEGACY_ENGINE
516 return static_cast<PageSegMode>(
537 int width,
int height) {
543 int bits_per_pixel = bytes_per_pixel == 0 ? 1 : bytes_per_pixel * 8;
544 SetImage(imagedata, bytes_per_line * 8 / bits_per_pixel, height + top,
545 bytes_per_pixel, bytes_per_line);
551 #ifndef DISABLED_LEGACY_ENGINE
562 #endif // ndef DISABLED_LEGACY_ENGINE
572 int width,
int height,
573 int bytes_per_pixel,
int bytes_per_line) {
576 bytes_per_pixel, bytes_per_line);
585 tprintf(
"Please call SetImage before SetSourceResolution.\n");
598 if (pixGetSpp(pix) == 4 && pixGetInputFormat(pix) == IFF_PNG) {
600 Pix* p1 = pixRemoveAlpha(pix);
602 (void)pixCopy(pix, p1);
653 Pixa** pixa,
int** blockids,
int** paraids) {
655 pixa, blockids, paraids);
698 bool text_only,
bool raw_image,
699 const int raw_padding,
700 Pixa** pixa,
int** blockids,
703 if (page_it ==
nullptr)
705 if (page_it ==
nullptr)
709 int component_count = 0;
710 int left, top, right, bottom;
716 &left, &top, &right, &bottom) &&
719 }
while (page_it->
Next(level));
727 }
while (page_it->
Next(level));
730 Boxa* boxa = boxaCreate(component_count);
732 *pixa = pixaCreate(component_count);
733 if (blockids !=
nullptr)
734 *blockids =
new int[component_count];
735 if (paraids !=
nullptr)
736 *paraids =
new int[component_count];
740 int component_index = 0;
743 bool got_bounding_box;
746 page_it->
BoundingBox(level, raw_padding, &left, &top, &right, &bottom);
751 if (got_bounding_box &&
753 Box* lbox = boxCreate(left, top, right - left, bottom - top);
754 boxaAddBox(boxa, lbox, L_INSERT);
755 if (pixa !=
nullptr) {
763 pixaAddPix(*pixa, pix, L_INSERT);
764 pixaAddBox(*pixa, lbox, L_CLONE);
766 if (paraids !=
nullptr) {
767 (*paraids)[component_index] = paraid;
771 if (blockids !=
nullptr) {
772 (*blockids)[component_index] = blockid;
780 }
while (page_it->
Next(level));
841 #ifndef DISABLED_LEGACY_ENGINE
847 #endif // ndef DISABLED_LEGACY_ENGINE
864 #ifndef DISABLED_LEGACY_ENGINE
869 #endif // ndef DISABLED_LEGACY_ENGINE
884 #ifndef GRAPHICS_DISABLED
886 #endif // GRAPHICS_DISABLED
892 #ifndef DISABLED_LEGACY_ENGINE
902 fclose(training_output_file);
903 #endif // ndef DISABLED_LEGACY_ENGINE
906 bool wait_for_text =
true;
918 #ifndef DISABLED_LEGACY_ENGINE
924 tprintf(
"Please call SetImage before attempting recognition.\n");
941 while (page_res_it.
word() !=
nullptr) {
945 page_res_it.
row()->
row, word_res);
950 #endif // ndef DISABLED_LEGACY_ENGINE
975 bool TessBaseAPI::ProcessPagesFileList(FILE *flist,
977 const char* retry_config,
978 int timeout_millisec,
980 int tessedit_page_number) {
981 if (!flist && !buf)
return false;
982 int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
987 buf->
split(
'\n', &lines);
988 if (lines.
empty())
return false;
992 for (
int i = 0; i < page; i++) {
994 if (fgets(pagename,
sizeof(pagename), flist) ==
nullptr)
break;
999 if (renderer && !renderer->
BeginDocument(document_title.c_str())) {
1006 if (fgets(pagename,
sizeof(pagename), flist) ==
nullptr)
break;
1008 if (page >= lines.
size())
break;
1009 snprintf(pagename,
sizeof(pagename),
"%s", lines[page].c_str());
1012 Pix *pix = pixRead(pagename);
1013 if (pix ==
nullptr) {
1014 tprintf(
"Image file %s cannot be read!\n", pagename);
1017 tprintf(
"Page %d : %s\n", page, pagename);
1018 bool r =
ProcessPage(pix, page, pagename, retry_config,
1019 timeout_millisec, renderer);
1021 if (!r)
return false;
1022 if (tessedit_page_number >= 0)
break;
1033 bool TessBaseAPI::ProcessPagesMultipageTiff(
const l_uint8 *data,
1035 const char* filename,
1036 const char* retry_config,
1037 int timeout_millisec,
1039 int tessedit_page_number) {
1040 #ifndef ANDROID_BUILD
1042 int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
1045 if (tessedit_page_number >= 0) {
1046 page = tessedit_page_number;
1047 pix = (data) ? pixReadMemTiff(data, size, page)
1048 : pixReadTiff(filename, page);
1050 pix = (data) ? pixReadMemFromMultipageTiff(data, size, &offset)
1051 : pixReadFromMultipageTiff(filename, &offset);
1053 if (pix ==
nullptr)
break;
1054 tprintf(
"Page %d\n", page + 1);
1058 bool r =
ProcessPage(pix, page, filename, retry_config,
1059 timeout_millisec, renderer);
1061 if (!r)
return false;
1062 if (tessedit_page_number >= 0)
break;
1074 int timeout_millisec,
1078 #ifndef DISABLED_LEGACY_ENGINE
1086 #endif // ndef DISABLED_LEGACY_ENGINE
1091 WriteMemoryCallback(
void *contents,
size_t size,
size_t nmemb,
void *userp)
1093 size = size * nmemb;
1094 std::string* buf = reinterpret_cast<std::string*>(userp);
1095 buf->append(reinterpret_cast<const char*>(contents), size);
1111 const char* retry_config,
1112 int timeout_millisec,
1114 bool stdInput = !strcmp(filename,
"stdin") || !strcmp(filename,
"-");
1117 if (_setmode(_fileno(stdin), _O_BINARY) == -1)
1118 tprintf(
"ERROR: cin to binary: %s", strerror(errno));
1122 if (stream_filelist) {
1123 return ProcessPagesFileList(stdin,
nullptr, retry_config,
1124 timeout_millisec, renderer,
1132 const l_uint8 *data =
nullptr;
1134 buf.assign((std::istreambuf_iterator<char>(std::cin)),
1135 (std::istreambuf_iterator<char>()));
1136 data = reinterpret_cast<const l_uint8 *>(buf.data());
1137 }
else if (strncmp(filename,
"http:", 5) == 0 ||
1138 strncmp(filename,
"https:", 6) == 0 ) {
1141 CURL* curl = curl_easy_init();
1142 if (curl ==
nullptr) {
1143 fprintf(stderr,
"Error, curl_easy_init failed\n");
1147 curlcode = curl_easy_setopt(curl, CURLOPT_URL, filename);
1149 curlcode = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
1151 curlcode = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
1153 curlcode = curl_easy_perform(curl);
1155 curl_easy_cleanup(curl);
1156 data = reinterpret_cast<const l_uint8 *>(buf.data());
1159 fprintf(stderr,
"Error, this tesseract has no URL support\n");
1164 if (FILE*
file = fopen(filename,
"rb")) {
1167 fprintf(stderr,
"Error, cannot read input file %s: %s\n",
1168 filename, strerror(errno));
1175 int r = (data !=
nullptr) ?
1176 findFileFormatBuffer(data, &format) :
1177 findFileFormat(filename, &format);
1180 if (r != 0 || format == IFF_UNKNOWN) {
1182 if (data !=
nullptr) {
1185 std::ifstream t(filename);
1186 std::string u((std::istreambuf_iterator<char>(t)),
1187 std::istreambuf_iterator<char>());
1190 return ProcessPagesFileList(
nullptr, &s, retry_config,
1191 timeout_millisec, renderer,
1196 bool tiff = (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
1197 format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
1198 format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
1199 #if LIBLEPT_MAJOR_VERSION > 1 || LIBLEPT_MINOR_VERSION > 76
1200 format == IFF_TIFF_JPEG ||
1202 format == IFF_TIFF_ZIP);
1207 pix = (data !=
nullptr) ? pixReadMem(data, buf.size()) : pixRead(filename);
1208 if (pix ==
nullptr) {
1214 if (renderer && !renderer->
BeginDocument(document_title.c_str())) {
1221 ProcessPagesMultipageTiff(data, buf.size(), filename, retry_config,
1222 timeout_millisec, renderer,
1225 timeout_millisec, renderer);
1231 if (!r || (renderer && !renderer->EndDocument())) {
1238 const char* retry_config,
int timeout_millisec,
1242 bool failed =
false;
1248 if (it ==
nullptr) {
1255 }
else if (timeout_millisec > 0) {
1258 monitor.
cancel =
nullptr;
1270 #ifndef ANDROID_BUILD
1272 pixWrite(
"tessinput.tif", page_pix, IFF_TIFF_G4);
1273 #endif // ANDROID_BUILD
1276 if (failed && retry_config !=
nullptr && retry_config[0] !=
'\0') {
1278 FILE* fp = fopen(kOldVarsFile,
"wb");
1279 if (fp ==
nullptr) {
1280 tprintf(
"Error, failed to open file \"%s\"\n", kOldVarsFile);
1293 if (renderer && !failed) {
1294 failed = !renderer->
AddImage(
this);
1357 text += para_text.get();
1359 char* result =
new char[text.
length() + 1];
1367 int left, top, right, bottom;
1368 it->
BoundingBox(level, &left, &top, &right, &bottom);
1384 int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1385 int page_id = page_number + 1;
1389 int page_num = page_id;
1404 tsv_str +=
"\t-1\t\n";
1424 AddBoxToTSV(res_it,
RIL_BLOCK, &tsv_str);
1425 tsv_str +=
"\t-1\t\n";
1436 AddBoxToTSV(res_it,
RIL_PARA, &tsv_str);
1437 tsv_str +=
"\t-1\t\n";
1448 tsv_str +=
"\t-1\t\n";
1452 int left, top, right, bottom;
1481 char* ret =
new char[tsv_str.
length() + 1];
1482 strcpy(ret, tsv_str.
c_str());
1525 char* result =
new char[total_length];
1527 int output_length = 0;
1530 int left, top, right, bottom;
1532 const std::unique_ptr<
char[]> text(
1536 for (
int i = 0; text[i] !=
'\0'; ++i) {
1540 snprintf(result + output_length, total_length - output_length,
1541 "%s %d %d %d %d %d\n", text.get(), left,
image_height_ - bottom,
1543 output_length += strlen(result + output_length);
1559 0x20ac, 0x201c, 0x201d, 0x2018, 0x2019, 0x2022, 0x2014, 0
1563 0x00a2, 0x0022, 0x0022, 0x0027, 0x0027, 0x00b7, 0x002d, 0
1575 bool tilde_crunch_written =
false;
1576 bool last_char_was_newline =
true;
1577 bool last_char_was_tilde =
false;
1581 char* result =
new char[total_length];
1589 (!tilde_crunch_written ||
1600 last_char_was_tilde =
false;
1602 if (!last_char_was_tilde) {
1604 last_char_was_tilde =
true;
1606 tilde_crunch_written =
true;
1607 last_char_was_newline =
false;
1612 tilde_crunch_written =
false;
1616 int length = lengths.
length();
1620 if (last_char_was_tilde &&
1621 word->
word->
space() == 0 && wordstr[offset] ==
' ') {
1625 offset = lengths[i++];
1627 if (i < length && wordstr[offset] != 0) {
1628 if (!last_char_was_newline)
1631 last_char_was_newline =
false;
1632 for (; i < length; offset += lengths[i++]) {
1633 if (wordstr[offset] ==
' ' ||
1636 last_char_was_tilde =
true;
1640 UNICHAR ch(wordstr + offset, lengths[i]);
1642 for (
int j = 0;
kUniChs[j] != 0; ++j) {
1648 if (uni_ch <= 0xff) {
1649 *ptr++ = static_cast<char>(uni_ch);
1650 last_char_was_tilde =
false;
1653 last_char_was_tilde =
true;
1662 tilde_crunch_written =
false;
1663 last_char_was_newline =
true;
1664 last_char_was_tilde =
false;
1672 #ifndef DISABLED_LEGACY_ENGINE
1684 const char** script_name,
1685 float* script_conf) {
1696 if (orient_deg) *orient_deg = orient_id * 90;
1701 *script_name = script;
1717 const char* script_name;
1727 std::stringstream stream;
1729 stream.imbue(std::locale::classic());
1731 stream.precision(2);
1734 <<
"Page number: " << page_number <<
"\n"
1735 <<
"Orientation in degrees: " << orient_deg <<
"\n"
1736 <<
"Rotate: " << rotate <<
"\n"
1737 <<
"Orientation confidence: " << orient_conf <<
"\n"
1738 <<
"Script: " << script_name <<
"\n"
1739 <<
"Script confidence: " << script_conf <<
"\n";
1741 char* result =
new char[text.length() + 1];
1742 strcpy(result, text.c_str());
1746 #endif // ndef DISABLED_LEGACY_ENGINE
1751 if (!conf)
return 0;
1754 while (*pt >= 0) sum += *pt++;
1755 if (pt != conf) sum /= pt - conf;
1770 int* conf =
new int[n_word+1];
1775 int w_conf = static_cast<int>(100 + 5 * choice->
certainty());
1777 if (w_conf < 0) w_conf = 0;
1778 if (w_conf > 100) w_conf = 100;
1779 conf[n_word++] = w_conf;
1785 #ifndef DISABLED_LEGACY_ENGINE
1799 bool success =
true;
1803 const std::unique_ptr<const char[]> text(
GetUTF8Text());
1805 tprintf(
"Trying to adapt \"%s\" to \"%s\"\n", text.get(), wordstr);
1807 if (text !=
nullptr) {
1810 if (word_res !=
nullptr) {
1815 for (t = 0; text[t] !=
'\0'; ++t) {
1816 if (text[t] ==
'\n' || text[t] ==
' ')
1818 while (wordstr[w] ==
' ') ++w;
1819 if (text[t] != wordstr[w])
1823 if (text[t] !=
'\0' || wordstr[w] !=
'\0') {
1831 if (pr_it.
word() ==
nullptr)
1834 word_res = pr_it.
word();
1851 #endif // ndef DISABLED_LEGACY_ENGINE
1928 if (it ==
nullptr) {
1934 if (x2 <= x1) x2 = x1 + 1;
1936 *out_slope = static_cast<float>(y2 - y1) / (x2 - x1);
1937 *out_offset = static_cast<int>(y1 - *out_slope * x1);
1940 int left, top, right, bottom;
1950 *out_offset += bottom - std::max(left_y, right_y);
1953 *out_slope = -*out_slope;
1980 for (
int i = 0; i < num_subs; ++i) {
1986 #ifndef DISABLED_LEGACY_ENGINE
1991 #endif // ndef DISABLED_LEGACY_ENGINE
1996 tprintf(
"Please call Init before attempting to set an image.\n");
2013 if (*pix !=
nullptr)
2021 tprintf(
"Warning: User defined image dpi is outside of expected range "
2030 tprintf(
"Warning: Invalid resolution %d dpi. Using %d instead.\n",
2035 static_cast<PageSegMode>(
2056 tprintf(
"Estimated internal resolution %d out of range! "
2057 "Corrected to %d.\n",
2067 tprintf(
"Please call SetImage before attempting recognition.\n");
2077 #ifndef DISABLED_LEGACY_ENGINE
2088 #ifndef DISABLED_LEGACY_ENGINE
2094 tprintf(
"Warning: Could not set equation detector\n");
2099 #endif // ndef DISABLED_LEGACY_ENGINE
2104 osd_tess ==
nullptr) {
2111 tprintf(
"Warning: Auto orientation and script detection requested,"
2112 " but data path is undefined\n");
2117 nullptr, 0,
nullptr,
nullptr,
2118 false, &mgr) == 0) {
2123 tprintf(
"Warning: Auto orientation and script detection requested,"
2124 " but osd language failed to load\n");
2171 int total_length = 2;
2172 int total_blobs = 0;
2178 if (choice !=
nullptr) {
2179 total_blobs += choice->
length() + 2;
2187 if (blob_count !=
nullptr)
2188 *blob_count = total_blobs;
2189 return total_length;
2192 #ifndef DISABLED_LEGACY_ENGINE
2210 #endif // ndef DISABLED_LEGACY_ENGINE
2231 bool** vertical_writing) {
2232 delete[] *block_orientation;
2233 *block_orientation =
nullptr;
2234 delete[] *vertical_writing;
2235 *vertical_writing =
nullptr;
2238 block_it.move_to_first();
2240 for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
2241 if (!block_it.data()->pdblk.poly_block()->IsText()) {
2247 tprintf(
"WARNING: Found no blocks\n");
2250 *block_orientation =
new int[num_blocks];
2251 *vertical_writing =
new bool[num_blocks];
2252 block_it.move_to_first();
2254 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
2255 block_it.forward()) {
2256 if (!block_it.data()->pdblk.poly_block()->IsText()) {
2259 FCOORD re_rotation = block_it.data()->re_rotation();
2260 float re_theta = re_rotation.
angle();
2261 FCOORD classify_rotation = block_it.data()->classify_rotation();
2262 float classify_theta = classify_rotation.
angle();
2263 double rot_theta = - (re_theta - classify_theta) * 2.0 / M_PI;
2264 if (rot_theta < 0) rot_theta += 4;
2265 int num_rotations = static_cast<int>(rot_theta + 0.5);
2266 (*block_orientation)[i] = num_rotations;
2269 (*vertical_writing)[i] = classify_rotation.
y() != 0.0f;
2276 int debug_level = 0;
2284 result_it, &models);
2310 for (ptr = text; *ptr; ptr++) {
2312 case '<': ret +=
"<";
break;
2313 case '>': ret +=
">";
break;
2314 case '&': ret +=
"&";
break;
2315 case '"': ret +=
""";
break;
2316 case '\'': ret +=
"'";
break;
2317 default: ret += *ptr;
2324 #ifndef DISABLED_LEGACY_ENGINE
2352 int32_t xstarts[] = {-32000};
2353 double quad_coeffs[] = {0, 0,
baseline};
2366 int width = pixGetWidth(pix);
2367 int height = pixGetHeight(pix);
2368 BLOCK block(
"a character",
true, 0, 0, 0, 0, width, height);
2375 C_BLOB_IT c_blob_it(list);
2376 if (c_blob_it.empty())
2379 C_OUTLINE_IT ol_it(c_blob_it.data()->out_list());
2380 for (c_blob_it.forward();
2381 !c_blob_it.at_first();
2382 c_blob_it.forward()) {
2383 C_BLOB *c_blob = c_blob_it.data();
2384 ol_it.add_list_after(c_blob->
out_list());
2397 float x_center = (box.
left() + box.
right()) / 2.0f;
2408 static TBLOB *make_tesseract_blob(
float baseline,
float xheight,
2409 float descender,
float ascender,
2410 bool numeric_mode, Pix* pix) {
2433 TBLOB *blob = make_tesseract_blob(
baseline, xheight, descender, ascender,
2437 float best_rating = -100;
2441 BLOB_CHOICE_LIST choices;
2443 BLOB_CHOICE_IT choice_it;
2444 choice_it.set_to_list(&choices);
2445 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
2446 choice_it.forward()) {
2447 if (choice_it.data()->rating() > best_rating) {
2448 best_rating = choice_it.data()->rating();
2462 auto *page_res =
new PAGE_RES(
false, block_list,
2471 pass1_result =
new PAGE_RES(
false, block_list,
2474 return pass1_result;
2484 length = (len == -1 ? strlen(repr) : len);
2503 static void add_space(TESS_CHAR_IT* it) {
2504 auto *t =
new TESS_CHAR(0,
" ");
2505 it->add_after_then_move(t);
2509 static float rating_to_cost(
float rating) {
2510 rating = 100 + rating;
2514 if (rating < 0) rating = 0;
2522 static void extract_result(TESS_CHAR_IT* out,
2526 while (page_res_it.word() !=
nullptr) {
2534 int n = strlen(len);
2535 for (
int i = 0; i < n; i++) {
2539 out->add_after_then_move(tc);
2543 page_res_it.forward();
2560 TESS_CHAR_LIST tess_chars;
2561 TESS_CHAR_IT tess_chars_it(&tess_chars);
2562 extract_result(&tess_chars_it, page_res);
2563 tess_chars_it.move_to_first();
2564 int n = tess_chars.length();
2566 *lengths =
new int[n];
2567 *costs =
new float[n];
2573 for (tess_chars_it.mark_cycle_pt();
2574 !tess_chars_it.cycled_list();
2575 tess_chars_it.forward(), i++) {
2577 text_len += (*lengths)[i] = tc->
length;
2578 (*costs)[i] = tc->
cost;
2582 (*y1)[i] = tc->
box.
top();
2584 char *p = *text =
new char[text_len];
2586 tess_chars_it.move_to_first();
2587 for (tess_chars_it.mark_cycle_pt();
2588 !tess_chars_it.cycled_list();
2589 tess_chars_it.forward()) {
2607 int* feature_outline_index) {
2613 &cn_features, &fx_info, &outline_counts);
2618 *num_features = cn_features.
size();
2619 memcpy(int_features, &cn_features[0], *num_features *
sizeof(cn_features[0]));
2621 if (feature_outline_index !=
nullptr) {
2623 for (
int i = 0; i < outline_counts.
size(); ++i) {
2624 while (f < outline_counts[i])
2625 feature_outline_index[f++] = i;
2633 int left,
int top,
int right,
int bottom) {
2634 TBOX box(left, bottom, right, top);
2635 BLOCK_IT b_it(blocks);
2636 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
2637 BLOCK* block = b_it.data();
2641 for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
2642 ROW* row = r_it.data();
2646 for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
2647 WERD* word = w_it.data();
2658 int num_max_matches,
2661 int* num_matches_returned) {
2662 auto* choices =
new BLOB_CHOICE_LIST;
2664 BLOB_CHOICE_IT choices_it(choices);
2665 int& index = *num_matches_returned;
2667 for (choices_it.mark_cycle_pt();
2668 !choices_it.cycled_list() && index < num_max_matches;
2669 choices_it.forward()) {
2672 ratings[index] = choice->
rating();
2675 *num_matches_returned = index;
2678 #endif // ndef DISABLED_LEGACY_ENGINE
bool DetectOrientationScript(int *orient_deg, float *orient_conf, const char **script_name, float *script_conf)
const STRING & unichar_string() const
void delete_data_pointers()
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void GetLoadedLanguagesAsVector(GenericVector< STRING > *langs) const
MutableIterator * GetMutableIterator()
int NumDawgs() const
Return the number of dawgs in the dawgs_ vector.
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)
static void ClearPersistentCache()
void PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, OSResults *osr)
bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const override
bool PSM_OSD_ENABLED(int pageseg_mode)
tesseract::BoxWord * box_word
float angle() const
find angle
bool tessedit_train_from_boxes
virtual TESS_LOCAL bool Threshold(Pix **pix)
void ResetDocumentDictionary()
TBOX intersection(const TBOX &box) const
void add_str_int(const char *str, int number)
float base_line(float xpos) const
PAGE_RES * page_res_
The page-level data.
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
bool flag(WERD_FLAGS mask) const
Boxa * GetConnectedComponents(Pixa **cc)
C_OUTLINE_LIST * out_list()
TESS_API int OrientationIdToValue(const int &id)
Boxa * GetStrips(Pixa **pixa, int **blockids)
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
void BestChoiceToCorrectText()
int(Dict::*)(void *, const UNICHARSET &, UNICHAR_ID, bool) const DictFunc
TESS_LOCAL PAGE_RES * RecognitionPass2(BLOCK_LIST *block_list, PAGE_RES *pass1_result)
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.
bool IsBinary() const
Returns true if the source image is binary.
void recog_training_segmented(const STRING &fname, PAGE_RES *page_res, volatile ETEXT_DESC *monitor, FILE *output_file)
TESS_LOCAL int TextLength(int *blob_count)
virtual Pix * GetPixRectThresholds()
GenericVector< ParagraphModel * > * paragraph_models_
TBOX bounding_box() const
ROW_LIST * row_list()
get rows
void InitForAnalysePage()
PageIterator * AnalyseLayout()
void SetEquationDetect(EquationDetect *detector)
void set_deadline_msecs(int32_t deadline_msecs)
BLOCK_RES * block() const
CRUNCH_MODE unlv_crunch_mode
constexpr int kMaxCredibleResolution
double min_orientation_margin
bool DetectOS(OSResults *)
const int kBytesPerBoxFileLine
Boxa * GetComponentImages(PageIteratorLevel level, bool text_only, bool raw_image, int raw_padding, Pixa **pixa, int **blockids, int **paraids)
double matcher_good_threshold
Boxa * GetTextlines(bool raw_image, int raw_padding, Pixa **pixa, int **blockids, int **paraids)
WERD_RES * restart_page()
void set_unlv_suspects(WERD_RES *word)
void SetSourceYResolution(int ppi)
BLOCK_LIST * block_list_
The page layout.
bool IsAtBeginningOf(PageIteratorLevel level) const override
bool GetTextDirection(int *out_offset, float *out_slope)
const char * get_script_from_script_id(int id) const
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)
GenericVector< IntParam * > int_params
bool GetIntVariable(const char *name, int *value) const
WERD_CHOICE * prev_word_best_choice_
#define STRING_VAR(name, val, comment)
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
bool(*)(const char *filename, GenericVector< char > *data) FileReader
bool tessedit_train_line_recognizer
void read_config_file(const char *filename, SetParamConstraint constraint)
Assume a single uniform block of text. (Default.)
void LearnWord(const char *fontname, WERD_RES *word)
const char * GetStringVariable(const char *name) const
int IntCastRounded(double x)
char * TesseractRect(const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height)
int tessedit_pageseg_mode
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
TESS_API int get_best_script(int orientation_id) const
double(Dict::*)(const char *, const char *, int, const char *, int) ProbabilityInContextFunc
const char * GetDatapath()
static TESS_API DawgCache * GlobalDawgCache()
static void NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode)
UNICHAR_ID unichar_id() const
int * AllWordConfidences()
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)
#define ELISTIZEH(CLASSNAME)
Tesseract * get_sub_lang(int index) const
int GetSourceYResolution() const
bool SetDebugVariable(const char *name, const char *value)
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
GenericVector< DoubleParam * > double_params
void pgeditor_main(int width, int height, PAGE_RES *page_res)
bool AddImage(TessBaseAPI *api)
void ExtractFontName(const STRING &filename, STRING *fontname)
Pix * GetThresholdedImage()
void SetSourceResolution(int ppi)
BLOCK_LIST * FindLinesCreateBlockList()
int init_tesseract_lm(const char *arg0, const char *textbase, const char *language, TessdataManager *mgr)
bool tessedit_resegment_from_line_boxes
bool tessedit_make_boxes_from_boxes
void ApplyBoxTraining(const STRING &fontname, PAGE_RES *page_res)
Pix * GetImage(PageIteratorLevel level, int padding, Pix *original_img, int *left, int *top) const
void GetFeaturesForBlob(TBLOB *blob, INT_FEATURE_STRUCT *int_features, int *num_features, int *feature_outline_index)
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
float Confidence(PageIteratorLevel level) const
void set_min_orientation_margin(double margin)
void set_pix_grey(Pix *grey_pix)
void extract_edges(Pix *pix, BLOCK *block)
TruthCallback truth_cb_
fxn for setting truth_* in WERD_RES
PDBLK pdblk
Page Description Block.
void ClearAdaptiveClassifier()
void TidyUp(PAGE_RES *page_res)
GenericVector< StringParam * > string_params
void * cancel_this
monitor-aware progress callback
bool interactive_display_mode
bool SetVariable(const char *name, const char *value)
const TBOX & BlobBox(int index) const
WERD_CHOICE * best_choice
bool tessedit_resegment_from_boxes
bool Empty(PageIteratorLevel level) const
const char * c_str() const
virtual bool Next(PageIteratorLevel level)
PAGE_RES * ApplyBoxes(const STRING &fname, bool find_segmentation, BLOCK_LIST *block_list)
const int kBytesPerNumber
Boxa * GetWords(Pixa **pixa)
void chomp_string(char *str)
EquationDetect * equ_detect_
The equation detector.
Tesseract * osd_tesseract_
For orientation & script detection.
TESS_LOCAL void AdaptToCharacter(const char *unichar_repr, int length, float baseline, float xheight, float descender, float ascender)
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
PolyBlockType BlockType() const
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
static ResultIterator * StartOfParagraph(const LTRResultIterator &resit)
Pix * GetBinaryImage(PageIteratorLevel level) const
const char * GetInitLanguagesAsString() const
Tesseract * tesseract_
The underlying data object.
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
ResultIterator * GetIterator()
bool IsValidCharacter(const char *utf8_character)
int GetThresholdedImageScaleFactor() const
STRING * input_file_
Name used by training code.
void(Wordrec::* fill_lattice_)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
constexpr int kMinCredibleResolution
bool LoadMemBuffer(const char *name, const char *data, int size)
#define MAX_NUM_INT_FEATURES
#define BOOL_VAR(name, val, comment)
Dict & getDict() override
void SetPageSegMode(PageSegMode mode)
bool tessedit_ambigs_training
TESS_CHAR(float _cost, const char *repr, int len=-1)
bool recognition_done_
page_res_ contains recognition data.
void ReadDebugConfigFile(const char *filename)
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
tesseract::ParamsVectors * GlobalParams()
const int kBytesPer64BitNumber
ADAPT_TEMPLATES AdaptedTemplates
char * GetBoxText(int page_number)
TBOX bounding_box() const
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)
void SetBlackAndWhitelist()
static const char * Version()
void InitAdaptiveClassifier(TessdataManager *mgr)
const char kTesseractReject
void set_text(const char *new_text)
Pix ** mutable_pix_binary()
const int kMaxBytesPerLine
void SetInputName(const char *name)
const char * GetUnichar(int unichar_id)
void MaximallyChopWord(const GenericVector< TBOX > &boxes, BLOCK *block, ROW *row, WERD_RES *word_res)
void GetBlockTextOrientations(int **block_orientation, bool **vertical_writing)
void SetInputImage(Pix *pix)
bool tessedit_write_images
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
char * GetTSVText(int page_number)
TBOX bounding_box() const
bool Next(PageIteratorLevel level) override
int GetScaledEstimatedResolution() const
virtual Pix * GetPixRectGrey()
TESS_LOCAL LTRResultIterator * GetLTRIterator()
int num_sub_langs() const
void SetOutputName(const char *name)
ImageThresholder * thresholder_
Image thresholding module.
bool IsEmpty() const
Return true if no image has been set.
bool WriteTRFile(const STRING &filename)
bool GetBoolVariable(const char *name, bool *value) const
int orientation_and_script_detection(STRING &filename, OSResults *, tesseract::Tesseract *)
static TBLOB * MakeTBLOB(Pix *pix)
Automatic page segmentation, but no OSD, or OCR.
Boxa * GetRegions(Pixa **pixa)
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
PageSegMode GetPageSegMode() const
PAGE_RES * SetupApplyBoxes(const GenericVector< TBOX > &boxes, BLOCK_LIST *block_list)
void SetProbabilityInContextFunc(ProbabilityInContextFunc f)
bool TrainLineRecognizer(const STRING &input_imagename, const STRING &output_basename, BLOCK_LIST *block_list)
FileReader reader_
Reads files from any filesystem.
int IsValidWord(const char *word)
bool BeginDocument(const char *title)
void set_pix_original(Pix *original_pix)
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
static ROW * MakeTessOCRRow(float baseline, float xheight, float descender, float ascender)
const char * GetInputName()
bool ProcessPage(Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
bool ProcessPagesInternal(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
bool PTIsTextType(PolyBlockType type)
bool major_overlap(const TBOX &box) const
bool GetVariableAsString(const char *name, STRING *val)
static void DeleteBlockList(BLOCK_LIST *block_list)
void SetFillLatticeFunc(FillLatticeFunc f)
bool contains_unichar(const char *const unichar_repr) const
static TESS_LOCAL int TesseractExtractResult(char **text, int **lengths, float **costs, int **x0, int **y0, int **x1, int **y1, PAGE_RES *page_res)
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
Returns false on error.
static ROW * FindRowForBox(BLOCK_LIST *blocks, int left, int top, int right, int bottom)
void SetDictFunc(DictFunc f)
CANCEL_FUNC cancel
for errcode use
void ReSegmentByClassification(PAGE_RES *page_res)
C_BLOB_LIST * blob_list()
get blobs
int InitLangMod(const char *datapath, const char *language)
const Dawg * GetDawg(int i) const
int GetScaleFactor() const
void CorrectClassifyWords(PAGE_RES *page_res)
virtual char * GetUTF8Text(PageIteratorLevel level) const
void set_source_resolution(int ppi)
char * GetUTF8Text(PageIteratorLevel level) const
DLLSYM void tprintf(const char *format,...)
Orientation and script detection only.
void RunAdaptiveClassifier(TBLOB *blob, int num_max_matches, int *unichar_ids, float *ratings, int *num_matches_returned)
const UNICHARSET & getUnicharset() const
const Dawg * GetDawg(int index) const
Return i-th dawg pointer recorded in the dawgs_ vector.
void set_pix_thresholds(Pix *thresholds)
bool recog_all_words(PAGE_RES *page_res, ETEXT_DESC *monitor, const TBOX *target_word_box, const char *word_config, int dopasses)
bool textord_equation_detect
void DetectParagraphs(int debug_level, GenericVector< RowInfo > *row_infos, GenericVector< PARA * > *row_owners, PARA_LIST *paragraphs, GenericVector< ParagraphModel * > *models)
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
bool GetDoubleVariable(const char *name, double *value) const
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
TESS_LOCAL int FindLines()
FILE * init_recog_training(const STRING &fname)
bool AdaptToWordStr(PageSegMode mode, const char *wordstr)
int RecognizeForChopTest(ETEXT_DESC *monitor)
const char * id_to_unichar(UNICHAR_ID id) const
TESS_LOCAL PAGE_RES * RecognitionPass1(BLOCK_LIST *block_list)
void SetRectangle(int left, int top, int width, int height)
const int kNumbersPerBlob
STRING * datapath_
Current location of tessdata.
void ResetAdaptiveClassifier()
bool classify_bln_numeric_mode
Tesseract * tesseract() const
static size_t getOpenCLDevice(void **device)
void AdaptToChar(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, float Threshold, ADAPT_TEMPLATES adaptive_templates)
TESS_LOCAL bool InternalSetImage()
struct TessResultRenderer TessResultRenderer
OcrEngineMode oem() const
const STRING & unichar_lengths() const
void(Wordrec::*)(const MATRIX &, const WERD_CHOICE_LIST &, const UNICHARSET &, BlamerBundle *) FillLatticeFunc
int GetSourceYResolution()
int GetScaledYResolution() const
void ReadConfigFile(const char *filename)
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
int Recognize(ETEXT_DESC *monitor)
const int kBlnBaselineOffset
STRING * output_file_
Name used by debug code.
TESS_LOCAL void DetectParagraphs(bool after_text_recognition)
#define ELISTIZE(CLASSNAME)
STRING * language_
Last initialized language.
void PrintVariables(FILE *fp) const
STRING HOcrEscape(const char *text)
Pix * pix_original() const
void split(char c, GenericVector< STRING > *splited)
static void ResetToDefaults(ParamsVectors *member_params)
bool Baseline(PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
GenericVector< BoolParam * > bool_params
char * GetOsdText(int page_number)
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
void SetRectangle(int left, int top, int width, int height)
int(Dict::* letter_is_okay_)(void *void_dawg_args, const UNICHARSET &unicharset, UNICHAR_ID unichar_id, bool word_end) const