tesseract  4.1.0
tesseractmain.cpp File Reference
#include <cerrno>
#include <iostream>
#include "allheaders.h"
#include "baseapi.h"
#include "dict.h"
#include "renderer.h"
#include "simddetect.h"
#include "tprintf.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 579 of file tesseractmain.cpp.

579  {
580  const char* lang = nullptr;
581  const char* image = nullptr;
582  const char* outputbase = nullptr;
583  const char* datapath = nullptr;
584  bool list_langs = false;
585  bool print_parameters = false;
586  l_int32 dpi = 0;
587  int arg_i = 1;
589 #ifdef DISABLED_LEGACY_ENGINE
590  auto enginemode = tesseract::OEM_LSTM_ONLY;
591 #else
593 #endif
594  /* main() calls functions like ParseArgs which call exit().
595  * This results in memory leaks if vars_vec and vars_values are
596  * declared as auto variables (destructor is not called then). */
597  static GenericVector<STRING> vars_vec;
598  static GenericVector<STRING> vars_values;
599 
600 #if !defined(DEBUG)
601  // Disable debugging and informational messages from Leptonica.
602  setMsgSeverity(L_SEVERITY_ERROR);
603 #endif
604 
605 #if defined(HAVE_TIFFIO_H) && defined(_WIN32)
606  /* Show libtiff errors and warnings on console (not in GUI). */
607  TIFFSetErrorHandler(Win32ErrorHandler);
608  TIFFSetWarningHandler(Win32WarningHandler);
609 #endif // HAVE_TIFFIO_H && _WIN32
610 
611  ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &dpi,
612  &list_langs, &print_parameters, &vars_vec, &vars_values, &arg_i,
613  &pagesegmode, &enginemode);
614 
615  if (lang == nullptr) {
616  // Set default language if none was given.
617  lang = "eng";
618  }
619 
620  if (image == nullptr && !list_langs && !print_parameters)
621  return EXIT_SUCCESS;
622 
623  // Call GlobalDawgCache here to create the global DawgCache object before
624  // the TessBaseAPI object. This fixes the order of destructor calls:
625  // first TessBaseAPI must be destructed, DawgCache must be the last object.
627 
628  // Avoid memory leak caused by auto variable when return is called.
629  static tesseract::TessBaseAPI api;
630 
631  api.SetOutputName(outputbase);
632 
633  const int init_failed = api.Init(datapath, lang, enginemode, &(argv[arg_i]),
634  argc - arg_i, &vars_vec, &vars_values, false);
635 
636  SetVariablesFromCLArgs(&api, argc, argv);
637 
638  // SIMD settings might be overridden by config variable.
640 
641  if (list_langs) {
642  PrintLangsList(&api);
643  return EXIT_SUCCESS;
644  }
645 
646  if (init_failed) {
647  fprintf(stderr, "Could not initialize tesseract.\n");
648  return EXIT_FAILURE;
649  }
650 
651  if (print_parameters) {
652  FILE* fout = stdout;
653  fprintf(stdout, "Tesseract parameters:\n");
654  api.PrintVariables(fout);
655  api.End();
656  return EXIT_SUCCESS;
657  }
658 
659  FixPageSegMode(&api, pagesegmode);
660 
661  if (dpi) {
662  char dpi_string[255];
663  snprintf(dpi_string, 254, "%d", dpi);
664  api.SetVariable("user_defined_dpi", dpi_string);
665  }
666 
667  if (pagesegmode == tesseract::PSM_AUTO_ONLY) {
668  int ret_val = EXIT_SUCCESS;
669 
670  Pix* pixs = pixRead(image);
671  if (!pixs) {
672  fprintf(stderr, "Leptonica can't process input file: %s\n", image);
673  return 2;
674  }
675 
676  api.SetImage(pixs);
677 
678  tesseract::Orientation orientation;
681  float deskew_angle;
682 
683  const tesseract::PageIterator* it = api.AnalyseLayout();
684  if (it) {
685  // TODO: Implement output of page segmentation, see documentation
686  // ("Automatic page segmentation, but no OSD, or OCR").
687  it->Orientation(&orientation, &direction, &order, &deskew_angle);
688  tprintf(
689  "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
690  "Deskew angle: %.4f\n",
691  orientation, direction, order, deskew_angle);
692  } else {
693  ret_val = EXIT_FAILURE;
694  }
695 
696  delete it;
697 
698  pixDestroy(&pixs);
699  return ret_val;
700  }
701 
702  // set in_training_mode to true when using one of these configs:
703  // ambigs.train, box.train, box.train.stderr, linebox, rebox
704  bool b = false;
705  bool in_training_mode =
706  (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) ||
707  (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) ||
708  (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b);
709 
710 #ifdef DISABLED_LEGACY_ENGINE
711  auto cur_psm = api.GetPageSegMode();
712  auto osd_warning = std::string("");
713  if (cur_psm == tesseract::PSM_OSD_ONLY) {
714  const char* disabled_osd_msg =
715  "\nERROR: The page segmentation mode 0 (OSD Only) is currently disabled.\n\n";
716  fprintf(stderr, "%s", disabled_osd_msg);
717  return EXIT_FAILURE;
718  } else if (cur_psm == tesseract::PSM_AUTO_OSD) {
720  osd_warning +=
721  "\nWarning: The page segmentation mode 1 (Auto+OSD) is currently disabled. "
722  "Using PSM 3 (Auto) instead.\n\n";
723  } else if (cur_psm == tesseract::PSM_SPARSE_TEXT_OSD) {
725  osd_warning +=
726  "\nWarning: The page segmentation mode 12 (Sparse text + OSD) is currently disabled. "
727  "Using PSM 11 (Sparse text) instead.\n\n";
728  }
729 #endif // def DISABLED_LEGACY_ENGINE
730 
731  // Avoid memory leak caused by auto variable when exit() is called.
733 
734  if (in_training_mode) {
735  renderers.push_back(nullptr);
736  } else if (outputbase != nullptr) {
737  PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
738  }
739 
740  bool banner = false;
741  if (outputbase != nullptr && strcmp(outputbase, "-") &&
742  strcmp(outputbase, "stdout")) {
743  banner = true;
744  }
745 
746  if (!renderers.empty()) {
747  if (banner) PrintBanner();
748 #ifdef DISABLED_LEGACY_ENGINE
749  if (!osd_warning.empty()) {
750  fprintf(stderr, "%s",osd_warning.c_str());
751  }
752 #endif
753  bool succeed = api.ProcessPages(image, nullptr, 0, renderers[0]);
754  if (!succeed) {
755  fprintf(stderr, "Error during processing.\n");
756  return EXIT_FAILURE;
757  }
758  }
759 
760  return EXIT_SUCCESS;
761 }
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:516
Orientation and script detection only.
Definition: publictypes.h:164
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:300
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:574
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:167
Find as much text as possible in no particular order.
Definition: publictypes.h:177
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:509
void PrintVariables(FILE *fp) const
Definition: baseapi.cpp:328
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
bool empty() const
Definition: genericvector.h:89
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:168
int push_back(T * object)
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:38
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1068
script detection. (OSD)
Definition: publictypes.h:165
static TESS_API DawgCache * GlobalDawgCache()
Definition: dict.cpp:193
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:280
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:804
static TESS_API void Update()
Definition: simddetect.cpp:162
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:340
Sparse text with orientation and script det.
Definition: publictypes.h:178
void SetOutputName(const char *name)
Definition: baseapi.cpp:273