34#include <allheaders.h>
37#if defined(USE_OPENCL)
48#if defined(HAVE_LIBARCHIVE)
51#if defined(HAVE_LIBCURL)
52# include <curl/curl.h>
58# if defined(HAVE_TIFFIO_H)
62static void Win32ErrorHandler(
const char *module,
const char *fmt, va_list ap) {
63 if (module !=
nullptr) {
64 fprintf(stderr,
"%s: ", module);
66 vfprintf(stderr, fmt, ap);
67 fprintf(stderr,
".\n");
70static void Win32WarningHandler(
const char *module,
const char *fmt, va_list ap) {
71 if (module !=
nullptr) {
72 fprintf(stderr,
"%s: ", module);
74 fprintf(stderr,
"Warning, ");
75 vfprintf(stderr, fmt, ap);
76 fprintf(stderr,
".\n");
81class AutoWin32ConsoleOutputCP {
83 explicit AutoWin32ConsoleOutputCP(UINT codeCP) {
84 oldCP_ = GetConsoleOutputCP();
85 SetConsoleOutputCP(codeCP);
87 ~AutoWin32ConsoleOutputCP() {
88 SetConsoleOutputCP(oldCP_);
95static AutoWin32ConsoleOutputCP autoWin32ConsoleOutputCP(CP_UTF8);
101static void PrintVersionInfo() {
106 versionStrP = getLeptonicaVersion();
107 printf(
" %s\n", versionStrP);
108 lept_free(versionStrP);
110 versionStrP = getImagelibVersions();
111 printf(
" %s\n", versionStrP);
112 lept_free(versionStrP);
115 cl_platform_id platform[4];
116 cl_uint num_platforms;
118 printf(
" OpenCL info:\n");
119 if (clGetPlatformIDs(4, platform, &num_platforms) == CL_SUCCESS) {
120 printf(
" Found %u platform(s).\n", num_platforms);
121 for (
unsigned n = 0; n < num_platforms; n++) {
123 if (clGetPlatformInfo(platform[n], CL_PLATFORM_NAME, 256, info, 0) == CL_SUCCESS) {
124 printf(
" Platform %u name: %s.\n", n + 1, info);
126 if (clGetPlatformInfo(platform[n], CL_PLATFORM_VERSION, 256, info, 0) == CL_SUCCESS) {
127 printf(
" Version: %s.\n", info);
129 cl_device_id devices[2];
131 if (clGetDeviceIDs(platform[n], CL_DEVICE_TYPE_ALL, 2, devices, &num_devices) == CL_SUCCESS) {
132 printf(
" Found %u device(s).\n", num_devices);
133 for (
unsigned i = 0; i < num_devices; ++i) {
134 if (clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0) == CL_SUCCESS) {
135 printf(
" Device %u name: %s.\n", i + 1, info);
142#if defined(HAVE_NEON) || defined(__aarch64__)
144 printf(
" Found NEON\n");
147 printf(
" Found AVX512BW\n");
150 printf(
" Found AVX512F\n");
153 printf(
" Found AVX2\n");
156 printf(
" Found AVX\n");
159 printf(
" Found FMA\n");
162 printf(
" Found SSE4.1\n");
166 printf(
" Found OpenMP %d\n", _OPENMP);
168#if defined(HAVE_LIBARCHIVE)
169# if ARCHIVE_VERSION_NUMBER >= 3002000
170 printf(
" Found %s\n", archive_version_details());
172 printf(
" Found %s\n", archive_version_string());
175#if defined(HAVE_LIBCURL)
176 printf(
" Found %s\n", curl_version());
180static void PrintHelpForPSM() {
182 "Page segmentation modes:\n"
183 " 0 Orientation and script detection (OSD) only.\n"
184 " 1 Automatic page segmentation with OSD.\n"
185 " 2 Automatic page segmentation, but no OSD, or OCR. (not "
187 " 3 Fully automatic page segmentation, but no OSD. (Default)\n"
188 " 4 Assume a single column of text of variable sizes.\n"
189 " 5 Assume a single uniform block of vertically aligned text.\n"
190 " 6 Assume a single uniform block of text.\n"
191 " 7 Treat the image as a single text line.\n"
192 " 8 Treat the image as a single word.\n"
193 " 9 Treat the image as a single word in a circle.\n"
194 " 10 Treat the image as a single character.\n"
195 " 11 Sparse text. Find as much text as possible in no"
196 " particular order.\n"
197 " 12 Sparse text with OSD.\n"
198 " 13 Raw line. Treat the image as a single text line,\n"
199 " bypassing hacks that are Tesseract-specific.\n";
201#ifdef DISABLED_LEGACY_ENGINE
202 const char *disabled_osd_msg =
"\nNOTE: The OSD modes are currently disabled.\n";
203 printf(
"%s%s", msg, disabled_osd_msg);
209#ifndef DISABLED_LEGACY_ENGINE
210static void PrintHelpForOEM() {
212 "OCR Engine modes:\n"
213 " 0 Legacy engine only.\n"
214 " 1 Neural nets LSTM engine only.\n"
215 " 2 Legacy + LSTM engines.\n"
216 " 3 Default, based on what is available.\n";
222static void PrintHelpExtra(
const char *program) {
225 " %s --help | --help-extra | --help-psm | "
226#ifndef DISABLED_LEGACY_ENGINE
230 " %s --list-langs [--tessdata-dir PATH]\n"
231#ifndef DISABLED_LEGACY_ENGINE
232 " %s --print-fonts-table [options...] [configfile...]\n"
234 " %s --print-parameters [options...] [configfile...]\n"
235 " %s imagename|imagelist|stdin outputbase|stdout [options...] "
239 " --tessdata-dir PATH Specify the location of tessdata path.\n"
240 " --user-words PATH Specify the location of user words file.\n"
241 " --user-patterns PATH Specify the location of user patterns file.\n"
242 " --dpi VALUE Specify DPI for input image.\n"
243 " --loglevel LEVEL Specify logging level. LEVEL can be\n"
244 " ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL or OFF.\n"
245 " -l LANG[+LANG] Specify language(s) used for OCR.\n"
246 " -c VAR=VALUE Set value for config variables.\n"
247 " Multiple -c arguments are allowed.\n"
248 " --psm NUM Specify page segmentation mode.\n"
249#ifndef DISABLED_LEGACY_ENGINE
250 " --oem NUM Specify OCR Engine mode.\n"
252 "NOTE: These options must occur before any configfile.\n"
254 program, program, program, program
255#ifndef DISABLED_LEGACY_ENGINE
261#ifndef DISABLED_LEGACY_ENGINE
269 " -h, --help Show minimal help message.\n"
270 " --help-extra Show extra help for advanced users.\n"
271 " --help-psm Show page segmentation modes.\n"
272#ifndef DISABLED_LEGACY_ENGINE
273 " --help-oem Show OCR Engine modes.\n"
275 " -v, --version Show version information.\n"
276 " --list-langs List available languages for tesseract engine.\n"
277#ifndef DISABLED_LEGACY_ENGINE
278 " --print-fonts-table Print tesseract fonts table.\n"
280 " --print-parameters Print tesseract parameters.\n");
283static void PrintHelpMessage(
const char *program) {
286 " %s --help | --help-extra | --version\n"
288 " %s imagename outputbase [options...] [configfile...]\n"
291 " -l LANG[+LANG] Specify language(s) used for OCR.\n"
292 "NOTE: These options must occur before any configfile.\n"
295 " --help Show this help message.\n"
296 " --help-extra Show extra help for advanced users.\n"
297 " --version Show version information.\n"
298 " --list-langs List available languages for tesseract "
300 program, program, program);
305 char opt1[256], opt2[255];
306 for (
int i = 0; i < argc; i++) {
307 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
308 strncpy(opt1, argv[i + 1], 255);
310 char *p = strchr(opt1,
'=');
312 fprintf(stderr,
"Missing = in configvar assignment\n");
317 strncpy(opt2, strchr(argv[i + 1],
'=') + 1,
sizeof(opt2) - 1);
322 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
330 std::vector<std::string> languages;
332 printf(
"List of available languages in \"%s\" (%zu):\n",
334 for (
const auto &language : languages) {
335 printf(
"%s\n", language.c_str());
360static bool checkArgValues(
int arg,
const char *mode,
int count) {
361 if (arg >= count || arg < 0) {
362 printf(
"Invalid %s value, please enter a number between 0-%d\n", mode, count - 1);
369static bool ParseArgs(
int argc,
char **argv,
const char **lang,
const char **image,
370 const char **outputbase,
const char **datapath, l_int32 *dpi,
371 bool *list_langs,
bool *print_parameters,
bool* print_fonts_table, std::vector<std::string> *vars_vec,
372 std::vector<std::string> *vars_values, l_int32 *arg_i,
376 for (i = 1; i < argc && (*outputbase ==
nullptr || argv[i][0] ==
'-'); i++) {
377 if (*image !=
nullptr && *outputbase ==
nullptr) {
379 *outputbase = argv[i];
380 }
else if ((strcmp(argv[i],
"-h") == 0) || (strcmp(argv[i],
"--help") == 0)) {
381 PrintHelpMessage(argv[0]);
383 }
else if (strcmp(argv[i],
"--help-extra") == 0) {
384 PrintHelpExtra(argv[0]);
386 }
else if ((strcmp(argv[i],
"--help-psm") == 0)) {
389#ifndef DISABLED_LEGACY_ENGINE
390 }
else if ((strcmp(argv[i],
"--help-oem") == 0)) {
394 }
else if ((strcmp(argv[i],
"-v") == 0) || (strcmp(argv[i],
"--version") == 0)) {
397 }
else if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
400 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
401 *datapath = argv[i + 1];
403 }
else if (strcmp(argv[i],
"--dpi") == 0 && i + 1 < argc) {
404 *dpi = atoi(argv[i + 1]);
406 }
else if (strcmp(argv[i],
"--loglevel") == 0 && i + 1 < argc) {
408 const std::string loglevel_string = argv[++i];
409 static const std::map<const std::string, int> loglevels {
420 auto loglevel = loglevels.at(loglevel_string);
422 }
catch(
const std::out_of_range& e) {
424 tprintf(
"Error, unsupported --loglevel %s\n", loglevel_string.c_str());
427 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
428 vars_vec->push_back(
"user_words_file");
429 vars_values->push_back(argv[i + 1]);
431 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
432 vars_vec->push_back(
"user_patterns_file");
433 vars_values->push_back(argv[i + 1]);
435 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
438 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
444 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
445#ifndef DISABLED_LEGACY_ENGINE
446 int oem = atoi(argv[i + 1]);
453 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
455 *print_parameters =
true;
456#ifndef DISABLED_LEGACY_ENGINE
457 }
else if (strcmp(argv[i],
"--print-fonts-table") == 0) {
459 *print_fonts_table =
true;
461 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
464 }
else if (*image ==
nullptr) {
468 fprintf(stderr,
"Error, unknown command line argument '%s'\n", argv[i]);
477 if (*lang !=
nullptr && strcmp(*lang,
"osd")) {
480 fprintf(stderr,
"Warning, detects only orientation with -l %s\n", *lang);
487 if (*outputbase ==
nullptr && noocr ==
false) {
488 PrintHelpMessage(argv[0]);
496 std::vector<std::unique_ptr<TessResultRenderer>> &renderers,
499#ifndef DISABLED_LEGACY_ENGINE
500 renderers.push_back(std::make_unique<tesseract::TessOsdRenderer>(outputbase));
509 auto renderer = std::make_unique<tesseract::TessHOcrRenderer>(outputbase, font_info);
510 if (renderer->happy()) {
511 renderers.push_back(std::move(renderer));
513 tprintf(
"Error, could not create hOCR output file: %s\n", strerror(errno));
520 auto renderer = std::make_unique<tesseract::TessAltoRenderer>(outputbase);
521 if (renderer->happy()) {
522 renderers.push_back(std::move(renderer));
524 tprintf(
"Error, could not create ALTO output file: %s\n", strerror(errno));
533 auto renderer = std::make_unique<tesseract::TessTsvRenderer>(outputbase, font_info);
534 if (renderer->happy()) {
535 renderers.push_back(std::move(renderer));
537 tprintf(
"Error, could not create TSV output file: %s\n", strerror(errno));
545 if (_setmode(_fileno(stdout), _O_BINARY) == -1)
546 tprintf(
"ERROR: cin to binary: %s", strerror(errno));
550 auto renderer = std::make_unique<tesseract::TessPDFRenderer>(outputbase, api.
GetDatapath(), textonly);
551 if (renderer->happy()) {
552 renderers.push_back(std::move(renderer));
554 tprintf(
"Error, could not create PDF output file: %s\n", strerror(errno));
562 auto renderer = std::make_unique<tesseract::TessUnlvRenderer>(outputbase);
563 if (renderer->happy()) {
564 renderers.push_back(std::move(renderer));
566 tprintf(
"Error, could not create UNLV output file: %s\n", strerror(errno));
573 auto renderer = std::make_unique<tesseract::TessLSTMBoxRenderer>(outputbase);
574 if (renderer->happy()) {
575 renderers.push_back(std::move(renderer));
577 tprintf(
"Error, could not create LSTM BOX output file: %s\n", strerror(errno));
584 auto renderer = std::make_unique<tesseract::TessBoxTextRenderer>(outputbase);
585 if (renderer->happy()) {
586 renderers.push_back(std::move(renderer));
588 tprintf(
"Error, could not create BOX output file: %s\n", strerror(errno));
595 auto renderer = std::make_unique<tesseract::TessWordStrBoxRenderer>(outputbase);
596 if (renderer->happy()) {
597 renderers.push_back(std::move(renderer));
599 tprintf(
"Error, could not create WordStr BOX output file: %s\n", strerror(errno));
605 if (b || (!error && renderers.empty())) {
609 auto renderer = std::make_unique<tesseract::TessTextRenderer>(outputbase);
610 if (renderer->happy()) {
611 renderers.push_back(std::move(renderer));
613 tprintf(
"Error, could not create TXT output file: %s\n", strerror(errno));
620 for (
size_t r = 1; r < renderers.size(); ++r) {
621 renderers[0]->insert(renderers[r].get());
622 renderers[r].release();
631int main(
int argc,
char **argv) {
632#if defined(__USE_GNU) && defined(HAVE_FEENABLEEXCEPT)
634# if defined(__clang__)
636 feenableexcept(FE_DIVBYZERO);
638 feenableexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_INVALID);
641 const char *lang =
nullptr;
642 const char *image =
nullptr;
643 const char *outputbase =
nullptr;
644 const char *datapath =
nullptr;
645 bool list_langs =
false;
646 bool print_parameters =
false;
647 bool print_fonts_table =
false;
651#ifdef DISABLED_LEGACY_ENGINE
656 std::vector<std::string> vars_vec;
657 std::vector<std::string> vars_values;
659 if (std::getenv(
"LEPT_MSG_SEVERITY")) {
661 setMsgSeverity(L_SEVERITY_EXTERNAL);
664 setMsgSeverity(L_SEVERITY_ERROR);
667#if defined(HAVE_TIFFIO_H) && defined(_WIN32)
669 TIFFSetErrorHandler(Win32ErrorHandler);
670 TIFFSetWarningHandler(Win32WarningHandler);
673 if (!ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &dpi, &list_langs,
674 &print_parameters, &print_fonts_table, &vars_vec, &vars_values, &arg_i, &pagesegmode, &enginemode)) {
678 bool in_recognition_mode = !list_langs && !print_parameters && !print_fonts_table;
680 if (lang ==
nullptr && in_recognition_mode) {
685 if (image ==
nullptr && in_recognition_mode) {
698 const int init_failed = api.
Init(datapath, lang, enginemode, &(argv[arg_i]), argc - arg_i,
699 &vars_vec, &vars_values,
false);
701 if (!SetVariablesFromCLArgs(api, argc, argv)) {
714 fprintf(stderr,
"Could not initialize tesseract.\n");
718 if (print_parameters) {
720 fprintf(stdout,
"Tesseract parameters:\n");
726#ifndef DISABLED_LEGACY_ENGINE
727 if (print_fonts_table) {
729 fprintf(stdout,
"Tesseract fonts table:\n");
736 FixPageSegMode(api, pagesegmode);
739 auto dpi_string = std::to_string(dpi);
740 api.
SetVariable(
"user_defined_dpi", dpi_string.c_str());
743 int ret_val = EXIT_SUCCESS;
746 Pix *pixs = pixRead(image);
748 fprintf(stderr,
"Leptonica can't process input file: %s\n", image);
759 const std::unique_ptr<const tesseract::PageIterator> it(api.
AnalyseLayout());
763 it->Orientation(&orientation, &direction, &order, &deskew_angle);
765 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
766 "Deskew angle: %.4f\n",
767 orientation, direction, order, deskew_angle);
769 ret_val = EXIT_FAILURE;
780 bool in_training_mode = (api.
GetBoolVariable(
"tessedit_ambigs_training", &b) && b) ||
785#ifdef DISABLED_LEGACY_ENGINE
787 auto osd_warning = std::string(
"");
789 const char *disabled_osd_msg =
790 "\nERROR: The page segmentation mode 0 (OSD Only) is currently "
792 fprintf(stderr,
"%s", disabled_osd_msg);
797 "\nWarning: The page segmentation mode 1 (Auto+OSD) is currently "
799 "Using PSM 3 (Auto) instead.\n\n";
803 "\nWarning: The page segmentation mode 12 (Sparse text + OSD) is "
804 "currently disabled. "
805 "Using PSM 11 (Sparse text) instead.\n\n";
809 std::vector<std::unique_ptr<TessResultRenderer>> renderers;
811 if (in_training_mode) {
812 renderers.push_back(
nullptr);
813 }
else if (outputbase !=
nullptr) {
814 PreloadRenderers(api, renderers, pagesegmode, outputbase);
817 if (!renderers.empty()) {
818#ifdef DISABLED_LEGACY_ENGINE
819 if (!osd_warning.empty()) {
820 fprintf(stderr,
"%s", osd_warning.c_str());
823 bool succeed = api.
ProcessPages(image,
nullptr, 0, renderers[0].get());
825 fprintf(stderr,
"Error during processing.\n");
826 ret_val = EXIT_FAILURE;
@ PSM_OSD_ONLY
Orientation and script detection only.
@ PSM_COUNT
Number of enum entries.
@ PSM_SPARSE_TEXT
Find as much text as possible in no particular order.
@ PSM_AUTO_ONLY
Automatic page segmentation, but no OSD, or OCR.
@ PSM_AUTO
Fully automatic page segmentation, but no OSD.
@ PSM_SINGLE_BLOCK
Assume a single uniform block of text. (Default.)
@ PSM_SPARSE_TEXT_OSD
Sparse text with orientation and script det.
void tprintf(const char *format,...)
void SetPageSegMode(PageSegMode mode)
const char * GetDatapath()
bool SetVariable(const char *name, const char *value)
static const char * Version()
void GetAvailableLanguagesAsVector(std::vector< std::string > *langs) const
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
PageSegMode GetPageSegMode() const
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const std::vector< std::string > *vars_vec, const std::vector< std::string > *vars_values, bool set_only_non_debug_params)
void PrintVariables(FILE *fp) const
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
PageIterator * AnalyseLayout()
void PrintFontsTable(FILE *fp) const
bool GetBoolVariable(const char *name, bool *value) const
void SetOutputName(const char *name)
static bool IsNEONAvailable()
static bool IsAVX512BWAvailable()
static bool IsFMAAvailable()
static bool IsAVXAvailable()
static bool IsAVX512FAvailable()
static bool IsSSEAvailable()
static bool IsAVX2Available()
static TESS_API void Update()
static DawgCache * GlobalDawgCache()