22 #include "config_auto.h" 28 #include "allheaders.h" 42 #if defined(HAVE_TIFFIO_H) 46 static void Win32ErrorHandler(
const char* module,
const char* fmt,
48 if (module !=
nullptr) {
49 fprintf(stderr,
"%s: ", module);
51 vfprintf(stderr, fmt, ap);
52 fprintf(stderr,
".\n");
55 static void Win32WarningHandler(
const char* module,
const char* fmt,
57 if (module !=
nullptr) {
58 fprintf(stderr,
"%s: ", module);
60 fprintf(stderr,
"Warning, ");
61 vfprintf(stderr, fmt, ap);
62 fprintf(stderr,
".\n");
68 static void PrintVersionInfo() {
73 versionStrP = getLeptonicaVersion();
74 printf(
" %s\n", versionStrP);
75 lept_free(versionStrP);
77 versionStrP = getImagelibVersions();
78 printf(
" %s\n", versionStrP);
79 lept_free(versionStrP);
82 cl_platform_id platform[4];
83 cl_uint num_platforms;
85 printf(
" OpenCL info:\n");
86 if (clGetPlatformIDs(4, platform, &num_platforms) == CL_SUCCESS) {
87 printf(
" Found %u platform(s).\n", num_platforms);
88 for (
unsigned n = 0; n < num_platforms; n++) {
90 if (clGetPlatformInfo(platform[n], CL_PLATFORM_NAME, 256, info, 0) ==
92 printf(
" Platform %u name: %s.\n", n + 1, info);
94 if (clGetPlatformInfo(platform[n], CL_PLATFORM_VERSION, 256, info, 0) ==
96 printf(
" Version: %s.\n", info);
98 cl_device_id devices[2];
100 if (clGetDeviceIDs(platform[n], CL_DEVICE_TYPE_ALL, 2, devices,
101 &num_devices) == CL_SUCCESS) {
102 printf(
" Found %u device(s).\n", num_devices);
103 for (
unsigned i = 0; i < num_devices; ++i) {
104 if (clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0) ==
106 printf(
" Device %u name: %s.\n", i + 1, info);
120 static void PrintHelpForPSM() {
122 "Page segmentation modes:\n" 123 " 0 Orientation and script detection (OSD) only.\n" 124 " 1 Automatic page segmentation with OSD.\n" 125 " 2 Automatic page segmentation, but no OSD, or OCR.\n" 126 " 3 Fully automatic page segmentation, but no OSD. (Default)\n" 127 " 4 Assume a single column of text of variable sizes.\n" 128 " 5 Assume a single uniform block of vertically aligned text.\n" 129 " 6 Assume a single uniform block of text.\n" 130 " 7 Treat the image as a single text line.\n" 131 " 8 Treat the image as a single word.\n" 132 " 9 Treat the image as a single word in a circle.\n" 133 " 10 Treat the image as a single character.\n" 134 " 11 Sparse text. Find as much text as possible in no" 135 " particular order.\n" 136 " 12 Sparse text with OSD.\n" 137 " 13 Raw line. Treat the image as a single text line,\n" 138 " bypassing hacks that are Tesseract-specific.\n";
140 #ifdef DISABLED_LEGACY_ENGINE 141 const char* disabled_osd_msg =
142 "\nNOTE: The OSD modes are currently disabled.\n";
143 printf(
"%s%s", msg, disabled_osd_msg);
149 #ifndef DISABLED_LEGACY_ENGINE 150 static void PrintHelpForOEM() {
152 "OCR Engine modes:\n" 153 " 0 Legacy engine only.\n" 154 " 1 Neural nets LSTM engine only.\n" 155 " 2 Legacy + LSTM engines.\n" 156 " 3 Default, based on what is available.\n";
160 #endif // ndef DISABLED_LEGACY_ENGINE 162 static void PrintHelpExtra(
const char* program) {
165 " %s --help | --help-extra | --help-psm | " 166 #ifndef DISABLED_LEGACY_ENGINE
170 " %s --list-langs [--tessdata-dir PATH]\n" 171 " %s --print-parameters [options...] [configfile...]\n" 172 " %s imagename|imagelist|stdin outputbase|stdout [options...] [configfile...]\n" 175 " --tessdata-dir PATH Specify the location of tessdata path.\n" 176 " --user-words PATH Specify the location of user words file.\n" 177 " --user-patterns PATH Specify the location of user patterns file.\n" 178 " --dpi VALUE Specify DPI for input image.\n" 179 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 180 " -c VAR=VALUE Set value for config variables.\n" 181 " Multiple -c arguments are allowed.\n" 182 " --psm NUM Specify page segmentation mode.\n" 183 #ifndef DISABLED_LEGACY_ENGINE
184 " --oem NUM Specify OCR Engine mode.\n" 186 "NOTE: These options must occur before any configfile.\n" 188 program, program, program, program
192 #ifndef DISABLED_LEGACY_ENGINE 200 " -h, --help Show minimal help message.\n" 201 " --help-extra Show extra help for advanced users.\n" 202 " --help-psm Show page segmentation modes.\n" 203 #ifndef DISABLED_LEGACY_ENGINE
204 " --help-oem Show OCR Engine modes.\n" 206 " -v, --version Show version information.\n" 207 " --list-langs List available languages for tesseract engine.\n" 208 " --print-parameters Print tesseract parameters.\n" 212 static void PrintHelpMessage(
const char* program) {
215 " %s --help | --help-extra | --version\n" 217 " %s imagename outputbase [options...] [configfile...]\n" 220 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 221 "NOTE: These options must occur before any configfile.\n" 224 " --help Show this help message.\n" 225 " --help-extra Show extra help for advanced users.\n" 226 " --version Show version information.\n" 227 " --list-langs List available languages for tesseract engine.\n",
228 program, program, program
234 char opt1[256], opt2[255];
235 for (
int i = 0; i < argc; i++) {
236 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
237 strncpy(opt1, argv[i + 1], 255);
239 char* p = strchr(opt1,
'=');
241 fprintf(stderr,
"Missing = in configvar assignment\n");
245 strncpy(opt2, strchr(argv[i + 1],
'=') + 1, 255);
250 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
259 printf(
"List of available languages (%d):\n", languages.
size());
260 for (
int index = 0; index < languages.
size(); ++index) {
261 STRING&
string = languages[index];
262 printf(
"%s\n",
string.
string());
267 static void PrintBanner() {
268 tprintf(
"Tesseract Open Source OCR Engine v%s with Leptonica\n",
292 static void checkArgValues(
int arg,
const char* mode,
int count) {
293 if (arg >=
count || arg < 0) {
294 printf(
"Invalid %s value, please enter a number between 0-%d\n", mode,
count - 1);
300 static void ParseArgs(
const int argc,
char** argv,
const char** lang,
301 const char** image,
const char** outputbase,
302 const char** datapath, l_int32* dpi,
bool* list_langs,
309 for (i = 1; i < argc && (*outputbase ==
nullptr || argv[i][0] ==
'-'); i++) {
310 if (*image !=
nullptr && *outputbase ==
nullptr) {
312 *outputbase = argv[i];
313 }
else if ((strcmp(argv[i],
"-h") == 0) || (strcmp(argv[i],
"--help") == 0)) {
314 PrintHelpMessage(argv[0]);
316 }
else if (strcmp(argv[i],
"--help-extra") == 0) {
317 PrintHelpExtra(argv[0]);
319 }
else if ((strcmp(argv[i],
"--help-psm") == 0)) {
322 #ifndef DISABLED_LEGACY_ENGINE 323 }
else if ((strcmp(argv[i],
"--help-oem") == 0)) {
327 }
else if ((strcmp(argv[i],
"-v") == 0) ||
328 (strcmp(argv[i],
"--version") == 0)) {
331 }
else if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
334 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
335 *datapath = argv[i + 1];
337 }
else if (strcmp(argv[i],
"--dpi") == 0 && i + 1 < argc) {
338 *dpi = atoi(argv[i + 1]);
340 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
344 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
345 vars_vec->
push_back(
"user_patterns_file");
348 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
351 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
355 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
356 #ifndef DISABLED_LEGACY_ENGINE 357 int oem = atoi(argv[i + 1]);
362 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
364 *print_parameters =
true;
365 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
368 }
else if (*image ==
nullptr) {
372 fprintf(stderr,
"Error, unknown command line argument '%s'\n", argv[i]);
381 if (*lang !=
nullptr && strcmp(*lang,
"osd")) {
384 fprintf(stderr,
"Warning, detects only orientation with -l %s\n", *lang);
391 if (*outputbase ==
nullptr && noocr ==
false) {
392 PrintHelpMessage(argv[0]);
397 static void PreloadRenderers(
402 #ifndef DISABLED_LEGACY_ENGINE 404 #endif // ndef DISABLED_LEGACY_ENGINE 413 if (renderer->
happy()) {
417 tprintf(
"Error, could not create hOCR output file: %s\n",
428 if (renderer->
happy()) {
432 tprintf(
"Error, could not create TSV output file: %s\n",
440 if (_setmode(_fileno(stdout), _O_BINARY) == -1)
441 tprintf(
"ERROR: cin to binary: %s", strerror(errno));
448 if (renderer->
happy()) {
452 tprintf(
"Error, could not create PDF output file: %s\n",
462 if (renderer->
happy()) {
466 tprintf(
"Error, could not create UNLV output file: %s\n",
475 if (renderer->
happy()) {
479 tprintf(
"Error, could not create BOX output file: %s\n",
485 if (b || renderers->
empty()) {
488 if (renderer->
happy()) {
492 tprintf(
"Error, could not create TXT output file: %s\n",
498 if (!renderers->
empty()) {
501 for (
int r = 1; r < renderers->
size(); ++r) {
502 (*renderers)[0]->insert((*renderers)[r]);
503 (*renderers)[r] =
nullptr;
514 int main(
int argc,
char** argv) {
515 const char* lang =
nullptr;
516 const char* image =
nullptr;
517 const char* outputbase =
nullptr;
518 const char* datapath =
nullptr;
519 bool list_langs =
false;
520 bool print_parameters =
false;
524 #ifdef DISABLED_LEGACY_ENGINE 537 setMsgSeverity(L_SEVERITY_ERROR);
540 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 542 TIFFSetErrorHandler(Win32ErrorHandler);
543 TIFFSetWarningHandler(Win32WarningHandler);
544 #endif // HAVE_TIFFIO_H && _WIN32 546 ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &dpi,
547 &list_langs, &print_parameters, &vars_vec, &vars_values, &arg_i,
548 &pagesegmode, &enginemode);
550 if (lang ==
nullptr) {
555 if (image ==
nullptr && !list_langs && !print_parameters)
570 const int init_failed = api.
Init(datapath, lang, enginemode, &(argv[arg_i]),
571 argc - arg_i, &vars_vec, &vars_values,
false);
573 SetVariablesFromCLArgs(&api, argc, argv);
576 PrintLangsList(&api);
581 fprintf(stderr,
"Could not initialize tesseract.\n");
585 if (print_parameters) {
587 fprintf(stdout,
"Tesseract parameters:\n");
593 FixPageSegMode(&api, pagesegmode);
596 char dpi_string[255];
597 snprintf(dpi_string, 254,
"%d", dpi);
602 int ret_val = EXIT_SUCCESS;
604 Pix* pixs = pixRead(image);
606 fprintf(stderr,
"Leptonica can't process input file: %s\n", image);
621 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" 622 "Deskew angle: %.4f\n",
623 orientation,
direction, order, deskew_angle);
625 ret_val = EXIT_FAILURE;
637 bool in_training_mode =
642 #ifdef DISABLED_LEGACY_ENGINE 644 auto osd_warning = std::string(
"");
646 const char* disabled_osd_msg =
647 "\nERROR: The page segmentation mode 0 (OSD Only) is currently disabled.\n\n";
648 fprintf(stderr,
"%s", disabled_osd_msg);
653 "\nWarning: The page segmentation mode 1 (Auto+OSD) is currently disabled. " 654 "Using PSM 3 (Auto) instead.\n\n";
658 "\nWarning: The page segmentation mode 12 (Sparse text + OSD) is currently disabled. " 659 "Using PSM 11 (Sparse text) instead.\n\n";
661 #endif // def DISABLED_LEGACY_ENGINE 666 if (in_training_mode) {
669 PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
673 if (outputbase !=
nullptr && strcmp(outputbase,
"-") &&
674 strcmp(outputbase,
"stdout")) {
678 if (!renderers.
empty()) {
679 if (banner) PrintBanner();
680 #ifdef DISABLED_LEGACY_ENGINE 681 if (!osd_warning.empty()) {
682 fprintf(stderr,
"%s",osd_warning.c_str());
685 bool succeed = api.
ProcessPages(image,
nullptr, 0, renderers[0]);
687 fprintf(stderr,
"Error during processing.\n");
Sparse text with orientation and script det.
static DawgCache * GlobalDawgCache()
void SetOutputName(const char *name)
PageSegMode GetPageSegMode() const
void SetPageSegMode(PageSegMode mode)
struct TessUnlvRenderer TessUnlvRenderer
Assume a single uniform block of text. (Default.)
struct TessPDFRenderer TessPDFRenderer
bool GetBoolVariable(const char *name, bool *value) const
static bool IsAVX512FAvailable()
struct TessTextRenderer TessTextRenderer
static bool IsAVX512BWAvailable()
int direction(EDGEPT *point)
#define PERF_COUNT_START(FUNCT_NAME)
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
const char * GetDatapath()
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
void PrintVariables(FILE *fp) const
struct TessHOcrRenderer TessHOcrRenderer
int main(int argc, char **argv)
DLLSYM void tprintf(const char *format,...)
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
Find as much text as possible in no particular order.
static bool IsSSEAvailable()
Automatic page segmentation, but no OSD, or OCR.
PageIterator * AnalyseLayout()
static bool IsAVXAvailable()
static const char * Version()
Orientation and script detection only.
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
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 bool IsAVX2Available()
struct TessBoxTextRenderer TessBoxTextRenderer
Fully automatic page segmentation, but no OSD.
bool SetVariable(const char *name, const char *value)