22 #include "config_auto.h" 27 #include "allheaders.h" 38 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 42 static void Win32WarningHandler(
const char* module,
const char* fmt,
45 fprintf(stderr,
"%s: ", module);
47 fprintf(stderr,
"Warning, ");
48 vfprintf(stderr, fmt, ap);
49 fprintf(stderr,
".\n");
54 static void PrintVersionInfo() {
59 versionStrP = getLeptonicaVersion();
60 printf(
" %s\n", versionStrP);
61 lept_free(versionStrP);
63 versionStrP = getImagelibVersions();
64 printf(
" %s\n", versionStrP);
65 lept_free(versionStrP);
68 cl_platform_id platform[4];
69 cl_uint num_platforms;
71 printf(
" OpenCL info:\n");
72 if (clGetPlatformIDs(4, platform, &num_platforms) == CL_SUCCESS) {
73 printf(
" Found %u platform(s).\n", num_platforms);
74 for (
unsigned n = 0; n < num_platforms; n++) {
76 if (clGetPlatformInfo(platform[n], CL_PLATFORM_NAME, 256, info, 0) ==
78 printf(
" Platform %u name: %s.\n", n + 1, info);
80 if (clGetPlatformInfo(platform[n], CL_PLATFORM_VERSION, 256, info, 0) ==
82 printf(
" Version: %s.\n", info);
84 cl_device_id devices[2];
86 if (clGetDeviceIDs(platform[n], CL_DEVICE_TYPE_ALL, 2, devices,
87 &num_devices) == CL_SUCCESS) {
88 printf(
" Found %u device(s).\n", num_devices);
89 for (
unsigned i = 0; i < num_devices; ++i) {
90 if (clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0) ==
92 printf(
" Device %u name: %s.\n", i + 1, info);
106 static void PrintHelpForPSM() {
108 "Page segmentation modes:\n" 109 " 0 Orientation and script detection (OSD) only.\n" 110 " 1 Automatic page segmentation with OSD.\n" 111 " 2 Automatic page segmentation, but no OSD, or OCR.\n" 112 " 3 Fully automatic page segmentation, but no OSD. (Default)\n" 113 " 4 Assume a single column of text of variable sizes.\n" 114 " 5 Assume a single uniform block of vertically aligned text.\n" 115 " 6 Assume a single uniform block of text.\n" 116 " 7 Treat the image as a single text line.\n" 117 " 8 Treat the image as a single word.\n" 118 " 9 Treat the image as a single word in a circle.\n" 119 " 10 Treat the image as a single character.\n" 120 " 11 Sparse text. Find as much text as possible in no" 121 " particular order.\n" 122 " 12 Sparse text with OSD.\n" 123 " 13 Raw line. Treat the image as a single text line,\n" 124 " bypassing hacks that are Tesseract-specific.\n";
129 static void PrintHelpForOEM() {
131 "OCR Engine modes:\n" 132 " 0 Legacy engine only.\n" 133 " 1 Neural nets LSTM engine only.\n" 134 " 2 Legacy + LSTM engines.\n" 135 " 3 Default, based on what is available.\n";
140 static void PrintHelpExtra(
const char* program) {
143 " %s --help | --help-extra | --help-psm | --help-oem | --version\n" 144 " %s --list-langs [--tessdata-dir PATH]\n" 145 " %s --print-parameters [options...] [configfile...]\n" 146 " %s imagename|imagelist|stdin outputbase|stdout [options...] [configfile...]\n" 149 " --tessdata-dir PATH Specify the location of tessdata path.\n" 150 " --user-words PATH Specify the location of user words file.\n" 151 " --user-patterns PATH Specify the location of user patterns file.\n" 152 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 153 " -c VAR=VALUE Set value for config variables.\n" 154 " Multiple -c arguments are allowed.\n" 155 " --psm NUM Specify page segmentation mode.\n" 156 " --oem NUM Specify OCR Engine mode.\n" 157 "NOTE: These options must occur before any configfile.\n" 159 program, program, program, program
169 " -h, --help Show minimal help message.\n" 170 " --help-extra Show extra help for advanced users.\n" 171 " --help-psm Show page segmentation modes.\n" 172 " --help-oem Show OCR Engine modes.\n" 173 " -v, --version Show version information.\n" 174 " --list-langs List available languages for tesseract engine.\n" 175 " --print-parameters Print tesseract parameters.\n" 179 static void PrintHelpMessage(
const char* program) {
182 " %s --help | --help-extra | --version\n" 184 " %s imagename outputbase [options...] [configfile...]\n" 187 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 188 "NOTE: These options must occur before any configfile.\n" 191 " --help Show this help message.\n" 192 " --help-extra Show extra help for advanced users.\n" 193 " --version Show version information.\n" 194 " --list-langs List available languages for tesseract engine.\n",
195 program, program, program
201 char opt1[256], opt2[255];
202 for (
int i = 0; i < argc; i++) {
203 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
204 strncpy(opt1, argv[i + 1], 255);
206 char* p = strchr(opt1,
'=');
208 fprintf(stderr,
"Missing = in configvar assignment\n");
212 strncpy(opt2, strchr(argv[i + 1],
'=') + 1, 255);
217 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
226 printf(
"List of available languages (%d):\n", languages.
size());
227 for (
int index = 0; index < languages.
size(); ++index) {
228 STRING&
string = languages[index];
229 printf(
"%s\n",
string.
string());
234 static void PrintBanner() {
235 tprintf(
"Tesseract Open Source OCR Engine v%s with Leptonica\n",
259 static void checkArgValues(
int arg,
const char*
mode,
int count) {
260 if (arg >= count || arg < 0) {
261 printf(
"Invalid %s value, please enter a number between 0-%d\n", mode, count - 1);
267 static void ParseArgs(
const int argc,
char** argv,
const char**
lang,
268 const char** image,
const char** outputbase,
269 const char** datapath,
270 bool* list_langs,
bool* print_parameters,
276 PrintHelpMessage(argv[0]);
281 if ((strcmp(argv[1],
"-h") == 0) || (strcmp(argv[1],
"--help") == 0)) {
282 PrintHelpMessage(argv[0]);
285 if (strcmp(argv[1],
"--help-extra") == 0) {
286 PrintHelpExtra(argv[0]);
289 if ((strcmp(argv[1],
"--help-psm") == 0)) {
293 if ((strcmp(argv[1],
"--help-oem") == 0)) {
297 if ((strcmp(argv[1],
"-v") == 0) || (strcmp(argv[1],
"--version") == 0)) {
305 while (i < argc && (*outputbase == NULL || argv[i][0] ==
'-')) {
306 if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
309 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
310 *datapath = argv[i + 1];
312 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
316 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
317 vars_vec->
push_back(
"user_patterns_file");
320 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
323 }
else if (strcmp(argv[i],
"-psm") == 0 && i + 1 < argc) {
329 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
333 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
334 int oem = atoi(argv[i + 1]);
338 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
340 *print_parameters =
true;
341 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
344 }
else if (*image == NULL) {
346 }
else if (*outputbase == NULL) {
347 *outputbase = argv[i];
354 if (argc == 2 && strcmp(argv[1],
"--list-langs") == 0) {
359 if (*outputbase == NULL && noocr ==
false) {
360 PrintHelpMessage(argv[0]);
365 static void PreloadRenderers(
408 if (b || renderers->
empty()) {
413 if (!renderers->
empty()) {
416 for (
int r = 1; r < renderers->
size(); ++r) {
417 (*renderers)[0]->insert((*renderers)[r]);
418 (*renderers)[r] = NULL;
428 int main(
int argc,
char** argv) {
429 const char* lang =
"eng";
430 const char* image = NULL;
431 const char* outputbase = NULL;
432 const char* datapath = NULL;
433 bool list_langs =
false;
434 bool print_parameters =
false;
446 setMsgSeverity(L_SEVERITY_ERROR);
449 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 451 TIFFSetWarningHandler(Win32WarningHandler);
454 ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &list_langs,
455 &print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode,
459 if (outputbase != NULL && strcmp(outputbase,
"-") &&
460 strcmp(outputbase,
"stdout")) {
476 int init_failed = api.
Init(datapath, lang, enginemode, &(argv[arg_i]),
477 argc - arg_i, &vars_vec, &vars_values,
false);
479 SetVariablesFromCLArgs(&api, argc, argv);
482 PrintLangsList(&api);
487 fprintf(stderr,
"Could not initialize tesseract.\n");
491 if (print_parameters) {
493 fprintf(stdout,
"Tesseract parameters:\n");
499 FixPageSegMode(&api, pagesegmode);
502 int ret_val = EXIT_SUCCESS;
504 Pix* pixs = pixRead(image);
506 fprintf(stderr,
"Cannot open input file: %s\n", image);
519 it->
Orientation(&orientation, &direction, &order, &deskew_angle);
521 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" 522 "Deskew angle: %.4f\n",
523 orientation, direction, order, deskew_angle);
525 ret_val = EXIT_FAILURE;
537 bool in_training_mode =
545 if (in_training_mode) {
548 PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
551 if (!renderers.
empty()) {
552 if (banner) PrintBanner();
553 bool succeed = api.
ProcessPages(image, NULL, 0, renderers[0]);
555 fprintf(stderr,
"Error during processing.\n");
static bool IsSSEAvailable()
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
static bool IsAVX512FAvailable()
Assume a single uniform block of text. (Default.)
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)
void PrintVariables(FILE *fp) const
void SetPageSegMode(PageSegMode mode)
const char * GetDatapath()
int direction(EDGEPT *point)
PageSegMode GetPageSegMode() const
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
static DawgCache * GlobalDawgCache()
int main(int argc, char **argv)
#define PERF_COUNT_START(FUNCT_NAME)
static bool IsAVX512BWAvailable()
Automatic page segmentation, but no OSD, or OCR.
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
void SetOutputName(const char *name)
static const char * Version()
bool GetBoolVariable(const char *name, bool *value) const
static bool IsAVX2Available()
PageIterator * AnalyseLayout()
static bool IsAVXAvailable()
bool SetVariable(const char *name, const char *value)
Fully automatic page segmentation, but no OSD.
Orientation and script detection only.