tesseract  4.00.00dev
tesseractmain.cpp File Reference
#include <iostream>
#include "allheaders.h"
#include "baseapi.h"
#include "basedir.h"
#include "dict.h"
#include "openclwrapper.h"
#include "osdetect.h"
#include "renderer.h"
#include "simddetect.h"
#include "strngs.h"
#include "tprintf.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

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 428 of file tesseractmain.cpp.

428  {
429  const char* lang = "eng";
430  const char* image = NULL;
431  const char* outputbase = NULL;
432  const char* datapath = NULL;
433  bool list_langs = false;
434  bool print_parameters = false;
435  int arg_i = 1;
438  /* main() calls functions like ParseArgs which call exit().
439  * This results in memory leaks if vars_vec and vars_values are
440  * declared as auto variables (destructor is not called then). */
441  static GenericVector<STRING> vars_vec;
442  static GenericVector<STRING> vars_values;
443 
444 #if !defined(DEBUG)
445  // Disable debugging and informational messages from Leptonica.
446  setMsgSeverity(L_SEVERITY_ERROR);
447 #endif
448 
449 #if defined(HAVE_TIFFIO_H) && defined(_WIN32)
450  /* Show libtiff warnings on console (not in GUI). */
451  TIFFSetWarningHandler(Win32WarningHandler);
452 #endif /* HAVE_TIFFIO_H && _WIN32 */
453 
454  ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &list_langs,
455  &print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode,
456  &enginemode);
457 
458  bool banner = false;
459  if (outputbase != NULL && strcmp(outputbase, "-") &&
460  strcmp(outputbase, "stdout")) {
461  banner = true;
462  }
463 
464  PERF_COUNT_START("Tesseract:main")
465 
466  // Call GlobalDawgCache here to create the global DawgCache object before
467  // the TessBaseAPI object. This fixes the order of destructor calls:
468  // first TessBaseAPI must be destructed, DawgCache must be the last object.
469  tesseract::Dict::GlobalDawgCache();
470 
471  // Avoid memory leak caused by auto variable when return is called.
472  static tesseract::TessBaseAPI api;
473 
474  api.SetOutputName(outputbase);
475 
476  int init_failed = api.Init(datapath, lang, enginemode, &(argv[arg_i]),
477  argc - arg_i, &vars_vec, &vars_values, false);
478 
479  SetVariablesFromCLArgs(&api, argc, argv);
480 
481  if (list_langs) {
482  PrintLangsList(&api);
483  return EXIT_SUCCESS;
484  }
485 
486  if (init_failed) {
487  fprintf(stderr, "Could not initialize tesseract.\n");
488  return EXIT_FAILURE;
489  }
490 
491  if (print_parameters) {
492  FILE* fout = stdout;
493  fprintf(stdout, "Tesseract parameters:\n");
494  api.PrintVariables(fout);
495  api.End();
496  return EXIT_SUCCESS;
497  }
498 
499  FixPageSegMode(&api, pagesegmode);
500 
501  if (pagesegmode == tesseract::PSM_AUTO_ONLY) {
502  int ret_val = EXIT_SUCCESS;
503 
504  Pix* pixs = pixRead(image);
505  if (!pixs) {
506  fprintf(stderr, "Cannot open input file: %s\n", image);
507  return 2;
508  }
509 
510  api.SetImage(pixs);
511 
512  tesseract::Orientation orientation;
515  float deskew_angle;
516 
517  tesseract::PageIterator* it = api.AnalyseLayout();
518  if (it) {
519  it->Orientation(&orientation, &direction, &order, &deskew_angle);
520  tprintf(
521  "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
522  "Deskew angle: %.4f\n",
523  orientation, direction, order, deskew_angle);
524  } else {
525  ret_val = EXIT_FAILURE;
526  }
527 
528  delete it;
529 
530  pixDestroy(&pixs);
531  return ret_val;
532  }
533 
534  // set in_training_mode to true when using one of these configs:
535  // ambigs.train, box.train, box.train.stderr, linebox, rebox
536  bool b = false;
537  bool in_training_mode =
538  (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) ||
539  (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) ||
540  (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b);
541 
542  // Avoid memory leak caused by auto variable when exit() is called.
544 
545  if (in_training_mode) {
546  renderers.push_back(NULL);
547  } else {
548  PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
549  }
550 
551  if (!renderers.empty()) {
552  if (banner) PrintBanner();
553  bool succeed = api.ProcessPages(image, NULL, 0, renderers[0]);
554  if (!succeed) {
555  fprintf(stderr, "Error during processing.\n");
556  return EXIT_FAILURE;
557  }
558  }
559 
561 
562  return EXIT_SUCCESS;
563 }
bool empty() const
Definition: genericvector.h:91
struct TessBaseAPI TessBaseAPI
Definition: capi.h:83
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
#define PERF_COUNT_END
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
#define tprintf(...)
Definition: tprintf.h:31
int push_back(T * object)
#define PERF_COUNT_START(FUNCT_NAME)
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:167
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:168