tesseract  4.1.1
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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 599 of file tesseractmain.cpp.

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