tesseract  5.0.0-alpha-619-ge9db
tesseractmain.cpp File Reference
#include <cerrno>
#include <iostream>
#include "allheaders.h"
#include <tesseract/baseapi.h>
#include "dict.h"
#include <tesseract/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 602 of file tesseractmain.cpp.

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