tesseract v5.3.3.20231005
tesseract.cpp File Reference
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory>
#include <allheaders.h>
#include <tesseract/baseapi.h>
#include "dict.h"
#include <tesseract/renderer.h>
#include "simddetect.h"
#include "tesseractclass.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 636 of file tesseract.cpp.

636 {
637#if defined(__USE_GNU) && defined(HAVE_FEENABLEEXCEPT)
638 // Raise SIGFPE.
639# if defined(__clang__)
640 // clang creates code which causes some FP exceptions, so don't enable those.
641 feenableexcept(FE_DIVBYZERO);
642# else
643 feenableexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_INVALID);
644# endif
645#endif
646 const char *lang = nullptr;
647 const char *image = nullptr;
648 const char *outputbase = nullptr;
649 const char *datapath = nullptr;
650 bool list_langs = false;
651 bool print_parameters = false;
652 bool print_fonts_table = false;
653 l_int32 dpi = 0;
654 int arg_i = 1;
656#ifdef DISABLED_LEGACY_ENGINE
657 auto enginemode = tesseract::OEM_LSTM_ONLY;
658#else
660#endif
661 std::vector<std::string> vars_vec;
662 std::vector<std::string> vars_values;
663
664 if (std::getenv("LEPT_MSG_SEVERITY")) {
665 // Get Leptonica message level from environment variable.
666 setMsgSeverity(L_SEVERITY_EXTERNAL);
667 } else {
668 // Disable debugging and informational messages from Leptonica.
669 setMsgSeverity(L_SEVERITY_ERROR);
670 }
671
672#if defined(HAVE_TIFFIO_H) && defined(_WIN32)
673 /* Show libtiff errors and warnings on console (not in GUI). */
674 TIFFSetErrorHandler(Win32ErrorHandler);
675 TIFFSetWarningHandler(Win32WarningHandler);
676#endif // HAVE_TIFFIO_H && _WIN32
677
678 if (!ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &dpi, &list_langs,
679 &print_parameters, &print_fonts_table, &vars_vec, &vars_values, &arg_i,
680 &pagesegmode, &enginemode)) {
681 return EXIT_FAILURE;
682 }
683
684 bool in_recognition_mode = !list_langs && !print_parameters && !print_fonts_table;
685
686 if (lang == nullptr && in_recognition_mode) {
687 // Set default language model if none was given and a model file is needed.
688 lang = "eng";
689 }
690
691 if (image == nullptr && in_recognition_mode) {
692 return EXIT_SUCCESS;
693 }
694
695 // Call GlobalDawgCache here to create the global DawgCache object before
696 // the TessBaseAPI object. This fixes the order of destructor calls:
697 // first TessBaseAPI must be destructed, DawgCache must be the last object.
699
700 TessBaseAPI api;
701
702 api.SetOutputName(outputbase);
703
704 const int init_failed = api.Init(datapath, lang, enginemode, &(argv[arg_i]), argc - arg_i,
705 &vars_vec, &vars_values, false);
706
707 if (!SetVariablesFromCLArgs(api, argc, argv)) {
708 return EXIT_FAILURE;
709 }
710
711 // SIMD settings might be overridden by config variable.
713
714 if (list_langs) {
715 PrintLangsList(api);
716 return EXIT_SUCCESS;
717 }
718
719 if (init_failed) {
720 fprintf(stderr, "Could not initialize tesseract.\n");
721 return EXIT_FAILURE;
722 }
723
724 if (print_parameters) {
725 FILE *fout = stdout;
726 fprintf(stdout, "Tesseract parameters:\n");
727 api.PrintVariables(fout);
728 api.End();
729 return EXIT_SUCCESS;
730 }
731
732#ifndef DISABLED_LEGACY_ENGINE
733 if (print_fonts_table) {
734 FILE *fout = stdout;
735 fprintf(stdout, "Tesseract fonts table:\n");
736 api.PrintFontsTable(fout);
737 api.End();
738 return EXIT_SUCCESS;
739 }
740#endif // ndef DISABLED_LEGACY_ENGINE
741
742 FixPageSegMode(api, pagesegmode);
743
744 if (dpi) {
745 auto dpi_string = std::to_string(dpi);
746 api.SetVariable("user_defined_dpi", dpi_string.c_str());
747 }
748
749 int ret_val = EXIT_SUCCESS;
750
751 if (pagesegmode == tesseract::PSM_AUTO_ONLY) {
752 Pix *pixs = pixRead(image);
753 if (!pixs) {
754 fprintf(stderr, "Leptonica can't process input file: %s\n", image);
755 return 2;
756 }
757
758 api.SetImage(pixs);
759
760 tesseract::Orientation orientation;
763 float deskew_angle;
764
765 const std::unique_ptr<const tesseract::PageIterator> it(api.AnalyseLayout());
766 if (it) {
767 // TODO: Implement output of page segmentation, see documentation
768 // ("Automatic page segmentation, but no OSD, or OCR").
769 it->Orientation(&orientation, &direction, &order, &deskew_angle);
770 tprintf(
771 "Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
772 "Deskew angle: %.4f\n",
773 orientation, direction, order, deskew_angle);
774 } else {
775 ret_val = EXIT_FAILURE;
776 }
777
778 pixDestroy(&pixs);
779 return ret_val;
780 }
781
782 // Set in_training_mode to true when using one of these configs:
783 // ambigs.train, box.train, box.train.stderr, linebox, rebox, lstm.train.
784 // In this mode no other OCR result files are written.
785 bool b = false;
786 bool in_training_mode = (api.GetBoolVariable("tessedit_ambigs_training", &b) && b) ||
787 (api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) ||
788 (api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b) ||
789 (api.GetBoolVariable("tessedit_train_line_recognizer", &b) && b);
790
792 if (!api.tesseract()->AnyTessLang()) {
793 fprintf(stderr, "Error, OSD requires a model for the legacy engine\n");
794 return EXIT_FAILURE;
795 }
796 }
797#ifdef DISABLED_LEGACY_ENGINE
798 auto cur_psm = api.GetPageSegMode();
799 auto osd_warning = std::string("");
800 if (cur_psm == tesseract::PSM_OSD_ONLY) {
801 const char *disabled_osd_msg =
802 "\nERROR: The page segmentation mode 0 (OSD Only) is currently "
803 "disabled.\n\n";
804 fprintf(stderr, "%s", disabled_osd_msg);
805 return EXIT_FAILURE;
806 } else if (cur_psm == tesseract::PSM_AUTO_OSD) {
808 osd_warning +=
809 "\nWarning: The page segmentation mode 1 (Auto+OSD) is currently "
810 "disabled. "
811 "Using PSM 3 (Auto) instead.\n\n";
812 } else if (cur_psm == tesseract::PSM_SPARSE_TEXT_OSD) {
814 osd_warning +=
815 "\nWarning: The page segmentation mode 12 (Sparse text + OSD) is "
816 "currently disabled. "
817 "Using PSM 11 (Sparse text) instead.\n\n";
818 }
819#endif // def DISABLED_LEGACY_ENGINE
820
821 std::vector<std::unique_ptr<TessResultRenderer>> renderers;
822
823 if (in_training_mode) {
824 renderers.push_back(nullptr);
825 } else if (outputbase != nullptr) {
826 PreloadRenderers(api, renderers, pagesegmode, outputbase);
827 }
828
829 if (!renderers.empty()) {
830#ifdef DISABLED_LEGACY_ENGINE
831 if (!osd_warning.empty()) {
832 fprintf(stderr, "%s", osd_warning.c_str());
833 }
834#endif
835 bool succeed = api.ProcessPages(image, nullptr, 0, renderers[0].get());
836 if (!succeed) {
837 fprintf(stderr, "Error during processing.\n");
838 ret_val = EXIT_FAILURE;
839 }
840 }
841
842 return ret_val;
843}
@ PSM_OSD_ONLY
Orientation and script detection only.
Definition: publictypes.h:158
@ PSM_SPARSE_TEXT
Find as much text as possible in no particular order.
Definition: publictypes.h:171
@ PSM_AUTO_ONLY
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:161
@ PSM_AUTO
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:162
@ PSM_SPARSE_TEXT_OSD
Sparse text with orientation and script det.
Definition: publictypes.h:173
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:511
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:279
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1071
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:519
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const std::vector< std::string > *vars_vec, const std::vector< std::string > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:368
void PrintVariables(FILE *fp) const
Definition: baseapi.cpp:356
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:576
Tesseract * tesseract() const
Definition: baseapi.h:711
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:812
void PrintFontsTable(FILE *fp) const
Definition: baseapi.cpp:338
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:304
void SetOutputName(const char *name)
Definition: baseapi.cpp:275
static TESS_API void Update()
Definition: simddetect.cpp:272
bool AnyTessLang() const
static DawgCache * GlobalDawgCache()
Definition: dict.cpp:172