27 #include "allheaders.h"
36 #if defined(HAVE_TIFFIO_H) && defined(_WIN32)
40 static void Win32WarningHandler(
const char* module,
const char* fmt,
43 fprintf(stderr,
"%s: ", module);
45 fprintf(stderr,
"Warning, ");
46 vfprintf(stderr, fmt, ap);
47 fprintf(stderr,
".\n");
57 versionStrP = getLeptonicaVersion();
58 printf(
" %s\n", versionStrP);
59 lept_free(versionStrP);
61 versionStrP = getImagelibVersions();
62 printf(
" %s\n", versionStrP);
63 lept_free(versionStrP);
66 cl_platform_id platform;
67 cl_uint num_platforms;
68 cl_device_id devices[2];
73 printf(
" OpenCL info:\n");
74 clGetPlatformIDs(1, &platform, &num_platforms);
75 printf(
" Found %d platforms.\n", num_platforms);
76 clGetPlatformInfo(platform, CL_PLATFORM_NAME, 256, info, 0);
77 printf(
" Platform name: %s.\n", info);
78 clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 256, info, 0);
79 printf(
" Version: %s.\n", info);
80 clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 2, devices, &num_devices);
81 printf(
" Found %d devices.\n", num_devices);
82 for (i = 0; i < num_devices; ++i) {
83 clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0);
84 printf(
" Device %d name: %s.\n", i + 1, info);
92 " %s --help | --help-psm | --help-oem | --version\n"
93 " %s --list-langs [--tessdata-dir PATH]\n"
94 " %s --print-parameters [options...] [configfile...]\n"
95 " %s imagename|stdin outputbase|stdout [options...] [configfile...]\n",
96 program, program, program, program);
101 "Page segmentation modes:\n"
102 " 0 Orientation and script detection (OSD) only.\n"
103 " 1 Automatic page segmentation with OSD.\n"
104 " 2 Automatic page segmentation, but no OSD, or OCR.\n"
105 " 3 Fully automatic page segmentation, but no OSD. (Default)\n"
106 " 4 Assume a single column of text of variable sizes.\n"
107 " 5 Assume a single uniform block of vertically aligned text.\n"
108 " 6 Assume a single uniform block of text.\n"
109 " 7 Treat the image as a single text line.\n"
110 " 8 Treat the image as a single word.\n"
111 " 9 Treat the image as a single word in a circle.\n"
112 " 10 Treat the image as a single character.\n"
113 " 11 Sparse text. Find as much text as possible in no"
114 " particular order.\n"
115 " 12 Sparse text with OSD.\n"
116 " 13 Raw line. Treat the image as a single text line,\n"
117 "\t\t\tbypassing hacks that are Tesseract-specific.\n";
124 "OCR Engine modes:\n"
125 " 0 Original Tesseract only.\n"
127 " 2 Tesseract + cube.\n"
128 " 3 Default, based on what is available.\n";
136 const char* ocr_options =
138 " --tessdata-dir PATH Specify the location of tessdata path.\n"
139 " --user-words PATH Specify the location of user words file.\n"
140 " --user-patterns PATH Specify the location of user patterns file.\n"
141 " -l LANG[+LANG] Specify language(s) used for OCR.\n"
142 " -c VAR=VALUE Set value for config variables.\n"
143 " Multiple -c arguments are allowed.\n"
144 " --psm NUM Specify page segmentation mode.\n"
145 " --oem NUM Specify OCR Engine mode.\n"
146 "NOTE: These options must occur before any configfile.\n";
148 printf(
"\n%s\n", ocr_options);
152 const char* single_options =
154 " -h, --help Show this help message.\n"
155 " --help-psm Show page segmentation modes.\n"
156 " --help-oem Show OCR Engine modes.\n"
157 " -v, --version Show version information.\n"
158 " --list-langs List available languages for tesseract engine.\n"
159 " --print-parameters Print tesseract parameters to stdout.\n";
161 printf(
"\n%s", single_options);
166 char opt1[256], opt2[255];
167 for (
int i = 0; i < argc; i++) {
168 if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
169 strncpy(opt1, argv[i + 1], 255);
171 char* p = strchr(opt1,
'=');
173 fprintf(stderr,
"Missing = in configvar assignment\n");
177 strncpy(opt2, strchr(argv[i + 1],
'=') + 1, 255);
182 fprintf(stderr,
"Could not set option: %s=%s\n", opt1, opt2);
191 printf(
"List of available languages (%d):\n", languages.
size());
192 for (
int index = 0; index < languages.
size(); ++index) {
193 STRING&
string = languages[index];
194 printf(
"%s\n",
string.
string());
200 tprintf(
"Tesseract Open Source OCR Engine v%s with Leptonica\n",
226 const char** image,
const char** outputbase,
227 const char** datapath,
bool* list_langs,
bool* print_parameters,
238 if ((strcmp(argv[1],
"-h") == 0) || (strcmp(argv[1],
"--help") == 0)) {
242 if ((strcmp(argv[1],
"--help-psm") == 0)) {
246 if ((strcmp(argv[1],
"--help-oem") == 0)) {
250 if ((strcmp(argv[1],
"-v") == 0) || (strcmp(argv[1],
"--version") == 0)) {
258 while (i < argc && (*outputbase == NULL || argv[i][0] ==
'-')) {
259 if (strcmp(argv[i],
"-l") == 0 && i + 1 < argc) {
262 }
else if (strcmp(argv[i],
"--tessdata-dir") == 0 && i + 1 < argc) {
263 *datapath = argv[i + 1];
265 }
else if (strcmp(argv[i],
"--user-words") == 0 && i + 1 < argc) {
269 }
else if (strcmp(argv[i],
"--user-patterns") == 0 && i + 1 < argc) {
270 vars_vec->
push_back(
"user_patterns_file");
273 }
else if (strcmp(argv[i],
"--list-langs") == 0) {
276 }
else if (strcmp(argv[i],
"-psm") == 0 && i + 1 < argc) {
281 }
else if (strcmp(argv[i],
"--psm") == 0 && i + 1 < argc) {
284 }
else if (strcmp(argv[i],
"--oem") == 0 && i + 1 < argc) {
287 }
else if (strcmp(argv[i],
"--print-parameters") == 0) {
289 *print_parameters =
true;
290 }
else if (strcmp(argv[i],
"-c") == 0 && i + 1 < argc) {
293 }
else if (*image == NULL) {
295 }
else if (*outputbase == NULL) {
296 *outputbase = argv[i];
303 if (argc == 2 && strcmp(argv[1],
"--list-langs") == 0) {
308 if (*outputbase == NULL && noocr ==
false) {
355 if (b || renderers->
empty()) {
360 if (!renderers->
empty()) {
363 for (
int r = 1; r < renderers->
size(); ++r) {
364 (*renderers)[0]->insert((*renderers)[r]);
365 (*renderers)[r] = NULL;
375 int main(
int argc,
char** argv) {
376 const char*
lang =
"eng";
377 const char* image = NULL;
378 const char* outputbase = NULL;
379 const char* datapath = NULL;
380 bool list_langs =
false;
381 bool print_parameters =
false;
393 setMsgSeverity(L_SEVERITY_WARNING);
396 #if defined(HAVE_TIFFIO_H) && defined(_WIN32)
398 TIFFSetWarningHandler(Win32WarningHandler);
401 ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &list_langs,
402 &print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode,
406 if (outputbase != NULL && strcmp(outputbase,
"-") &&
407 strcmp(outputbase,
"stdout")) {
416 int init_failed = api.
Init(datapath, lang, enginemode, &(argv[arg_i]),
417 argc - arg_i, &vars_vec, &vars_values,
false);
419 fprintf(stderr,
"Could not initialize tesseract.\n");
430 if (print_parameters) {
432 fprintf(stdout,
"Tesseract parameters:\n");
443 Pix* pixs = pixRead(image);
445 fprintf(stderr,
"Cannot open input file: %s\n", image);
458 it->
Orientation(&orientation, &direction, &order, &deskew_angle);
460 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
461 "Deskew angle: %.4f\n",
462 orientation, direction, order, deskew_angle);
476 bool in_training_mode =
483 if (in_training_mode) {
489 if (!renderers.
empty()) {
491 bool succeed = api.
ProcessPages(image, NULL, 0, renderers[0]);
493 fprintf(stderr,
"Error during processing.\n");
void PrintLangsList(tesseract::TessBaseAPI *api)
void SetVariablesFromCLArgs(tesseract::TessBaseAPI *api, int argc, char **argv)
Orientation and script detection only.
PageSegMode GetPageSegMode() const
Assume a single uniform block of text. (Default.)
void PrintHelpMessage(const char *program)
void PreloadRenderers(tesseract::TessBaseAPI *api, tesseract::PointerVector< tesseract::TessResultRenderer > *renderers, tesseract::PageSegMode pagesegmode, const char *outputbase)
int main(int argc, char **argv)
void SetPageSegMode(PageSegMode mode)
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)
int direction(EDGEPT *point)
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
PageIterator * AnalyseLayout()
static const char * Version()
#define PERF_COUNT_START(FUNCT_NAME)
const char * GetDatapath()
Automatic page segmentation, but no OSD, or OCR.
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
void SetOutputName(const char *name)
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
void FixPageSegMode(tesseract::TessBaseAPI *api, tesseract::PageSegMode pagesegmode)
void PrintVariables(FILE *fp) 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)
Fully automatic page segmentation, but no OSD.
bool SetVariable(const char *name, const char *value)
bool GetBoolVariable(const char *name, bool *value) const