tesseract  3.04.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseractmain.cpp File Reference
#include <iostream>
#include "allheaders.h"
#include "baseapi.h"
#include "basedir.h"
#include "renderer.h"
#include "strngs.h"
#include "tprintf.h"
#include "openclwrapper.h"
#include "osdetect.h"

Go to the source code of this file.

Functions

void PrintVersionInfo ()
 
void PrintUsage (const char *program)
 
void PrintHelpForPSM ()
 
void PrintHelpMessage (const char *program)
 
void SetVariablesFromCLArgs (tesseract::TessBaseAPI *api, int argc, char **argv)
 
void PrintLangsList (tesseract::TessBaseAPI *api)
 
void FixPageSegMode (tesseract::TessBaseAPI *api, tesseract::PageSegMode pagesegmode)
 
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)
 
void PreloadRenderers (tesseract::TessBaseAPI *api, tesseract::PointerVector< tesseract::TessResultRenderer > *renderers, tesseract::PageSegMode pagesegmode, const char *outputbase)
 
int main (int argc, char **argv)
 

Function Documentation

void FixPageSegMode ( tesseract::TessBaseAPI api,
tesseract::PageSegMode  pagesegmode 
)

We have 2 possible sources of pagesegmode: a config file and the command line. For backwards compatibility reasons, the default in tesseract is tesseract::PSM_SINGLE_BLOCK, but the default for this program is tesseract::PSM_AUTO. We will let the config file take priority, so the command-line default can take priority over the tesseract default, so we use the value from the command line only if the retrieved mode is still tesseract::PSM_SINGLE_BLOCK, indicating no change in any config file. Therefore the only way to force tesseract::PSM_SINGLE_BLOCK is from the command line. It would be simpler if we could set the value before Init, but that doesn't work.

Definition at line 190 of file tesseractmain.cpp.

191  {
193  api->SetPageSegMode(pagesegmode);
194 }
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:467
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:160
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:460
int main ( int  argc,
char **  argv 
)

This program reads in a text file consisting of feature samples from a training page in the following format:

   FontName UTF8-char-str xmin ymin xmax ymax page-number
    NumberOfFeatureTypes(N)
      FeatureTypeName1 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      FeatureTypeName2 NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
      ...
      FeatureTypeNameN NumberOfFeatures(M)
         Feature1
         ...
         FeatureM
   FontName CharName ...

The result of this program is a binary inttemp file used by the OCR engine.

Parameters
argcnumber of command line arguments
argvarray of command line arguments
Returns
none
Note
Exceptions: none
History: Fri Aug 18 08:56:17 1989, DSJ, Created.
History: Mon May 18 1998, Christy Russson, Revistion started.

Definition at line 338 of file tesseractmain.cpp.

