### Build Tesseract OCR with Autotools Source: https://github.com/ub-mannheim/tesseract/blob/main/INSTALL.GIT.md Steps to build Tesseract OCR from source using autotools on Linux/Unix systems. Requires dependencies like a C++17 compiler, automake, pkg-config, and development libraries. Includes installation and training build steps. ```shell ./autogen.sh ./configure make sudo make install sudo ldconfig make training sudo make training-install ``` -------------------------------- ### Build Tesseract OCR with CMake (Linux) Source: https://github.com/ub-mannheim/tesseract/blob/main/INSTALL.GIT.md Instructions for building Tesseract OCR using the CMake build system on Linux. This involves creating a build directory, configuring the build, and then compiling and installing. ```shell mkdir build cd build && cmake .. && make sudo make install ``` -------------------------------- ### Download Tesseract Traineddata Repository Source: https://github.com/ub-mannheim/tesseract/blob/main/INSTALL.GIT.md Clones the official Tesseract OCR traineddata repository using Git. This repository contains language data files and can be very large, useful primarily for packagers. ```git git clone https://github.com/tesseract-ocr/tessdata.git tesseract-ocr.tessdata ``` -------------------------------- ### Basic Tesseract OCR Command Source: https://github.com/ub-mannheim/tesseract/wiki/FAQ-(deutsch) Demonstrates a basic Tesseract OCR command to process an image directly from a URL and output the result to the console. It uses the default installed language model. ```bash tesseract https://digi.bib.uni-mannheim.de/fileadmin/vl/ubmaosi/59088/max/59088_0008.jpg - ``` -------------------------------- ### Define and Install text2image Executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Sets up the text2image executable, specifying its source files and linking it to the pango_training library. It also defines installation rules for runtime and library destinations, with optional PDB file installation for MSVC. ```cmake set(TEXT2IMAGE_SRC text2image.cpp degradeimage.cpp degradeimage.h) add_executable(text2image ${TEXT2IMAGE_SRC}) target_link_libraries(text2image pango_training) project_group(text2image "Training Tools") install( TARGETS text2image RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Install Tesseract Files and Targets Source: https://github.com/ub-mannheim/tesseract/blob/main/CMakeLists.txt Configures the installation of the Tesseract library, executable, header files, and configuration files. It handles platform-specific installations (e.g., PDB files on MSVC) and exports targets for CMake. ```cmake install( FILES ${CMAKE_CURRENT_BINARY_DIR}/tesseract_$.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig RENAME tesseract.pc) install(TARGETS tesseract DESTINATION bin) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() install( TARGETS libtesseract EXPORT TesseractTargets RUNTIME DESTINATION bin RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) if (MSVC AND BUILD_SHARED_LIBS) install(FILES $ DESTINATION bin OPTIONAL) endif() install( EXPORT TesseractTargets NAMESPACE Tesseract:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tesseract) install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}) install( FILES include/tesseract/baseapi.h include/tesseract/capi.h include/tesseract/renderer.h ${CMAKE_CURRENT_BINARY_DIR}/include/tesseract/version.h include/tesseract/ltrresultiterator.h include/tesseract/pageiterator.h include/tesseract/resultiterator.h include/tesseract/osdetect.h include/tesseract/publictypes.h include/tesseract/ocrclass.h include/tesseract/export.h include/tesseract/unichar.h # ${CMAKE_CURRENT_BINARY_DIR}/src/endianness.h DESTINATION include/tesseract) ``` -------------------------------- ### Build ScrollView.jar for Viewer Debugging Source: https://github.com/ub-mannheim/tesseract/blob/main/INSTALL.GIT.md Compiles the ScrollView.jar file required for Tesseract's viewer debugging. This process automatically downloads necessary Java libraries like piccolo2d and jaxb-api via curl. ```make make ScrollView.jar ``` -------------------------------- ### Add Executable wordlist2dawg Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'wordlist2dawg' executable. It links against the 'common_training' library and specifies installation destinations for runtime, library, and archive files. Includes optional PDB file installation for MSVC. ```CMake add_executable(wordlist2dawg wordlist2dawg.cpp) target_link_libraries(wordlist2dawg common_training) project_group(wordlist2dawg "Training Tools") install( TARGETS wordlist2dawg RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Add Executable lstmtraining Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'lstmtraining' executable. It links against the 'unicharset_training' library and the 'LIB_pthread' library, specifying installation destinations for runtime, library, and archive files. Includes optional PDB file installation for MSVC. ```CMake add_executable(lstmtraining lstmtraining.cpp) target_link_libraries(lstmtraining unicharset_training ${LIB_pthread}) project_group(lstmtraining "Training Tools") install( TARGETS lstmtraining RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Install Build Tools Source: https://github.com/ub-mannheim/tesseract/wiki/Windows-build Installs essential build tools like automake and build-essential, along with the cross-compilation toolchain for Windows (mingw-w64). These are prerequisites for building Tesseract. ```shell apt install automake build-essentials apt install mingw-w64 mingw-w64-tools ``` -------------------------------- ### Run Tesseract Unit Tests Source: https://github.com/ub-mannheim/tesseract/blob/main/unittest/README.md Instructions to prepare the Tesseract environment and execute unit tests. This involves updating build configurations, initializing submodules, cloning test data, copying fonts, setting an environment variable, and running the build check. ```shell autoreconf -fiv git submodule update --init git clone https://github.com/egorpugin/tessdata tessdata_unittest --depth 1 cp tessdata_unittest/fonts/* test/testing/ mv tessdata_unittest/* ../ export TESSDATA_PREFIX=/prefix/to/path/to/tessdata make check ``` -------------------------------- ### Build dawg2wordlist executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the build process for the dawg2wordlist executable. It specifies the source file, links against the common_training library, groups the target, and configures installation paths. Includes optional installation of PDB files for MSVC. ```cmake add_executable(dawg2wordlist dawg2wordlist.cpp) target_link_libraries(dawg2wordlist common_training) project_group(dawg2wordlist "Training Tools") install( TARGETS dawg2wordlist RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Install Configuration Data Source: https://github.com/ub-mannheim/tesseract/blob/main/CMakeLists.txt Installs Tesseract configuration data files (e.g., for tessdata) if the INSTALL_CONFIGS option is enabled. ```cmake if(INSTALL_CONFIGS) install(FILES ${TESSERACT_CONFIGS} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/tessdata/configs) install(FILES ${TESSERACT_TESSCONFIGS} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/tessdata/tessconfigs) endif() ``` -------------------------------- ### Build Setup: Include Directories and Definitions Source: https://github.com/ub-mannheim/tesseract/blob/main/CMakeLists.txt Includes build-related CMake functions and source group definitions. It adds preprocessor definitions and includes necessary directories, with special handling for Android toolchains to ensure proper compilation. ```cmake # ############################################################################## # # build # # ############################################################################## include(BuildFunctions) include(SourceGroups) add_definitions(-D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS=1) include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) if(ANDROID_TOOLCHAIN) include_directories(${ANDROID_TOOLCHAIN}/sysroot/usr/include) add_compile_definitions(__ANDROID_API_FUTURE__) endif() # ############################################################################## # LIBRARY tesseract ``` -------------------------------- ### List Tesseract Languages (CLI) Source: https://github.com/ub-mannheim/tesseract/wiki/Install-additional-language-and-script-models This command lists all the language data files currently recognized and available for use by the Tesseract OCR engine. It's useful for verifying successful installation of new language packs. ```bash tesseract --list-langs ``` -------------------------------- ### Add Executable combine_lang_model Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'combine_lang_model' executable. It links against the 'unicharset_training' library and specifies installation destinations for runtime, library, and archive files. Includes optional PDB file installation for MSVC. ```CMake add_executable(combine_lang_model combine_lang_model.cpp) target_link_libraries(combine_lang_model unicharset_training) project_group(combine_lang_model "Training Tools") install( TARGETS combine_lang_model RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Build combine_tessdata executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the build process for the combine_tessdata executable. It specifies the source file, links against the common_training library, groups the target, and configures installation paths. Includes optional installation of PDB files for MSVC. ```cmake add_executable(combine_tessdata combine_tessdata.cpp) target_link_libraries(combine_tessdata common_training) project_group(combine_tessdata "Training Tools") install( TARGETS combine_tessdata RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Add Executable set_unicharset_properties Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'set_unicharset_properties' executable. It links against the 'unicharset_training' library and specifies installation destinations for runtime, library, and archive files. Includes optional PDB file installation for MSVC. ```CMake add_executable(set_unicharset_properties set_unicharset_properties.cpp) target_link_libraries(set_unicharset_properties unicharset_training) project_group(set_unicharset_properties "Training Tools") install( TARGETS set_unicharset_properties RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Configure pango_training Library (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the pango_training library, gathering source files from the pango directory. It links against unicharset_training and conditionally links against Pango libraries based on build configurations (SW_BUILD or PKG_CONFIG_FOUND). Includes header generation and include directory setup. ```cmake if(PKG_CONFIG_FOUND OR SW_BUILD) if(PKG_CONFIG_FOUND) pkg_check_modules( PANGO REQUIRED IMPORTED_TARGET pango>=1.38.0 cairo pangoft2 pangocairo fontconfig) endif() file(GLOB pango_training_src pango/*) add_library(pango_training ${pango_training_src}) target_link_libraries(pango_training PUBLIC unicharset_training) if(SW_BUILD) target_link_libraries(pango_training PUBLIC org.sw.demo.gnome.pango.pangocairo) else() if(PKG_CONFIG_FOUND) target_include_directories(pango_training BEFORE PUBLIC ${PANGO_INCLUDE_DIRS}) target_compile_definitions(pango_training PUBLIC -DPANGO_ENABLE_ENGINE) target_link_libraries(pango_training PUBLIC PkgConfig::PANGO) endif() endif() target_include_directories(pango_training PUBLIC pango ${CMAKE_CURRENT_BINARY_DIR}) generate_export_header(pango_training EXPORT_MACRO_NAME TESS_PANGO_TRAINING_API) project_group(pango_training "Training Tools") endif(PKG_CONFIG_FOUND OR SW_BUILD) ``` -------------------------------- ### Add Executable merge_unicharsets Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'merge_unicharsets' executable. It links against the 'common_training' library and specifies installation destinations for runtime, library, and archive files. Includes optional PDB file installation for MSVC. ```CMake add_executable(merge_unicharsets merge_unicharsets.cpp) target_link_libraries(merge_unicharsets common_training) project_group(merge_unicharsets "Training Tools") install( TARGETS merge_unicharsets RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Install Tesseract Dependencies (Debian Packages) Source: https://github.com/ub-mannheim/tesseract/wiki/Windows-build Installs additional pre-compiled libraries for Windows as Debian packages, which are necessary dependencies for building Tesseract. ```shell dpkg -i mingw64-i686-bzip2_1.0.6-4_all.deb mingw64-i686-cairo_1.14.12-1_all.deb \ mingw64-i686-expat_2.2.2-1_all.deb mingw64-i686-fontconfig_2.12.6-1_all.deb \ mingw64-i686-freetype2_2.6.5-1_all.deb mingw64-i686-gettext_0.19.8.1-2_all.deb \ mingw64-i686-glib2.0_2.54.3-1_all.deb mingw64-i686-harfbuzz_1.7.4-1_all.deb \ mingw64-i686-icu_57.1-2_all.deb \ mingw64-i686-libffi_3.2.1-1_all.deb mingw64-i686-lzo2_2.08-1_all.deb \ mingw64-i686-pango1.0_1.40.14-1_all.deb mingw64-i686-pcre_8.40-3_all.deb \ mingw64-i686-pixman_0.34.0-1_all.deb mingw64-i686-win-iconv_0.0.6-2_all.deb ``` -------------------------------- ### Install Windows Libraries (Debian Packages) Source: https://github.com/ub-mannheim/tesseract/wiki/Windows-build Installs pre-compiled libraries for Windows, converted to Debian packages. These are required dependencies for Tesseract and Leptonica when cross-compiling. ```shell for p in mingw64-i686-giflib mingw64-i686-jbigkit mingw64-i686-libjpeg-turbo \ mingw64-i686-libpng mingw64-i686-libwebp mingw64-i686-openjpeg2 \ mingw64-i686-tiff mingw64-i686-xz mingw64-i686-zlib; do dpkg -i ${p}_*.deb done ``` -------------------------------- ### Add Library unicharset_training Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the 'unicharset_training' library, compiling source files from the 'unicharset' directory. It handles linking with ICU (i18n, PkgConfig::ICU, or ICU_LIBRARIES) and 'common_training', sets public include directories, and manages installation. Includes export header generation and optional PDB installation for MSVC. ```CMake if(ICU_FOUND) if(NOT SW_BUILD) include_directories(${ICU_INCLUDE_DIRS}) endif() file(GLOB unicharset_training_src unicharset/*) add_library(unicharset_training ${unicharset_training_src}) if(SW_BUILD) target_link_libraries(unicharset_training PUBLIC common_training org.sw.demo.unicode.icu.i18n) else() if(PKG_CONFIG_FOUND) target_link_libraries(unicharset_training PUBLIC common_training PkgConfig::ICU) else() target_link_libraries(unicharset_training PUBLIC common_training ${ICU_LIBRARIES}) endif() endif() target_include_directories(unicharset_training PUBLIC unicharset ${CMAKE_CURRENT_BINARY_DIR}) install( TARGETS unicharset_training RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) if (MSVC AND BUILD_SHARED_LIBS) install(FILES $ DESTINATION bin OPTIONAL) endif() generate_export_header(unicharset_training EXPORT_MACRO_NAME TESS_UNICHARSET_TRAINING_API) project_group(unicharset_training "Training Tools") endif() ``` -------------------------------- ### Clone and Build Leptonica Source: https://github.com/ub-mannheim/tesseract/wiki/Windows-build Clones the Leptonica library from its GitHub repository and configures it for cross-compilation targeting Windows. It then builds and installs Leptonica. ```shell cd $HOME mkdir -p src/github/DanBloomberg cd src/github/DanBloomberg git clone https://github.com/DanBloomberg/leptonica.git cd leptonica ./autobuild mkdir -p bin/ndebug/i686-w64-mingw32 cd bin/ndebug/i686-w64-mingw32 ../../../configure --host=i686-w64-mingw32 --prefix=/usr/i686-w64-mingw32 CFLAGS='-Wall -Wextra -pedantic -g -O2' make sudo make install ``` -------------------------------- ### Add Executable lstmeval Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'lstmeval' executable. It links against the 'unicharset_training' library and the 'LIB_pthread' library, specifying installation destinations for runtime, library, and archive files. Includes optional PDB file installation for MSVC. ```CMake add_executable(lstmeval lstmeval.cpp) target_link_libraries(lstmeval unicharset_training ${LIB_pthread}) project_group(lstmeval "Training Tools") install( TARGETS lstmeval RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Build mftraining executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the build process for the mftraining executable, used for merged feature training. It links against the common_training library and configures installation. This target is conditionally compiled if the legacy engine is not disabled. ```cmake if(NOT DISABLED_LEGACY_ENGINE) add_executable(mftraining mftraining.cpp mergenf.cpp mergenf.h) target_link_libraries(mftraining common_training) project_group(mftraining "Training Tools") install( TARGETS mftraining RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() endif() ``` -------------------------------- ### CMake: Build classifier_tester Executable Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the 'classifier_tester' executable, linking it against the 'common_training' library. It specifies installation rules for the executable and its PDB file on MSVC builds. ```cmake # ############################################################################## # EXECUTABLE classifier_tester # ############################################################################## if(NOT DISABLED_LEGACY_ENGINE) add_executable(classifier_tester classifier_tester.cpp) target_link_libraries(classifier_tester common_training) project_group(classifier_tester "Training Tools") install( TARGETS classifier_tester RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() endif() ``` -------------------------------- ### Display Build Configuration Summary Source: https://github.com/ub-mannheim/tesseract/blob/main/CMakeLists.txt Outputs detailed build configuration information to the console using message commands. This includes build type, architecture, compiler details, C++ standard, compiler options, definitions, linker flags, installation prefix, and various feature flags (e.g., AVX, NEON, LTO, OpenMP, libcurl, LSTM float, native optimization, graphics, legacy engine, training tools, tests, system ICU). ```cmake message(STATUS) message(STATUS "General configuration for Tesseract ${PACKAGE_VERSION}") message(STATUS "--------------------------------------------------------") message(STATUS "Build type: ${CMAKE_BUILD_TYPE} ${BUILD_ARCH}") message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID}") message(STATUS "Compiler version: ${CMAKE_CXX_COMPILER_VERSION}") message(STATUS "Used standard: C++${CMAKE_CXX_STANDARD}") message(STATUS "CXX compiler options: ${COMPILER_FLAGS}") get_directory_property(DirCompDefs COMPILE_DEFINITIONS) message(STATUS "Compile definitions = ${DirCompDefs}") message(STATUS "Linker options: ${CMAKE_EXE_LINKER_FLAGS} " "${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UP}}") message(STATUS "Install directory: ${CMAKE_INSTALL_PREFIX}") message(STATUS "HAVE_AVX: ${HAVE_AVX}") message(STATUS "HAVE_AVX2: ${HAVE_AVX2}") message(STATUS "HAVE_AVX512F: ${HAVE_AVX512F}") message(STATUS "HAVE_FMA: ${HAVE_FMA}") message(STATUS "HAVE_SSE4_1: ${HAVE_SSE4_1}") message(STATUS "MARCH_NATIVE_OPT: ${MARCH_NATIVE_OPT}") message(STATUS "HAVE_NEON: ${HAVE_NEON}") message(STATUS "Link-time optimization: ${CMAKE_INTERPROCEDURAL_OPTIMIZATION}") message(STATUS "--------------------------------------------------------") message(STATUS "Build with sw [SW_BUILD]: ${SW_BUILD}") message(STATUS "Build with openmp support [OPENMP_BUILD]: ${OPENMP_BUILD}") message(STATUS "Build with libarchive support [HAVE_LIBARCHIVE]: " "${HAVE_LIBARCHIVE}") message(STATUS "Build with libcurl support [HAVE_LIBCURL]: ${HAVE_LIBCURL}") message(STATUS "Enable float for LSTM [FAST_FLOAT]: ${FAST_FLOAT}") message(STATUS "Enable optimization for host CPU (could break HW compatibility)" " [ENABLE_NATIVE]: ${ENABLE_NATIVE}") message(STATUS "Disable disable graphics (ScrollView) [GRAPHICS_DISABLED]: " "${GRAPHICS_DISABLED}") message(STATUS "Disable the legacy OCR engine [DISABLED_LEGACY_ENGINE]: " "${DISABLED_LEGACY_ENGINE}") message(STATUS "Build training tools [BUILD_TRAINING_TOOLS]: " "${BUILD_TRAINING_TOOLS}") message(STATUS "Build tests [BUILD_TESTS]: ${BUILD_TESTS}") message(STATUS "Use system ICU Library [USE_SYSTEM_ICU]: ${USE_SYSTEM_ICU}") message( STATUS "Install tesseract configs [INSTALL_CONFIGS]: ${INSTALL_CONFIGS}") message(STATUS "--------------------------------------------------------") message(STATUS) ``` -------------------------------- ### CMake: Define common_training Library Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the 'common_training' static library, including its source files, include directories, and linkage to libtesseract. It also specifies installation rules for runtime, library, and archive destinations, and generates an export header. ```cmake # ############################################################################## # LIBRARY common_training # ############################################################################## set(COMMON_TRAINING_SRC common/commandlineflags.cpp common/commandlineflags.h common/commontraining.cpp common/commontraining.h common/ctc.cpp common/ctc.h common/networkbuilder.cpp common/networkbuilder.h) if(NOT DISABLED_LEGACY_ENGINE) list( APPEND COMMON_TRAINING_SRC common/errorcounter.cpp common/errorcounter.h common/intfeaturedist.cpp common/intfeaturedist.h common/intfeaturemap.cpp common/intfeaturemap.h common/mastertrainer.cpp common/mastertrainer.h common/sampleiterator.cpp common/sampleiterator.h common/trainingsampleset.cpp common/trainingsampleset.h) endif() add_library(common_training ${COMMON_TRAINING_SRC}) target_include_directories(common_training PUBLIC common ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(common_training PUBLIC libtesseract) install( TARGETS common_training RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) generate_export_header(common_training EXPORT_MACRO_NAME TESS_COMMON_TRAINING_API) if (MSVC AND BUILD_SHARED_LIBS) install(FILES $ DESTINATION bin OPTIONAL) endif() project_group(common_training "Training Tools") ``` -------------------------------- ### Build wordlist2dawg executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt This CMake code snippet indicates the definition for the wordlist2dawg executable. Although incomplete in the provided text, it follows the pattern of defining an executable, linking libraries, grouping, and configuring installation, similar to other training tools. ```cmake # ############################################################################## # EXECUTABLE wordlist2dawg # ############################################################################## # add_executable(wordlist2dawg wordlist2dawg.cpp) # target_link_libraries(wordlist2dawg common_training) # project_group(wordlist2dawg "Training Tools") # install( # TARGETS wordlist2dawg # RUNTIME DESTINATION bin # LIBRARY DESTINATION lib # ARCHIVE DESTINATION lib) # if (MSVC) # install(FILES $ DESTINATION bin OPTIONAL) # endif() ``` -------------------------------- ### CMake: Configure Tesseract Project Setup Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Initializes CMake version policies, sets platform-specific variables like pthread, and handles the PkgConfig dependency for ICU. It includes logic for downloading and extracting ICU binaries on Windows for MSVC builds when not using system ICU. ```cmake if(NOT ${CMAKE_VERSION} VERSION_LESS "3.12.0") cmake_policy(SET CMP0074 NEW) endif() if(UNIX AND NOT ANDROID) set(LIB_pthread pthread) endif() if(SW_BUILD) set(ICU_FOUND 1) else() # NOT SW_BUILD find_package(PkgConfig) endif() # experimental # If PkgConfig is not present training tools will not be build, # so it does not make sense to set ICU. if(MSVC AND PKG_CONFIG_FOUND AND NOT SW_BUILD AND NOT USE_SYSTEM_ICU) include(CheckTypeSize) check_type_size("void *" SIZEOF_VOID_P) if(SIZEOF_VOID_P EQUAL 8) set(X64 1) set(ARCH_NAME 64) elseif(SIZEOF_VOID_P EQUAL 4) set(X86 1) set(ARCH_NAME 32) else() message(FATAL_ERROR "Cannot determine target architecture") endif() set(ICU_DIR "${CMAKE_CURRENT_BINARY_DIR}/icu") set(ICU_ARCHIVE "${ICU_DIR}/icu${ARCH_NAME}.zip") if(X86) set(ICU_HASH 45167a240b60e36b59a87eda23490ce4) else() set(ICU_HASH 480c72491576c048de1218c3c5519399) endif() message(STATUS "Downloading latest ICU binaries") set(COMPILER "msvc10") set(ICU_URL "https://github.com/unicode-org/icu/releases/download") set(ICU_R "56-1") set(ICU_V "56_1") file( DOWNLOAD "${ICU_URL}/release-${ICU_R}/icu4c-${ICU_V}-Win${ARCH_NAME}-${COMPILER}.zip" "${ICU_ARCHIVE}" SHOW_PROGRESS INACTIVITY_TIMEOUT 300 # seconds EXPECTED_HASH MD5=${ICU_HASH}) execute_process( COMMAND ${CMAKE_COMMAND} -E tar xz "${ICU_ARCHIVE}" WORKING_DIRECTORY "${ICU_DIR}" RESULT_VARIABLE __result) if(NOT __result EQUAL 0) message(FATAL_ERROR "error ${__result}") endif() set(ICU_ROOT ${ICU_DIR}/icu) endif() # experimental if(NOT SW_BUILD) if(PKG_CONFIG_FOUND) pkg_check_modules(ICU REQUIRED IMPORTED_TARGET icu-uc icu-i18n) else() find_package(ICU 52.1 COMPONENTS uc i18n) endif() if(ICU_FOUND) message(">> ICU_FOUND ${ICU_FOUND} ${ICU_VERSION} ${ICU_LIBRARIES} ${ICU_INCLUDE_DIRS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CXX_FLAGS}") else() message(">> ICU not found!") endif() endif() ``` -------------------------------- ### Build cntraining executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the build process for the cntraining executable, used for character training. It links against the common_training library and configures installation. This target is conditionally compiled if the legacy engine is not disabled. ```cmake if(NOT DISABLED_LEGACY_ENGINE) add_executable(cntraining cntraining.cpp) target_link_libraries(cntraining common_training) project_group(cntraining "Training Tools") install( TARGETS cntraining RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() endif() ``` -------------------------------- ### Advanced Tesseract OCR Command with Multiple Outputs Source: https://github.com/ub-mannheim/tesseract/wiki/FAQ-(deutsch) An extended Tesseract OCR command that processes an image from a URL, specifies the German language model ('deu'), and generates output files in multiple formats: ALTO XML, hOCR, TSV, plain text, and PDF. The output base name is 'ergebnis'. ```bash tesseract https://digi.bib.uni-mannheim.de/fileadmin/vl/ubmaosi/59088/max/59088_0008.jpg ergebnis -l deu alto hocr tsv txt pdf ``` -------------------------------- ### Tesseract Model Information and Selection Source: https://github.com/ub-mannheim/tesseract/wiki/FAQ-(deutsch) Provides guidance on Tesseract OCR models suitable for historical European texts, including Fraktur and Antiqua fonts with long s. It details specific models like 'deu_frak', 'frk', 'script/Fraktur', and models trained by UB Mannheim, explaining their characteristics, dependencies, and potential issues. ```APIDOC TesseractOCRModels: Description: Information on Tesseract models for historical European texts, including Fraktur and Antiqua fonts. ModelSelection: - deu_frak: Description: User-contributed model for Tesseract 3. Supports legacy OCR engine only. May detect character attributes like cursive or fat. Compatibility: Tesseract 3 legacy engine. - frk: Description: Standard model for German Fraktur texts. Includes a German dictionary. Has restrictions on character set and issues with 'ch' and 'ck' ligatures. Compatibility: LSTM engine. Restrictions: Character set limitations, ligature handling. - script/Fraktur: Description: Standard model for European Fraktur and historic Antiqua texts. Supports a wider character set than 'frk' but has similar issues with 'ch' and 'ck' ligatures. Compatibility: LSTM engine. Restrictions: Ligature handling. - Fraktur models from UB Mannheim: Description: Models trained by UB Mannheim, often providing better results and eliminating issues found in 'frk' and 'script/Fraktur'. They support various German umlaut variants and typically do not require a dictionary. Sources: - Based on script/Fraktur: https://ub-backup.bib.uni-mannheim.de/~stweil/ocrd-train/data/Fraktur_5000000/tessdata_fast/ - Trained from scratch (GT4HistOCR): https://ub-backup.bib.uni-mannheim.de/~stweil/ocrd-train/data/GT4HistOCR_5000000/tessdata_fast/ - Trained from Austrian newspapers (ONB): https://ub-backup.bib.uni-mannheim.de/~stweil/ocrd-train/data/ONB/tessdata_fast/ - Latest models (2021): https://ub-backup.bib.uni-mannheim.de/~stweil/tesstrain/frak2021/tessdata_fast/ Special Case: - frak2021_1.069.traineddata: Includes an added dictionary. Older Tesseract versions might show a warning about missing dictionaries, which can be ignored. Compatibility: LSTM engine. Notes: Models work without a dictionary, but some versions may include one. Installation: Command: tesseract --list-langs Description: Lists installed language models and the directory where models are located. Additional models can be placed in this directory or its subdirectories. ``` -------------------------------- ### Define and Install unicharset_extractor Executable (CMake) Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the unicharset_extractor executable, setting its source files, required C++ standard, linking dependencies, and installation paths. It also includes conditional installation of PDB files for MSVC builds. ```cmake add_executable(unicharset_extractor unicharset_extractor.cpp) target_compile_features(unicharset_extractor PRIVATE cxx_std_17) target_link_libraries(unicharset_extractor unicharset_training) project_group(unicharset_extractor "Training Tools") install( TARGETS unicharset_extractor RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() ``` -------------------------------- ### Build Project with Make Source: https://github.com/ub-mannheim/tesseract/blob/main/CONTRIBUTING.md Compiles the library and command-line interface (CLI) tools using the `make` utility. This command should be run after the `configure` script has been executed successfully. ```shell make ``` -------------------------------- ### Build Training Tools Source: https://github.com/ub-mannheim/tesseract/blob/main/CONTRIBUTING.md Compiles the training tools for the project using `make training`. This command is used when development involves creating or modifying training utilities. ```shell make training ``` -------------------------------- ### List Installed Tesseract Languages Source: https://github.com/ub-mannheim/tesseract/blob/main/CONTRIBUTING.md This command verifies that Tesseract has access to the installed language data files, such as English (eng) and OSD. It's a crucial step before reporting issues related to language processing. ```bash tesseract --list-langs ``` -------------------------------- ### Enable and Configure Testing Source: https://github.com/ub-mannheim/tesseract/blob/main/CMakeLists.txt Enables the testing framework and adds subdirectories for Google Test and custom unit tests if the BUILD_TESTS option is enabled and the necessary Google Test CMakeLists.txt is found. ```cmake if(BUILD_TESTS AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/unittest/third_party/googletest/CMakeLists.txt ) enable_testing() add_subdirectory(unittest/third_party/googletest) add_subdirectory(unittest) endif() ``` -------------------------------- ### CMake: Configure Tesseract Build and Tests Source: https://github.com/ub-mannheim/tesseract/blob/main/unittest/CMakeLists.txt This CMake script configures the build process for Tesseract, setting up common include directories, compile definitions, and linking libraries for various test executables. It conditionally includes Pango support and excludes legacy tests based on build flags. ```cmake # find_package(GTest REQUIRED) include(GoogleTest) # Todo install GoogleTests? # Set common include directories set(COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/../src/training ${CMAKE_CURRENT_SOURCE_DIR}/../src/ccutil ${CMAKE_CURRENT_SOURCE_DIR}/../src/ccstruct ${CMAKE_CURRENT_SOURCE_DIR}/../src/viewer ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../src/training/unicharset ${CMAKE_CURRENT_SOURCE_DIR}/../src/training/common ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googlemock/include) if (MSVC) set(TESSBIN_DIR ${EXECUTABLE_OUTPUT_PATH}/$) else() set(TESSBIN_DIR ${EXECUTABLE_OUTPUT_PATH}) endif() # Set common compile definitions set(COMMON_COMPILE_DEFINITIONS "-DTESTING_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../test/testing\"" "-DTESSDATA_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../tessdata\"" "-DTESSBIN_DIR=\"${TESSBIN_DIR}\"" "-DTESTDATA_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../test/testdata\"" "-DLANGDATA_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../langdata_lstm\"") file( GLOB TEST_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc") set(COMMON_LINK_LIBS libtesseract GTest::gtest_main common_training unicharset_training) set(TRAINING_TESTS commandlineflags_test.cc dawg_test.cc lstm_recode_test.cc lstm_squashed_test.cc lstm_test.cc lstm_test.cc normstrngs_test.cc unichar_test.cc unicharcompress_test.cc unicharset_test.cc validate_grapheme_test.cc validate_indic_test.cc validate_khmer_test.cc validate_myanmar_test.cc validator_test.cc) set(PANGO_TESTS ligature_table_test.cc pango_font_info_test.cc pango_font_info_test.cc stringrenderer_test.cc) set(LEGACY_TESTS applybox_test.cc bitvector_test.cc equationdetect_test.cc indexmapbidi_test.cc intfeaturemap_test.cc mastertrainer_test.cc osd_test.cc params_model_test.cc shapetable_test.cc) if(BUILD_TRAINING_TOOLS AND PANGO_FOUND) list(APPEND COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../src/training/pango ${PANGO_INCLUDE_DIRS}) else() list(REMOVE_ITEM TEST_SOURCES ${PANGO_TESTS}) endif() if(DISABLED_LEGACY_ENGINE) list(REMOVE_ITEM TEST_SOURCES ${LEGACY_TESTS}) endif() if(NOT BUILD_TRAINING_TOOLS) list(REMOVE_ITEM TEST_SOURCES ${TRAINING_TESTS}) endif() set(TATWEEL_TEST_EXTRA_SRC util/utf8/unilib.cc util/utf8/unicodetext.cc third_party/utf/rune.c) message(STATUS "Enabled tests: ${TEST_SOURCES}") foreach(test_source IN LISTS TEST_SOURCES) get_filename_component(test_name ${test_source} NAME_WE) if(${test_source} IN_LIST PANGO_TESTS) list(APPEND COMMON_LINK_LIBS pango_training ${PANGO_LIBRARIES}) endif() if(${test_name} MATCHES "tatweel_test") list(APPEND test_source ${TATWEEL_TEST_EXTRA_SRC}) list(APPEND COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/util/utf8) endif() add_executable(${test_name} ${test_source}) if(${test_name} MATCHES "progress_test") target_link_libraries(${test_name} PRIVATE GTest::gmock) endif() target_compile_definitions(${test_name} PRIVATE ${COMMON_COMPILE_DEFINITIONS}) target_include_directories(${test_name} PRIVATE ${COMMON_INCLUDE_DIRS}) target_link_libraries(${test_name} PRIVATE ${COMMON_LINK_LIBS}) add_test(NAME ${test_name} COMMAND ${test_name}) endforeach() # Discover tests gtest_discover_tests(apiexample_test baseapi_test # baseapi_thread_test) add_test(baseapi_gtests baseapi_test.cc) ``` -------------------------------- ### Add Executable unicharset_extractor Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Configures the 'unicharset_extractor' executable. It specifies the source file and installation destinations for runtime, library, and archive files. ```CMake add_executable(unicharset_extractor unicharset_extractor.cpp) ``` -------------------------------- ### Create Uninstall Target Source: https://github.com/ub-mannheim/tesseract/blob/main/CMakeLists.txt Defines a custom 'uninstall' target that executes a CMake script to remove installed files, ensuring a clean uninstallation process. ```cmake if(NOT TARGET uninstall) configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates/cmake_uninstall.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY) add_custom_target( uninstall COMMENT "Uninstall installed files" COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) endif() ``` -------------------------------- ### CMake: Build ambiguous_words Executable Source: https://github.com/ub-mannheim/tesseract/blob/main/src/training/CMakeLists.txt Defines the 'ambiguous_words' executable, linking it against the 'common_training' library. It includes installation rules for the executable and its PDB file on MSVC builds. ```cmake # ############################################################################## # EXECUTABLE ambiguous_words # ############################################################################## if(NOT DISABLED_LEGACY_ENGINE) add_executable(ambiguous_words ambiguous_words.cpp) target_link_libraries(ambiguous_words common_training) project_group(ambiguous_words "Training Tools") install( TARGETS ambiguous_words RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) if (MSVC) install(FILES $ DESTINATION bin OPTIONAL) endif() endif() ```