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");
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 " %s --help | --help-psm | --help-oem | --version\n" 107 " %s --list-langs [--tessdata-dir PATH]\n" 108 " %s --print-parameters [options...] [configfile...]\n" 109 " %s imagename|stdin outputbase|stdout [options...] [configfile...]\n",
110 program, program, program, program);
115 "Page segmentation modes:\n" 116 " 0 Orientation and script detection (OSD) only.\n" 117 " 1 Automatic page segmentation with OSD.\n" 118 " 2 Automatic page segmentation, but no OSD, or OCR.\n" 119 " 3 Fully automatic page segmentation, but no OSD. (Default)\n" 120 " 4 Assume a single column of text of variable sizes.\n" 121 " 5 Assume a single uniform block of vertically aligned text.\n" 122 " 6 Assume a single uniform block of text.\n" 123 " 7 Treat the image as a single text line.\n" 124 " 8 Treat the image as a single word.\n" 125 " 9 Treat the image as a single word in a circle.\n" 126 " 10 Treat the image as a single character.\n" 127 " 11 Sparse text. Find as much text as possible in no" 128 " particular order.\n" 129 " 12 Sparse text with OSD.\n" 130 " 13 Raw line. Treat the image as a single text line,\n" 131 "\t\t\tbypassing hacks that are Tesseract-specific.\n";
138 "OCR Engine modes:\n" 139 " 0 Original Tesseract only.\n" 140 " 1 Neural nets LSTM only.\n" 141 " 2 Tesseract + LSTM.\n" 142 " 3 Default, based on what is available.\n";
150 const char* ocr_options =
152 " --tessdata-dir PATH Specify the location of tessdata path.\n" 153 " --user-words PATH Specify the location of user words file.\n" 154 " --user-patterns PATH Specify the location of user patterns file.\n" 155 " -l LANG[+LANG] Specify language(s) used for OCR.\n" 156 " -c VAR=VALUE Set value for config variables.\n" 157 " Multiple -c arguments are allowed.\n" 158 " --psm NUM Specify page segmentation mode.\n" 159 " --oem NUM Specify OCR Engine mode.\n" 160 "NOTE: These options must occur before any configfile.\n";
162 printf(
"\n%s\n", ocr_options);
166 const char* single_options =
168 " -h, --help Show this help message.\n" 169 " --help-psm Show page segmentation modes.\n" 170 " --help-oem Show OCR Engine modes.\n" 171 " -v, --version Show version information.\n" 172 " --list-langs List available languages for tesseract engine.\n" 173 " --print-parameters Print tesseract parameters.\n";
175 printf(
"\n%s", single_options);
180 char opt1[256], opt2[255];
181 for (
int i = 0; i < argc; i++) {
182 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
183 strncpy(opt1, argv[i + 1], 255);
185 char* p = strchr(opt1,
'=');
187 fprintf(stderr,
"Missing = in configvar assignment\n");
191 strncpy(opt2, strchr(argv[i + 1],
'=') + 1, 255);
196 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
205 printf(
"List of available languages (%d):\n", languages.
size());
206 for (
int index = 0; index < languages.
size(); ++index) {
207 STRING&
string = languages[index];
208 printf(
"%s\n",
string.
string());
214 tprintf(
"Tesseract Open Source OCR Engine v%s with Leptonica\n",
240 const char** image,
const char** outputbase,
241 const char** datapath,
bool* list_langs,
bool* print_parameters,
252 if ((strcmp(argv[1],
"-h") == 0) || (strcmp(argv[1],
"--help") == 0)) {
256 if ((strcmp(argv[1],
"--help-psm") == 0)) {
260 if ((strcmp(argv[1],
"--help-oem") == 0)) {
264 if ((strcmp(argv[1],
"-v") == 0) || (strcmp(argv[1],
"--version") == 0)) {
272 while (i < argc && (*outputbase == NULL || argv[i][0] ==
'-')) {
273 if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
276 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
277 *datapath = argv[i + 1];
279 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
283 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
284 vars_vec->
push_back(
"user_patterns_file");
287 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
290 }
else if (strcmp(argv[i],
"-psm") == 0 && i + 1 < argc) {
295 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
298 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
301 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
303 *print_parameters =
true;
304 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
307 }
else if (*image == NULL) {
309 }
else if (*outputbase == NULL) {
310 *outputbase = argv[i];
317 if (argc == 2 && strcmp(argv[1],
"--list-langs") == 0) {
322 if (*outputbase == NULL && noocr ==
false) {
371 if (b || renderers->
empty()) {
376 if (!renderers->
empty()) {
379 for (
int r = 1; r < renderers->
size(); ++r) {
380 (*renderers)[0]->insert((*renderers)[r]);
381 (*renderers)[r] = NULL;
391 int main(
int argc,
char** argv) {
392 const char*
lang =
"eng";
393 const char* image = NULL;
394 const char* outputbase = NULL;
395 const char* datapath = NULL;
396 bool list_langs =
false;
397 bool print_parameters =
false;
409 setMsgSeverity(L_SEVERITY_ERROR);
412 #if defined(HAVE_TIFFIO_H) && defined(_WIN32) 414 TIFFSetWarningHandler(Win32WarningHandler);
417 ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &list_langs,
418 &print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode,
422 if (outputbase != NULL && strcmp(outputbase,
"-") &&
423 strcmp(outputbase,
"stdout")) {
439 int init_failed = api.
Init(datapath, lang, enginemode, &(argv[arg_i]),
440 argc - arg_i, &vars_vec, &vars_values,
false);
442 fprintf(stderr,
"Could not initialize tesseract.\n");
453 if (print_parameters) {
455 fprintf(stdout,
"Tesseract parameters:\n");
464 int ret_val = EXIT_SUCCESS;
466 Pix* pixs = pixRead(image);
468 fprintf(stderr,
"Cannot open input file: %s\n", image);
481 it->
Orientation(&orientation, &direction, &order, &deskew_angle);
483 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" 484 "Deskew angle: %.4f\n",
485 orientation, direction, order, deskew_angle);
487 ret_val = EXIT_FAILURE;
499 bool in_training_mode =
507 if (in_training_mode) {
513 if (!renderers.
empty()) {
515 bool succeed = api.
ProcessPages(image, NULL, 0, renderers[0]);
517 fprintf(stderr,
"Error during processing.\n");
void PrintLangsList(tesseract::TessBaseAPI *api)
#define PERF_COUNT_START(FUNCT_NAME)
static const char * Version()
void PreloadRenderers(tesseract::TessBaseAPI *api, tesseract::PointerVector< tesseract::TessResultRenderer > *renderers, tesseract::PageSegMode pagesegmode, const char *outputbase)
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 SetVariablesFromCLArgs(tesseract::TessBaseAPI *api, int argc, char **argv)
bool SetVariable(const char *name, const char *value)
Assume a single uniform block of text. (Default.)
int direction(EDGEPT *point)
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
PageIterator * AnalyseLayout()
static bool IsAVXAvailable()
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
bool GetBoolVariable(const char *name, bool *value) const
Automatic page segmentation, but no OSD, or OCR.
void FixPageSegMode(tesseract::TessBaseAPI *api, tesseract::PageSegMode pagesegmode)
void PrintHelpMessage(const char *program)
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
int main(int argc, char **argv)
void SetPageSegMode(PageSegMode mode)
void SetOutputName(const char *name)
PageSegMode GetPageSegMode() const
void PrintVariables(FILE *fp) const
const char * GetDatapath()
Fully automatic page segmentation, but no OSD.
static DawgCache * GlobalDawgCache()
static bool IsSSEAvailable()
Orientation and script detection only.
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
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)