338  {
339  const char* lang = "eng";
340  const char* image = NULL;
341  const char* outputbase = NULL;
342  const char* datapath = NULL;
343  bool list_langs = false;
344  bool print_parameters = false;
345  GenericVector<STRING> vars_vec, vars_values;
346  int arg_i = 1;
348 
349  ParseArgs(argc, argv,
350  &lang, &image, &outputbase, &datapath,
351  &list_langs, &print_parameters,
352  &vars_vec, &vars_values, &arg_i, &pagesegmode);
353 
354  PERF_COUNT_START("Tesseract:main")
356 
357  api.SetOutputName(outputbase);
358 
359  int init_failed = api.Init(datapath, lang, tesseract::OEM_DEFAULT,
360  &(argv[arg_i]), argc - arg_i, &vars_vec, &vars_values, false);
361  if (init_failed) {
362  fprintf(stderr, "Could not initialize tesseract.\n");
363  exit(1);
364  }
365 
366  SetVariablesFromCLArgs(&api, argc, argv);
367 
368  if (list_langs) {
370  exit(0);
371  }
372 
373  if (print_parameters) {
374  FILE* fout = stdout;
375  fprintf(stdout, "Tesseract parameters:\n");
376  api.PrintVariables(fout);
377  api.End();
378  exit(0);
379  }
380 
381  FixPageSegMode(&api, pagesegmode);
382 
383  if (pagesegmode == tesseract::PSM_AUTO_ONLY) {
384  int ret_val = 0;
385 
386  Pix* pixs = pixRead(image);
387  if (!pixs) {
388  fprintf(stderr, "Cannot open input file: %s\n", image);
389  exit(2);
390  }
391 
392  api.SetImage(pixs);
393 
394  tesseract::Orientation orientation;
397  float deskew_angle;
398 
399  tesseract::PageIterator* it = api.AnalyseLayout();
400  if (it) {
401  it->Orientation(&orientation, &direction, &order, &deskew_angle);
402  tprintf("Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n" \
403  "Deskew angle: %.4f\n",
404  orientation, direction, order, deskew_angle);
405  } else {
406  ret_val = 1;
407  }
408 
409  delete it;
410 
411  pixDestroy(&pixs);
412  exit(ret_val);
413  }
414 
415  // set in_training_mode to true when using one of these configs:
416  // ambigs.train, box.train, box.train.stderr, linebox, rebox
417  bool b = false;
418  bool in_training_mode =
419  (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) ||
420  (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) ||
421  (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b);
422 
424 
425  if (in_training_mode) {
426  renderers.push_back(NULL);
427  } else {
428  PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
429  }
430 
431  if (!renderers.empty()) {
432  bool succeed = api.ProcessPages(image, NULL, 0, renderers[0]);
433  if (!succeed) {
434  fprintf(stderr, "Error during processing.\n");
435  exit(1);
436  }
437  }
438 
440  return 0; // Normal exit
441 }
int push_back(T *object)
void PreloadRenderers(tesseract::TessBaseAPI *api, tesseract::PointerVector< tesseract::TessResultRenderer > *renderers, tesseract::PageSegMode pagesegmode, const char *outputbase)
#define tprintf(...)
Definition: tprintf.h:31
void FixPageSegMode(tesseract::TessBaseAPI *api, tesseract::PageSegMode pagesegmode)
#define PERF_COUNT_END
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:155
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
bool empty() const
Definition: genericvector.h:84
#define PERF_COUNT_START(FUNCT_NAME)
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:156
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
void PrintLangsList(tesseract::TessBaseAPI *api)
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)
struct TessBaseAPI TessBaseAPI
Definition: capi.h:69
void SetVariablesFromCLArgs(tesseract::TessBaseAPI *api, int argc, char **argv)
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 
)

Definition at line 197 of file tesseractmain.cpp.

207  {
208  if (argc == 1) {
209  PrintHelpMessage(argv[0]);
210  exit(0);
211  }
212 
213  if (argc == 2) {
214  if ((strcmp(argv[1], "-h") == 0) ||
215  (strcmp(argv[1], "--help") == 0)) {
216  PrintHelpMessage(argv[0]);
217  exit(0);
218  }
219  if ((strcmp(argv[1], "--help-psm") == 0)) {
220  PrintHelpForPSM();
221  exit(0);
222  }
223  if ((strcmp(argv[1], "-v") == 0) ||
224  (strcmp(argv[1], "--version") == 0)) {
226  exit(0);
227  }
228  }
229 
230  bool noocr = false;
231  int i = 1;
232  while (i < argc && (*outputbase == NULL || argv[i][0] == '-')) {
233  if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
234  *lang = argv[i + 1];
235  ++i;
236  } else if (strcmp(argv[i], "--tessdata-dir") == 0 && i + 1 < argc) {
237  *datapath = argv[i + 1];
238  ++i;
239  } else if (strcmp(argv[i], "--user-words") == 0 && i + 1 < argc) {
240  vars_vec->push_back("user_words_file");
241  vars_values->push_back(argv[i + 1]);
242  ++i;
243  } else if (strcmp(argv[i], "--user-patterns") == 0 && i + 1 < argc) {
244  vars_vec->push_back("user_patterns_file");
245  vars_values->push_back(argv[i + 1]);
246  ++i;
247  } else if (strcmp(argv[i], "--list-langs") == 0) {
248  noocr = true;
249  *list_langs = true;
250  } else if (strcmp(argv[i], "-psm") == 0 && i + 1 < argc) {
251  *pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[i + 1]));
252  ++i;
253  } else if (strcmp(argv[i], "--print-parameters") == 0) {
254  noocr = true;
255  *print_parameters = true;
256  } else if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) {
257  // handled properly after api init
258  ++i;
259  } else if (*image == NULL) {
260  *image = argv[i];
261  } else if (*outputbase == NULL) {
262  *outputbase = argv[i];
263  }
264  ++i;
265  }
266 
267  *arg_i = i;
268 
269  if (argc == 2 && strcmp(argv[1], "--list-langs") == 0) {
270  *list_langs = true;
271  noocr = true;
272  }
273 
274  if (*outputbase == NULL && noocr == false) {
275  PrintHelpMessage(argv[0]);
276  exit(1);
277  }
278 
279  if (*outputbase != NULL && strcmp(*outputbase, "-") &&
280  strcmp(*outputbase, "stdout")) {
281  tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n",
283  }
284 }
static const char * Version()
Definition: baseapi.cpp:142
int push_back(T object)
#define tprintf(...)
Definition: tprintf.h:31
void PrintHelpForPSM()
void PrintHelpMessage(const char *program)
void PrintVersionInfo()
void PreloadRenderers ( tesseract::TessBaseAPI api,
tesseract::PointerVector< tesseract::TessResultRenderer > *  renderers,
tesseract::PageSegMode  pagesegmode,
const char *  outputbase 
)

