27 #include "allheaders.h" 37 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 41 static void Win32WarningHandler(
const char* module,
const char* fmt,
44 fprintf(stderr,
"%s: ", module);
46 fprintf(stderr,
"Warning, ");
47 vfprintf(stderr, fmt, ap);
48 fprintf(stderr,
".\n");
58 versionStrP = getLeptonicaVersion();
59 printf(
" %s\n", versionStrP);
60 lept_free(versionStrP);
62 versionStrP = getImagelibVersions();
63 printf(
" %s\n", versionStrP);
64 lept_free(versionStrP);
67 cl_platform_id platform[4];
68 cl_uint num_platforms;
70 printf(
" OpenCL info:\n");
71 if (clGetPlatformIDs(4, platform, &num_platforms) == CL_SUCCESS) {
72 printf(
" Found %u platform(s).\n", num_platforms);
73 for (
unsigned n = 0; n < num_platforms; n++) {
75 if (clGetPlatformInfo(platform[n], CL_PLATFORM_NAME, 256, info, 0) ==
77 printf(
" Platform %u name: %s.\n", n + 1, info);
79 if (clGetPlatformInfo(platform[n], CL_PLATFORM_VERSION, 256, info, 0) ==
81 printf(
" Version: %s.\n", info);
83 cl_device_id devices[2];
85 if (clGetDeviceIDs(platform[n], CL_DEVICE_TYPE_ALL, 2, devices,
86 &num_devices) == CL_SUCCESS) {
87 printf(
" Found %u device(s).\n", num_devices);
88 for (
unsigned i = 0; i < num_devices; ++i) {
89 if (clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0) ==
91 printf(
" Device %u name: %s.\n", i + 1, info);
103 " %s --help | --help-psm | --help-oem | --version\n" 104 " %s --list-langs [--tessdata-dir PATH]\n" 105 " %s --print-parameters [options...] [configfile...]\n" 106 " %s imagename|stdin outputbase|stdout [options...] [configfile...]\n",
107 program, program, program, program);
112 "Page segmentation modes:\n" 113 " 0 Orientation and script detection (OSD) only.\n" 114 " 1 Automatic page segmentation with OSD.\n" 115 " 2 Automatic page segmentation, but no OSD, or OCR.\n" 116 " 3 Fully automatic page segmentation, but no OSD. (Default)\n" 117 " 4 Assume a single column of text of variable sizes.\n" 118 " 5 Assume a single uniform block of vertically aligned text.\n" 119 " 6 Assume a single uniform block of text.\n" 120 " 7 Treat the image as a single text line.\n" 121 " 8 Treat the image as a single word.\n" 122 " 9 Treat the image as a single word in a circle.\n" 123 " 10 Treat the image as a single character.\n" 124 " 11 Sparse text. Find as much text as possible in no" 125 " particular order.\n" 126 " 12 Sparse text with OSD.\n" 127 " 13 Raw line. Treat the image as a single text line,\n" 128 "\t\t\tbypassing hacks that are Tesseract-specific.\n";
135 "OCR Engine modes:\n" 136 " 0 Original Tesseract only.\n" 138 " 2 Tesseract + cube.\n" 139 " 3 Default, based on what is available.\n";
147 const char* ocr_options =
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 printf(
"\n%s\n", ocr_options);
163 const char* single_options =
165 " -h, --help Show this help message.\n" 166 " --help-psm Show page segmentation modes.\n" 167 " --help-oem Show OCR Engine modes.\n" 168 " -v, --version Show version information.\n" 169 " --list-langs List available languages for tesseract engine.\n" 170 " --print-parameters Print tesseract parameters to stdout.\n";
172 printf(
"\n%s", single_options);
177 char opt1[256], opt2[255];
178 for (
int i = 0; i < argc; i++) {
179 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
180 strncpy(opt1, argv[i + 1], 255);
182 char* p = strchr(opt1,
'=');
184 fprintf(stderr,
"Missing = in configvar assignment\n");
188 strncpy(opt2, strchr(argv[i + 1],
'=') + 1, 255);
192 if (!
api->SetVariable(opt1, opt2)) {
193 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
201 api->GetAvailableLanguagesAsVector(&languages);
202 printf(
"List of available languages (%d):\n", languages.
size());
203 for (
int index = 0; index < languages.
size(); ++index) {
204 STRING&
string = languages[index];
205 printf(
"%s\n",
string.
string());
211 tprintf(
"Tesseract Open Source OCR Engine v%s with Leptonica\n",
232 api->SetPageSegMode(pagesegmode);
237 const char** image,
const char** outputbase,
238 const char** datapath,
bool* list_langs,
bool* print_parameters,
249 if ((strcmp(argv[1],
"-h") == 0) || (strcmp(argv[1],
"--help") == 0)) {
253 if ((strcmp(argv[1],
"--help-psm") == 0)) {
257 if ((strcmp(argv[1],
"--help-oem") == 0)) {
261 if ((strcmp(argv[1],
"-v") == 0) || (strcmp(argv[1],
"--version") == 0)) {
269 while (i < argc && (*outputbase == NULL || argv[i][0] ==
'-')) {
270 if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
273 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
274 *datapath = argv[i + 1];
276 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
280 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
281 vars_vec->
push_back(
"user_patterns_file");
284 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
287 }
else if (strcmp(argv[i],
"-psm") == 0 && i + 1 < argc) {
292 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
295 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
298 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
300 *print_parameters =
true;
301 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
304 }
else if (*image == NULL) {
306 }
else if (*outputbase == NULL) {
307 *outputbase = argv[i];
314 if (argc == 2 && strcmp(argv[1],
"--list-langs") == 0) {
319 if (*outputbase == NULL && noocr ==
false) {
333 api->GetBoolVariable(
"tessedit_create_hocr", &b);
336 api->GetBoolVariable(
"hocr_font_info", &font_info);
341 api->GetBoolVariable(
"tessedit_create_tsv", &b);
344 api->GetBoolVariable(
"hocr_font_info", &font_info);
349 api->GetBoolVariable(
"tessedit_create_pdf", &b);
352 api->GetBoolVariable(
"textonly_pdf", &textonly);
354 outputbase,
api->GetDatapath(), textonly));
357 api->GetBoolVariable(
"tessedit_write_unlv", &b);
362 api->GetBoolVariable(
"tessedit_create_boxfile", &b);
367 api->GetBoolVariable(
"tessedit_create_txt", &b);
368 if (b || renderers->
empty()) {
373 if (!renderers->
empty()) {
376 for (
int r = 1; r < renderers->
size(); ++r) {
377 (*renderers)[0]->insert((*renderers)[r]);
378 (*renderers)[r] = NULL;
388 int main(
int argc,
char** argv) {
389 const char*
lang =
"eng";
390 const char* image = NULL;
391 const char* outputbase = NULL;
392 const char* datapath = NULL;
393 bool list_langs =
false;
394 bool print_parameters =
false;
406 setMsgSeverity(L_SEVERITY_ERROR);
409 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 411 TIFFSetWarningHandler(Win32WarningHandler);
414 ParseArgs(argc, argv, &
lang, &image, &outputbase, &datapath, &list_langs,
415 &print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode,
419 if (outputbase != NULL && strcmp(outputbase,
"-") &&
420 strcmp(outputbase,
"stdout")) {
434 api.SetOutputName(outputbase);
436 int init_failed =
api.Init(datapath,
lang, enginemode, &(argv[arg_i]),
437 argc - arg_i, &vars_vec, &vars_values,
false);
439 fprintf(stderr,
"Could not initialize tesseract.\n");
450 if (print_parameters) {
452 fprintf(stdout,
"Tesseract parameters:\n");
453 api.PrintVariables(fout);
461 int ret_val = EXIT_SUCCESS;
463 Pix* pixs = pixRead(image);
465 fprintf(stderr,
"Cannot open input file: %s\n", image);
480 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" 481 "Deskew angle: %.4f\n",
482 orientation,
direction, order, deskew_angle);
484 ret_val = EXIT_FAILURE;
496 bool in_training_mode =
497 (
api.GetBoolVariable(
"tessedit_ambigs_training", &b) && b) ||
498 (
api.GetBoolVariable(
"tessedit_resegment_from_boxes", &b) && b) ||
499 (
api.GetBoolVariable(
"tessedit_make_boxes_from_boxes", &b) && b);
504 if (in_training_mode) {
510 if (!renderers.
empty()) {
512 bool succeed =
api.ProcessPages(image, NULL, 0, renderers[0]);
514 fprintf(stderr,
"Error during processing.\n");
void PrintLangsList(tesseract::TessBaseAPI *api)
void PreloadRenderers(tesseract::TessBaseAPI *api, tesseract::PointerVector< tesseract::TessResultRenderer > *renderers, tesseract::PageSegMode pagesegmode, const char *outputbase)
Orientation and script detection only.
void SetVariablesFromCLArgs(tesseract::TessBaseAPI *api, int argc, char **argv)
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
Automatic page segmentation, but no OSD, or OCR.
void FixPageSegMode(tesseract::TessBaseAPI *api, tesseract::PageSegMode pagesegmode)
void PrintHelpMessage(const char *program)
int main(int argc, char **argv)
#define PERF_COUNT_START(FUNCT_NAME)
int direction(EDGEPT *point)
Fully automatic page segmentation, but no OSD.
Assume a single uniform block of text. (Default.)
static DawgCache * GlobalDawgCache()
static const char * Version()
void PrintUsage(const char *program)
void ParseArgs(const int argc, char **argv, const char **lang, const char **image, const char **outputbase, const char **datapath, bool *list_langs, bool *print_parameters, GenericVector< STRING > *vars_vec, GenericVector< STRING > *vars_values, int *arg_i, tesseract::PageSegMode *pagesegmode, tesseract::OcrEngineMode *enginemode)