21 #include "config_auto.h" 27 #include "allheaders.h" 30 #if defined(USE_OPENCL) 41 #if defined(HAVE_LIBARCHIVE) 48 #if defined(HAVE_TIFFIO_H) 52 static void Win32ErrorHandler(
const char* module,
const char* fmt,
54 if (module !=
nullptr) {
55 fprintf(stderr,
"%s: ", module);
57 vfprintf(stderr, fmt, ap);
58 fprintf(stderr,
".\n");
61 static void Win32WarningHandler(
const char* module,
const char* fmt,
63 if (module !=
nullptr) {
64 fprintf(stderr,
"%s: ", module);
66 fprintf(stderr,
"Warning, ");
67 vfprintf(stderr, fmt, ap);
68 fprintf(stderr,
".\n");
74 static void PrintVersionInfo() {
79 versionStrP = getLeptonicaVersion();
80 printf(
" %s\n", versionStrP);
81 lept_free(versionStrP);
83 versionStrP = getImagelibVersions();
84 printf(
" %s\n", versionStrP);
85 lept_free(versionStrP);
88 cl_platform_id platform[4];
89 cl_uint num_platforms;
91 printf(
" OpenCL info:\n");
92 if (clGetPlatformIDs(4, platform, &num_platforms) == CL_SUCCESS) {
93 printf(
" Found %u platform(s).\n", num_platforms);
94 for (
unsigned n = 0; n < num_platforms; n++) {
96 if (clGetPlatformInfo(platform[n], CL_PLATFORM_NAME, 256, info, 0) ==
98 printf(
" Platform %u name: %s.\n", n + 1, info);
100 if (clGetPlatformInfo(platform[n], CL_PLATFORM_VERSION, 256, info, 0) ==
102 printf(
" Version: %s.\n", info);
104 cl_device_id devices[2];
106 if (clGetDeviceIDs(platform[n], CL_DEVICE_TYPE_ALL, 2, devices,
107 &num_devices) == CL_SUCCESS) {
108 printf(
" Found %u device(s).\n", num_devices);
109 for (
unsigned i = 0; i < num_devices; ++i) {
110 if (clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0) ==
112 printf(
" Device %u name: %s.\n", i + 1, info);
125 printf(
" Found OpenMP %d\n", _OPENMP);
127 #if defined(HAVE_LIBARCHIVE) 128 # if ARCHIVE_VERSION_NUMBER >= 3002000 129 printf(
" Found %s\n", archive_version_details());
131 printf(
" Found %s\n", archive_version_string());
132 # endif // ARCHIVE_VERSION_NUMBER 133 #endif // HAVE_LIBARCHIVE 137 static void PrintHelpForPSM() {
139 "Page segmentation modes:\n" 140 " 0 Orientation and script detection (OSD) only.\n" 141 " 1 Automatic page segmentation with OSD.\n" 142 " 2 Automatic page segmentation, but no OSD, or OCR. (not implemented)\n" 143 " 3 Fully automatic page segmentation, but no OSD. (Default)\n" 144 " 4 Assume a single column of text of variable sizes.\n" 145 " 5 Assume a single uniform block of vertically aligned text.\n" 146 " 6 Assume a single uniform block of text.\n" 147 " 7 Treat the image as a single text line.\n" 148 " 8 Treat the image as a single word.\n" 149 " 9 Treat the image as a single word in a circle.\n" 150 " 10 Treat the image as a single character.\n" 151 " 11 Sparse text. Find as much text as possible in no" 152 " particular order.\n" 153 " 12 Sparse text with OSD.\n" 154 " 13 Raw line. Treat the image as a single text line,\n" 155 " bypassing hacks that are Tesseract-specific.\n";
157 #ifdef DISABLED_LEGACY_ENGINE 158 const char* disabled_osd_msg =
159 "\nNOTE: The OSD modes are currently disabled.\n";
160 printf(
"%s%s", msg, disabled_osd_msg);
166 #ifndef DISABLED_LEGACY_ENGINE 167 static void PrintHelpForOEM() {
169 "OCR Engine modes:\n" 170 " 0 Legacy engine only.\n" 171 " 1 Neural nets LSTM engine only.\n" 172 " 2 Legacy + LSTM engines.\n" 173 " 3 Default, based on what is available.\n";
177 #endif // ndef DISABLED_LEGACY_ENGINE 179 static void PrintHelpExtra(
const char* program) {
182 " %s --help | --help-extra | --help-psm | " 183 #ifndef DISABLED_LEGACY_ENGINE
187 " %s --list-langs [--tessdata-dir PATH]\n" 188 " %s --print-parameters [options...] [configfile...]\n" 189 " %s imagename|imagelist|stdin outputbase|stdout [options...] [configfile...]\n" 192 " --tessdata-dir PATH Specify the location of tessdata path.\n" 193 " --user-words PATH Specify the location of user words file.\n" 194 " --user-patterns PATH Specify the location of user patterns file.\n" 195 " --dpi VALUE Specify DPI for input image.\n" 196 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 197 " -c VAR=VALUE Set value for config variables.\n" 198 " Multiple -c arguments are allowed.\n" 199 " --psm NUM Specify page segmentation mode.\n" 200 #ifndef DISABLED_LEGACY_ENGINE
201 " --oem NUM Specify OCR Engine mode.\n" 203 "NOTE: These options must occur before any configfile.\n" 205 program, program, program, program
209 #ifndef DISABLED_LEGACY_ENGINE 217 " -h, --help Show minimal help message.\n" 218 " --help-extra Show extra help for advanced users.\n" 219 " --help-psm Show page segmentation modes.\n" 220 #ifndef DISABLED_LEGACY_ENGINE
221 " --help-oem Show OCR Engine modes.\n" 223 " -v, --version Show version information.\n" 224 " --list-langs List available languages for tesseract engine.\n" 225 " --print-parameters Print tesseract parameters.\n" 229 static void PrintHelpMessage(
const char* program) {
232 " %s --help | --help-extra | --version\n" 234 " %s imagename outputbase [options...] [configfile...]\n" 237 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 238 "NOTE: These options must occur before any configfile.\n" 241 " --help Show this help message.\n" 242 " --help-extra Show extra help for advanced users.\n" 243 " --version Show version information.\n" 244 " --list-langs List available languages for tesseract engine.\n",
245 program, program, program
251 char opt1[256], opt2[255];
252 for (
int i = 0; i < argc; i++) {
253 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
254 strncpy(opt1, argv[i + 1], 255);
256 char* p = strchr(opt1,
'=');
258 fprintf(stderr,
"Missing = in configvar assignment\n");
262 strncpy(opt2, strchr(argv[i + 1],
'=') + 1,
sizeof(opt2) - 1);
267 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
276 printf(
"List of available languages (%d):\n", languages.
size());
277 for (
int index = 0; index < languages.
size(); ++index) {
278 STRING&
string = languages[index];
279 printf(
"%s\n",
string.
string());
284 static void PrintBanner() {
285 tprintf(
"Tesseract Open Source OCR Engine v%s with Leptonica\n",
309 static void checkArgValues(
int arg,
const char* mode,
int count) {
310 if (arg >= count || arg < 0) {
311 printf(
"Invalid %s value, please enter a number between 0-%d\n", mode, count - 1);
317 static void ParseArgs(
const int argc,
char** argv,
const char** lang,
318 const char** image,
const char** outputbase,
319 const char** datapath, l_int32* dpi,
bool* list_langs,
326 for (i = 1; i < argc && (*outputbase ==
nullptr || argv[i][0] ==
'-'); i++) {
327 if (*image !=
nullptr && *outputbase ==
nullptr) {
329 *outputbase = argv[i];
330 }
else if ((strcmp(argv[i],
"-h") == 0) || (strcmp(argv[i],
"--help") == 0)) {
331 PrintHelpMessage(argv[0]);
333 }
else if (strcmp(argv[i],
"--help-extra") == 0) {
334 PrintHelpExtra(argv[0]);
336 }
else if ((strcmp(argv[i],
"--help-psm") == 0)) {
339 #ifndef DISABLED_LEGACY_ENGINE 340 }
else if ((strcmp(argv[i],
"--help-oem") == 0)) {
344 }
else if ((strcmp(argv[i],
"-v") == 0) ||
345 (strcmp(argv[i],
"--version") == 0)) {
348 }
else if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
351 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
352 *datapath = argv[i + 1];
354 }
else if (strcmp(argv[i],
"--dpi") == 0 && i + 1 < argc) {
355 *dpi = atoi(argv[i + 1]);
357 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
361 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
362 vars_vec->
push_back(
"user_patterns_file");
365 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
368 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
372 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
373 #ifndef DISABLED_LEGACY_ENGINE 374 int oem = atoi(argv[i + 1]);
379 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
381 *print_parameters =
true;
382 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
385 }
else if (*image ==
nullptr) {
389 fprintf(stderr,
"Error, unknown command line argument '%s'\n", argv[i]);
398 if (*lang !=
nullptr && strcmp(*lang,
"osd")) {
401 fprintf(stderr,
"Warning, detects only orientation with -l %s\n", *lang);
408 if (*outputbase ==
nullptr && noocr ==
false) {
409 PrintHelpMessage(argv[0]);
414 static void PreloadRenderers(
419 #ifndef DISABLED_LEGACY_ENGINE 421 #endif // ndef DISABLED_LEGACY_ENGINE 431 if (renderer->happy()) {
435 tprintf(
"Error, could not create hOCR output file: %s\n",
445 if (renderer->happy()) {
449 tprintf(
"Error, could not create ALTO output file: %s\n",
461 if (renderer->happy()) {
465 tprintf(
"Error, could not create TSV output file: %s\n",
474 if (_setmode(_fileno(stdout), _O_BINARY) == -1)
475 tprintf(
"ERROR: cin to binary: %s", strerror(errno));
482 if (renderer->happy()) {
486 tprintf(
"Error, could not create PDF output file: %s\n",
497 if (renderer->happy()) {
501 tprintf(
"Error, could not create UNLV output file: %s\n",
511 if (renderer->happy()) {
515 tprintf(
"Error, could not create LSTM BOX output file: %s\n",
525 if (renderer->happy()) {
529 tprintf(
"Error, could not create BOX output file: %s\n",
539 if (renderer->happy()) {
543 tprintf(
"Error, could not create WordStr BOX output file: %s\n",
550 if (b || (!error && renderers->
empty())) {
553 if (renderer->happy()) {
557 tprintf(
"Error, could not create TXT output file: %s\n",
563 if (!renderers->
empty()) {
566 for (
int r = 1; r < renderers->
size(); ++r) {
567 (*renderers)[0]->insert((*renderers)[r]);
568 (*renderers)[r] =
nullptr;
579 int main(
int argc,
char** argv) {
580 const char* lang =
nullptr;
581 const char* image =
nullptr;
582 const char* outputbase =
nullptr;
583 const char* datapath =
nullptr;
584 bool list_langs =
false;
585 bool print_parameters =
false;
589 #ifdef DISABLED_LEGACY_ENGINE 602 setMsgSeverity(L_SEVERITY_ERROR);
605 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 607 TIFFSetErrorHandler(Win32ErrorHandler);
608 TIFFSetWarningHandler(Win32WarningHandler);
609 #endif // HAVE_TIFFIO_H && _WIN32 611 ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &dpi,
612 &list_langs, &print_parameters, &vars_vec, &vars_values, &arg_i,
613 &pagesegmode, &enginemode);
615 if (lang ==
nullptr) {
620 if (image ==
nullptr && !list_langs && !print_parameters)
633 const int init_failed = api.
Init(datapath, lang, enginemode, &(argv[arg_i]),
634 argc - arg_i, &vars_vec, &vars_values,
false);
636 SetVariablesFromCLArgs(&api, argc, argv);
642 PrintLangsList(&api);
647 fprintf(stderr,
"Could not initialize tesseract.\n");
651 if (print_parameters) {
653 fprintf(stdout,
"Tesseract parameters:\n");
659 FixPageSegMode(&api, pagesegmode);
662 char dpi_string[255];
663 snprintf(dpi_string, 254,
"%d", dpi);
668 int ret_val = EXIT_SUCCESS;
670 Pix* pixs = pixRead(image);
672 fprintf(stderr,
"Leptonica can't process input file: %s\n", image);
687 it->
Orientation(&orientation, &direction, &order, &deskew_angle);
689 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" 690 "Deskew angle: %.4f\n",
691 orientation, direction, order, deskew_angle);
693 ret_val = EXIT_FAILURE;
705 bool in_training_mode =
710 #ifdef DISABLED_LEGACY_ENGINE 712 auto osd_warning = std::string(
"");
714 const char* disabled_osd_msg =
715 "\nERROR: The page segmentation mode 0 (OSD Only) is currently disabled.\n\n";
716 fprintf(stderr,
"%s", disabled_osd_msg);
721 "\nWarning: The page segmentation mode 1 (Auto+OSD) is currently disabled. " 722 "Using PSM 3 (Auto) instead.\n\n";
726 "\nWarning: The page segmentation mode 12 (Sparse text + OSD) is currently disabled. " 727 "Using PSM 11 (Sparse text) instead.\n\n";
729 #endif // def DISABLED_LEGACY_ENGINE 734 if (in_training_mode) {
736 }
else if (outputbase !=
nullptr) {
737 PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
741 if (outputbase !=
nullptr && strcmp(outputbase,
"-") &&
742 strcmp(outputbase,
"stdout")) {
746 if (!renderers.
empty()) {
747 if (banner) PrintBanner();
748 #ifdef DISABLED_LEGACY_ENGINE 749 if (!osd_warning.empty()) {
750 fprintf(stderr,
"%s",osd_warning.c_str());
753 bool succeed = api.
ProcessPages(image,
nullptr, 0, renderers[0]);
755 fprintf(stderr,
"Error during processing.\n");
struct TessTextRenderer TessTextRenderer
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
PageSegMode GetPageSegMode() const
static bool IsAVX2Available()
Orientation and script detection only.
int main(int argc, char **argv)
static bool IsAVXAvailable()
static bool IsSSEAvailable()
bool GetBoolVariable(const char *name, bool *value) const
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
const char * GetDatapath()
Automatic page segmentation, but no OSD, or OCR.
Find as much text as possible in no particular order.
static const char * Version()
struct TessUnlvRenderer TessUnlvRenderer
void SetPageSegMode(PageSegMode mode)
void PrintVariables(FILE *fp) const
DLLSYM void tprintf(const char *format,...)
struct TessHOcrRenderer TessHOcrRenderer
Fully automatic page segmentation, but no OSD.
int direction(EDGEPT *point)
static bool IsAVX512FAvailable()
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
struct TessPDFRenderer TessPDFRenderer
static TESS_API DawgCache * GlobalDawgCache()
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
bool SetVariable(const char *name, const char *value)
Assume a single uniform block of text. (Default.)
PageIterator * AnalyseLayout()
struct TessBoxTextRenderer TessBoxTextRenderer
static bool IsAVX512BWAvailable()
static TESS_API void Update()
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)
Sparse text with orientation and script det.
void SetOutputName(const char *name)