Definition at line 286 of file tesseractmain.cpp.

289  {
290  if (pagesegmode == tesseract::PSM_OSD_ONLY) {
291  renderers->push_back(new tesseract::TessOsdRenderer(outputbase));
292  } else {
293  bool b;
294  api->GetBoolVariable("tessedit_create_hocr", &b);
295  if (b) {
296  bool font_info;
297  api->GetBoolVariable("hocr_font_info", &font_info);
298  renderers->push_back(
299  new tesseract::TessHOcrRenderer(outputbase, font_info));
300  }
301 
302  api->GetBoolVariable("tessedit_create_pdf", &b);
303  if (b) {
304  renderers->push_back(new tesseract::TessPDFRenderer(outputbase,
305  api->GetDatapath()));
306  }
307 
308  api->GetBoolVariable("tessedit_write_unlv", &b);
309  if (b) {
310  renderers->push_back(new tesseract::TessUnlvRenderer(outputbase));
311  }
312 
313  api->GetBoolVariable("tessedit_create_boxfile", &b);
314  if (b) {
315  renderers->push_back(new tesseract::TessBoxTextRenderer(outputbase));
316  }
317 
318  api->GetBoolVariable("tessedit_create_txt", &b);
319  if (b || renderers->empty()) {
320  renderers->push_back(new tesseract::TessTextRenderer(outputbase));
321  }
322  }
323 
324  if (!renderers->empty()) {
325  // Since the PointerVector auto-deletes, null-out the renderers that are
326  // added to the root, and leave the root in the vector.
327  for (int r = 1; r < renderers->size(); ++r) {
328  (*renderers)[0]->insert((*renderers)[r]);
329  (*renderers)[r] = NULL;
330  }
331  }
332 }
int push_back(T *object)
const char * GetDatapath()
Definition: baseapi.cpp:954
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:236
Orientation and script detection only.
Definition: publictypes.h:152
bool empty() const
Definition: genericvector.h:84
void PrintHelpForPSM ( )

Definition at line 83 of file tesseractmain.cpp.

83  {
84  const char* msg =
85  "Page segmentation modes:\n"
86  " 0 Orientation and script detection (OSD) only.\n"
87  " 1 Automatic page segmentation with OSD.\n"
88  " 2 Automatic page segmentation, but no OSD, or OCR.\n"
89  " 3 Fully automatic page segmentation, but no OSD. (Default)\n"
90  " 4 Assume a single column of text of variable sizes.\n"
91  " 5 Assume a single uniform block of vertically aligned text.\n"
92  " 6 Assume a single uniform block of text.\n"
93  " 7 Treat the image as a single text line.\n"
94  " 8 Treat the image as a single word.\n"
95  " 9 Treat the image as a single word in a circle.\n"
96  " 10 Treat the image as a single character.\n"
97 
98  //TODO: Consider publishing these modes.
99  #if 0
100  " 11 Sparse text. Find as much text as possible in no"
101  " particular order.\n"
102  " 12 Sparse text with OSD.\n"
103  " 13 Raw line. Treat the image as a single text line,\n"
104  "\t\t\tbypassing hacks that are Tesseract-specific.\n"
105  #endif
106  ;
107 
108  fprintf(stderr, "%s", msg);
109 }
void PrintHelpMessage ( const char *  program)

Definition at line 111 of file tesseractmain.cpp.

