Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseractmain.cpp File Reference
#include "allheaders.h"
#include "baseapi.h"
#include "basedir.h"
#include "strngs.h"
#include "tesseractmain.h"
#include "tprintf.h"

Go to the source code of this file.

Macros

#define _(x)   (x)
 

Functions

int main (int argc, char **argv)
 

Macro Definition Documentation

#define _ (   x)    (x)

Definition at line 35 of file tesseractmain.cpp.

Function Documentation

int main ( int  argc,
char **  argv 
)

Public Function Prototypes

Definition at line 50 of file tesseractmain.cpp.

50  {
51 #ifdef USING_GETTEXT
52  setlocale (LC_ALL, "");
53  bindtextdomain (PACKAGE, LOCALEDIR);
54  textdomain (PACKAGE);
55 #endif
56  if ((argc == 2 && strcmp(argv[1], "-v") == 0) ||
57  (argc == 2 && strcmp(argv[1], "--version") == 0)) {
58  char *versionStrP;
59 
60  fprintf(stderr, "tesseract %s\n", tesseract::TessBaseAPI::Version());
61 
62  versionStrP = getLeptonicaVersion();
63  fprintf(stderr, " %s\n", versionStrP);
64  lept_free(versionStrP);
65 
66  versionStrP = getImagelibVersions();
67  fprintf(stderr, " %s\n", versionStrP);
68  lept_free(versionStrP);
69 
70  exit(0);
71  }
72 
74  STRING tessdata_dir;
75  truncate_path(argv[0], &tessdata_dir);
76  int rc = api.Init(tessdata_dir.string(), NULL);
77  if (rc) {
78  fprintf(stderr, _("Could not initialize tesseract.\n"));
79  exit(1);
80  }
81 
82  if (argc == 2 && strcmp(argv[1], "--list-langs") == 0) {
83  GenericVector<STRING> languages;
84  api.GetAvailableLanguagesAsVector(&languages);
85  fprintf(stderr, _("List of available languages (%d):\n"), languages.size());
86  for (int index = 0; index < languages.size(); ++index) {
87  STRING& string = languages[index];
88  fprintf(stderr, "%s\n", string.string());
89  }
90  api.Clear();
91  exit(0);
92  }
93  api.End();
94 
95  // Make the order of args a bit more forgiving than it used to be.
96  const char* lang = "eng";
97  const char* image = NULL;
98  const char* output = NULL;
100  int arg = 1;
101  while (arg < argc && (output == NULL || argv[arg][0] == '-')) {
102  if (strcmp(argv[arg], "-l") == 0 && arg + 1 < argc) {
103  lang = argv[arg + 1];
104  ++arg;
105  } else if (strcmp(argv[arg], "-psm") == 0 && arg + 1 < argc) {
106  pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[arg + 1]));
107  ++arg;
108  } else if (image == NULL) {
109  image = argv[arg];
110  } else if (output == NULL) {
111  output = argv[arg];
112  }
113  ++arg;
114  }
115  if (output == NULL) {
116  fprintf(stderr, _("Usage:%s imagename outputbase [-l lang] "
117  "[-psm pagesegmode] [configfile...]\n\n"), argv[0]);
118  fprintf(stderr,
119  _("pagesegmode values are:\n"
120  "0 = Orientation and script detection (OSD) only.\n"
121  "1 = Automatic page segmentation with OSD.\n"
122  "2 = Automatic page segmentation, but no OSD, or OCR\n"
123  "3 = Fully automatic page segmentation, but no OSD. (Default)\n"
124  "4 = Assume a single column of text of variable sizes.\n"
125  "5 = Assume a single uniform block of vertically aligned text.\n"
126  "6 = Assume a single uniform block of text.\n"
127  "7 = Treat the image as a single text line.\n"
128  "8 = Treat the image as a single word.\n"
129  "9 = Treat the image as a single word in a circle.\n"
130  "10 = Treat the image as a single character.\n"));
131  fprintf(stderr, _("-l lang and/or -psm pagesegmode must occur before any"
132  "configfile.\n\n"));
133  fprintf(stderr, _("Single options:\n"));
134  fprintf(stderr, _(" -v --version: version info\n"));
135  fprintf(stderr, _(" --list-langs: list available languages for tesseract "
136  "engine\n"));
137  exit(1);
138  }
139 
140 
141  api.SetOutputName(output);
142 
143  rc = api.Init(tessdata_dir.string(), lang, tesseract::OEM_DEFAULT,
144  &(argv[arg]), argc - arg, NULL, NULL, false);
145  if (rc) {
146  fprintf(stderr, _("Could not initialize tesseract.\n"));
147  exit(1);
148  }
149 
150  // We have 2 possible sources of pagesegmode: a config file and
151  // the command line. For backwards compatability reasons, the
152  // default in tesseract is tesseract::PSM_SINGLE_BLOCK, but the
153  // default for this program is tesseract::PSM_AUTO. We will let
154  // the config file take priority, so the command-line default
155  // can take priority over the tesseract default, so we use the
156  // value from the command line only if the retrieved mode
157  // is still tesseract::PSM_SINGLE_BLOCK, indicating no change
158  // in any config file. Therefore the only way to force
159  // tesseract::PSM_SINGLE_BLOCK is from the command line.
160  // It would be simpler if we could set the value before Init,
161  // but that doesn't work.
163  api.SetPageSegMode(pagesegmode);
164  tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n",
166 
167 
168  FILE* fin = fopen(image, "rb");
169  if (fin == NULL) {
170  fprintf(stderr, _("Cannot open input file: %s\n"), image);
171  exit(2);
172  }
173  fclose(fin);
174 
175  PIX *pixs;
176  if ((pixs = pixRead(image)) == NULL) {
177  fprintf(stderr, _("Unsupported image type.\n"));
178  exit(3);
179  }
180  pixDestroy(&pixs);
181 
183  if (!api.ProcessPages(image, NULL, 0, &text_out)) {
184  fprintf(stderr, _("Error during processing.\n"));
185  }
186  bool output_hocr = false;
187  api.GetBoolVariable("tessedit_create_hocr", &output_hocr);
188  bool output_box = false;
189  api.GetBoolVariable("tessedit_create_boxfile", &output_box);
190  STRING outfile = output;
191  outfile += output_hocr ? ".html" : output_box ? ".box" : ".txt";
192  FILE* fout = fopen(outfile.string(), "wb");
193  if (fout == NULL) {
194  fprintf(stderr, _("Cannot create output file %s\n"), outfile.string());
195  exit(1);
196  }
197  fwrite(text_out.string(), 1, text_out.length(), fout);
198  fclose(fout);
199 
200  return 0; // Normal exit
201 }
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, STRING *text_out)
Definition: baseapi.cpp:803
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:152
inT32 length() const
Definition: strngs.cpp:151
TESS_API void truncate_path(const char *code_path, STRING *trunc_path)
Definition: basedir.cpp:32
struct Pix PIX
Definition: capi.h:69
#define NULL
Definition: host.h:144
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)
Definition: baseapi.cpp:213
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:173
const char * string() const
Definition: strngs.cpp:156
#define _(x)
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:377
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
Definition: strngs.h:40
int size() const
Definition: genericvector.h:59
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:370
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
Definition: baseapi.cpp:293
static const char * Version()
Definition: baseapi.cpp:130
void SetOutputName(const char *name)
Definition: baseapi.cpp:146
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:156
#define PACKAGE
Definition: config_auto.h:135