tesseract  4.0.0-1-g2a2b
tesseractmain.cpp File Reference
#include <cerrno>
#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 
)

Definition at line 514 of file tesseractmain.cpp.

514  {
515  const char* lang = nullptr;
516  const char* image = nullptr;
517  const char* outputbase = nullptr;
518  const char* datapath = nullptr;
519  bool list_langs = false;
520  bool print_parameters = false;
521  l_int32 dpi = 0;
522  int arg_i = 1;
524 #ifdef DISABLED_LEGACY_ENGINE
525  auto enginemode = tesseract::OEM_LSTM_ONLY;
526 #else
528 #endif
529  /* main() calls functions like ParseArgs which call exit().
530  * This results in memory leaks if vars_vec and vars_values are
531  * declared as auto variables (destructor is not called then). */
532  static GenericVector<STRING> vars_vec;
533  static GenericVector<STRING> vars_values;
534 
535 #if !defined(DEBUG)
536  // Disable debugging and informational messages from Leptonica.
537  setMsgSeverity(L_SEVERITY_ERROR);
538 #endif
539 
540 #if defined(HAVE_TIFFIO_H) && defined(_WIN32)
541  /* Show libtiff errors and warnings on console (not in GUI). */
542  TIFFSetErrorHandler(Win32ErrorHandler);
543  TIFFSetWarningHandler(Win32WarningHandler);
544 #endif // HAVE_TIFFIO_H && _WIN32
545 
546  ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &dpi,
547  &list_langs, &print_parameters, &vars_vec, &vars_values, &arg_i,
548  &pagesegmode, &enginemode);
549 
550  if (lang == nullptr) {
551  // Set default language if none was given.
552  lang = "eng";
553  }
554 
555  if (image == nullptr && !list_langs && !print_parameters)
556  return EXIT_SUCCESS;
557 
558  PERF_COUNT_START("Tesseract:main")
559 
560  // Call GlobalDawgCache here to create the global DawgCache object before
561  // the TessBaseAPI object. This fixes the order of destructor calls:
562  // first TessBaseAPI must be destructed, DawgCache must be the last object.
563  tesseract::Dict::GlobalDawgCache();
564 
565  // Avoid memory leak caused by auto variable when return is called.
566  static tesseract::TessBaseAPI api;
567 
568  api.SetOutputName(outputbase);
569 
570  const int init_failed = api.Init(datapath, lang, enginemode, &(argv[arg_i]),
571  argc - arg_i, &vars_vec, &vars_values, false);
572 
573  SetVariablesFromCLArgs(&api, argc, argv);
574 
575  if (list_langs) {
576  PrintLangsList(&api);
577  return EXIT_SUCCESS;
578  }
579 
580  if (init_failed) {
581  fprintf(stderr, "Could not initialize tesseract.\n");
582  return EXIT_FAILURE;
583  }
584 
585  if (print_parameters) {
586  FILE* fout = stdout;
587  fprintf(stdout, "Tesseract parameters:\n");
588  api.PrintVariables(fout);
589  api.End();
590  return EXIT_SUCCESS;
591  }
592 
593  FixPageSegMode(&api, pagesegmode);
594 
595  if (dpi) {
596  char dpi_string[255];
597  snprintf(dpi_string, 254, "%d", dpi);
598  api.SetVariable("user_defined_dpi", dpi_string);
599  }
600 
601  if (pagesegmode == tesseract::PSM_AUTO_ONLY) {
602  int ret_val = EXIT_SUCCESS;
603 
604  Pix* pixs = pixRead(image);
605  if (!pixs) {
606  fprintf(stderr, "Leptonica can't process input file: %s\n", image);
607  return 2;
608  }
609 
610  api.SetImage(pixs);
611 
612  tesseract::Orientation orientation;
615  float deskew_angle;
616 
617  const tesseract::PageIterator* it = api.AnalyseLayout();
618  if (it) {
619  it->Orientation(&orientation, &direction, &order, &deskew_angle);
620  tprintf(
621  "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
622  "Deskew angle: %.4f\n",
623  orientation, direction, order, deskew_angle);
624  } else {
625  ret_val = EXIT_FAILURE;
626  }
627 
628  delete it;
629 
630  pixDestroy(&pixs);
631  return ret_val;
632  }
633 
634  // set in_training_mode to true when using one of these configs:
635  // ambigs.train, box.train, box.train.stderr, linebox, rebox
636  bool b = false;
637  bool in_training_mode =
638  (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) ||
639  (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) ||
640  (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b);
641 
642 #ifdef DISABLED_LEGACY_ENGINE
643  auto cur_psm = api.GetPageSegMode();
644  auto osd_warning = std::string("");
645  if (cur_psm == tesseract::PSM_OSD_ONLY) {
646  const char* disabled_osd_msg =
647  "\nERROR: The page segmentation mode 0 (OSD Only) is currently disabled.\n\n";
648  fprintf(stderr, "%s", disabled_osd_msg);
649  return EXIT_FAILURE;
650  } else if (cur_psm == tesseract::PSM_AUTO_OSD) {
651  api.SetPageSegMode(tesseract::PSM_AUTO);
652  osd_warning +=
653  "\nWarning: The page segmentation mode 1 (Auto+OSD) is currently disabled. "
654  "Using PSM 3 (Auto) instead.\n\n";
655  } else if (tesseract::PSM_SPARSE_TEXT_OSD) {
656  api.SetPageSegMode(tesseract::PSM_SPARSE_TEXT);
657  osd_warning +=
658  "\nWarning: The page segmentation mode 12 (Sparse text + OSD) is currently disabled. "
659  "Using PSM 11 (Sparse text) instead.\n\n";
660  }
661 #endif // def DISABLED_LEGACY_ENGINE
662 
663  // Avoid memory leak caused by auto variable when exit() is called.
665 
666  if (in_training_mode) {
667  renderers.push_back(nullptr);
668  } else {
669  PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
670  }
671 
672  bool banner = false;
673  if (outputbase != nullptr && strcmp(outputbase, "-") &&
674  strcmp(outputbase, "stdout")) {
675  banner = true;
676  }
677 
678  if (!renderers.empty()) {
679  if (banner) PrintBanner();
680 #ifdef DISABLED_LEGACY_ENGINE
681  if (!osd_warning.empty()) {
682  fprintf(stderr, "%s",osd_warning.c_str());
683  }
684 #endif
685  bool succeed = api.ProcessPages(image, nullptr, 0, renderers[0]);
686  if (!succeed) {
687  fprintf(stderr, "Error during processing.\n");
688  return EXIT_FAILURE;
689  }
690  }
691 
693 
694  return EXIT_SUCCESS;
695 }
Sparse text with orientation and script det.
Definition: publictypes.h:178
struct TessBaseAPI TessBaseAPI
Definition: capi.h:89
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
#define PERF_COUNT_START(FUNCT_NAME)
bool empty() const
Definition: genericvector.h:90
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int push_back(T * object)
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
#define PERF_COUNT_END
Orientation and script detection only.
Definition: publictypes.h:164
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