111  {
112  PrintUsage(program);
113 
114  const char* ocr_options =
115  "OCR options:\n"
116  " --tessdata-dir PATH Specify the location of tessdata path.\n"
117  " --user-words PATH Specify the location of user words file.\n"
118  " --user-patterns PATH Specify the location of user patterns file.\n"
119  " -l LANG[+LANG] Specify language(s) used for OCR.\n"
120  " -c VAR=VALUE Set value for config variables.\n"
121  " Multiple -c arguments are allowed.\n"
122  " -psm NUM Specify page segmentation mode.\n"
123  "NOTE: These options must occur before any configfile.\n"
124  ;
125 
126  fprintf(stderr, "\n%s\n", ocr_options);
127  PrintHelpForPSM();
128 
129  const char *single_options =
130  "Single options:\n"
131  " -h, --help Show this help message.\n"
132  " --help-psm Show page segmentation modes.\n"
133  " -v, --version Show version information.\n"
134  " --list-langs List available languages for tesseract engine.\n"
135  " --print-parameters Print tesseract parameters to stdout.\n"
136  ;
137 
138  fprintf(stderr, "\n%s", single_options);
139 }
void PrintHelpForPSM()
void PrintUsage(const char *program)
void PrintLangsList ( tesseract::TessBaseAPI api)

Definition at line 164 of file tesseractmain.cpp.

164  {
165  GenericVector<STRING> languages;
166  api->GetAvailableLanguagesAsVector(&languages);
167  fprintf(stderr, "List of available languages (%d):\n",
168  languages.size());
169  for (int index = 0; index < languages.size(); ++index) {
170  STRING& string = languages[index];
171  fprintf(stderr, "%s\n", string.string());
172  }
173  api->End();
174 }
int size() const
Definition: genericvector.h:72
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
Definition: baseapi.cpp:370
Definition: strngs.h:44
void PrintUsage ( const char *  program)

Definition at line 73 of file tesseractmain.cpp.

73  {
74  fprintf(stderr,
75  "Usage:\n"
76  " %s --help | --help-psm | --version\n"
77  " %s --list-langs [--tessdata-dir PATH]\n"
78  " %s --print-parameters [options...] [configfile...]\n"
79  " %s imagename|stdin outputbase|stdout [options...] [configfile...]\n",
80  program, program, program, program);
81 }
void PrintVersionInfo ( )

Definition at line 36 of file tesseractmain.cpp.

36  {
37  char *versionStrP;
38 
39  fprintf(stderr, "tesseract %s\n", tesseract::TessBaseAPI::Version());
40 
41  versionStrP = getLeptonicaVersion();
42  fprintf(stderr, " %s\n", versionStrP);
43  lept_free(versionStrP);
44 
45  versionStrP = getImagelibVersions();
46  fprintf(stderr, " %s\n", versionStrP);
47  lept_free(versionStrP);
48 
49 #ifdef USE_OPENCL
50  cl_platform_id platform;
51  cl_uint num_platforms;
52  cl_device_id devices[2];
53  cl_uint num_devices;
54  char info[256];
55  int i;
56 
57  fprintf(stderr, " OpenCL info:\n");
58  clGetPlatformIDs(1, &platform, &num_platforms);
59  fprintf(stderr, " Found %d platforms.\n", num_platforms);
60  clGetPlatformInfo(platform, CL_PLATFORM_NAME, 256, info, 0);
61  fprintf(stderr, " Platform name: %s.\n", info);
62  clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 256, info, 0);
63  fprintf(stderr, " Version: %s.\n", info);
64  clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 2, devices, &num_devices);
65  fprintf(stderr, " Found %d devices.\n", num_devices);
66  for (i = 0; i < num_devices; ++i) {
67  clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0);
68  fprintf(stderr, " Device %d name: %s.\n", i+1, info);
69  }
70 #endif
71 }
static const char * Version()
Definition: baseapi.cpp:142
void SetVariablesFromCLArgs ( tesseract::TessBaseAPI api,
int  argc,
char **  argv 
)

Definition at line 141 of file tesseractmain.cpp.

141  {
142  char opt1[256], opt2[255];
143  for (int i = 0; i < argc; i++) {
144  if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) {
145  strncpy(opt1, argv[i + 1], 255);
146  opt1[255] = '\0';
147  char *p = strchr(opt1, '=');
148  if (!p) {
149  fprintf(stderr, "Missing = in configvar assignment\n");
150  exit(1);
151  }
152  *p = 0;
153  strncpy(opt2, strchr(argv[i + 1], '=') + 1, 255);
154  opt2[254] = 0;
155  ++i;
156 
157  if (!api->SetVariable(opt1, opt2)) {
158  fprintf(stderr, "Could not set option: %s=%s\n", opt1, opt2);
159  }
160  }
161  }
162 }
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:216