Compiling - kana112233/tesseract GitHub Wiki

#各种平台的编译指南

**注意:**此wiki希望您熟悉操作系统上的编译软件.

使用与building leptonica相同的工具来构建tesseract.

目录

Linux

To install Tesseract 4.x you can simply run the following command on your Ubuntu 18.xx bionic:

sudo apt install tesseract-ocr

If you wish to install the Developer Tools which can be used for training, run the following command:

sudo apt install libtesseract-dev 

以下说明适用于在Linux上构建,也可以应用于其他类UNIX操作系统.

###依赖关系

  • C和C ++的编译器:GCC或Clang

  • GNU Autotools:autoconf,automake,libtool

  • pkg-config

  • Leptonica

  • libpng,libjpeg,libtiff

Ubuntu

If they are not already installed, you need the following libraries (Ubuntu 16.04/14.04):

sudo apt-get install g++ # or clang++ (presumably)
sudo apt-get install autoconf automake libtool
sudo apt-get install pkg-config
sudo apt-get install libpng-dev
sudo apt-get install libjpeg8-dev
sudo apt-get install libtiff5-dev
sudo apt-get install zlib1g-dev

if you plan to install the training tools, you also need the following libraries:

sudo apt-get install libicu-dev
sudo apt-get install libpango1.0-dev
sudo apt-get install libcairo2-dev

Leptonica

您还需要安装Leptonica. 在编译Tesseract之前,确保已安装Leptonica的开发标题.

Tesseract版本和Leptonica的最低版本要求:

Tesseract Leptonica Ubuntu
4.00 1.74.2 Ubuntu 18.04
3.05 1.74.0 Must build from source
3.04 1.71 Ubuntu 16.04
3.03 1.70 Ubuntu 14.04
3.02 1.69 Ubuntu 12.04
3.01 1.67

一种选择是安装发行版的Leptonica软件包:

sudo apt-get install libleptonica-dev

但是如果您使用的是旧版本的Linux,Leptonica版本可能太旧了,因此您需要从源代码构建.

来源是https://github.com/DanBloomberg/leptonica. 有关构建的说明,请参阅Leptonica README.

请注意,如果从源代码构建Leptonica,您可能需要确保/usr/local/lib位于库路径中. 这是一个标准的Linux错误,Stackoverflow上的信息非常有用.

##从Git安装Tesseract

请按照https://github.com/tesseract-ocr/tesseract/wiki/Compiling--GitInstallation中的说明进行操作 93-GitInstallation)

另请阅读安装说明

##安装在别处/没有root

Tesseract可以配置为在任何地方安装,这样就可以在没有root访问权限的情况下安装它.

要在$ HOME/local中安装它:

./autogen.sh
./configure --prefix=$HOME/local/
make
make install

要使用也安装在$ HOME/local中的Leptonica库在$ HOME/local中安装它:

./autogen.sh
LIBLEPT_HEADERSDIR=$HOME/local/include ./configure \
  --prefix=$HOME/local/ --with-extra-libraries=$HOME/local/lib
make
make install

在某些系统中,您可能还需要在运行configure脚本之前指定pkg-config的路径:

export PKG_CONFIG_PATH=$HOME/local/lib/pkgconfig

##在Ubuntu 16.xx上编译Tesseract 4.0和Leptonica 1.7.4的过程的视频表示

*视频源自Leptonica 1.7.4版    *视频源自Tesseract-OCR 4.0构建

##语言数据

*下载您感兴趣的语言的数据文件.    *将其移至tessdata目录(例如'mv tessdata $ TESSDATA \ _PREFIX',如果定义为TESSDATA_PREFIX)

You can also use:

export TESSDATA_PREFIX=/some/path/to/tessdata

to point to your tessdata directory (example: if your tessdata path is '/usr/local/share/tessdata' you have to use 'export TESSDATA_PREFIX='/usr/local/share/').

#Windows

master branch,3.05及更高版本

####使用Tesseract

**! 重要!!! **要在您的应用程序中使用Tesseract(包括tess或将其链接到您的应用程序),请参阅这个非常简单的示例https://github.com/tesseract-ocr/tesseract/wiki/User-App-Example .

####构建最新的库

  1. 下载来自https:/的最新CPPAN(C ++ Archive Networkhttps://cppan.org/)客户端 /cppan.org/客户/`.

2.运行cppan --build pvt.cppan.demo.google.tesseract.tesseract-master.

####对于使用tesseract的visual studio项目

1.安装VcpkgVisual C ++包管理器.

2.运行vcpkg install tesseract:x64-windows for 64-bit. 使用--head作为主分支.

####静态链接

要构建一个自包含的tesseract.exe可执行文件(没有任何DLL或运行时依赖项),请使用上面的Vcpkg并使用以下命令:

  • vcpkg install tesseract:x64-windows-static for 64-bit

  • vcpkg install tesseract:x86-windows-static for 32-bit

使用--head作为主分支. 它可能仍然需要一个用于OpenMP运行时的DLL,vcomp140.dll(您可以在Visual C ++ Redistributable 2015中找到它).

####构建培训工具

今天,可以使用Visual Studio在Windows上构建一整套tess培训工具.

最新版本(Win10,VS2015/VS2017)更可取.

去做这个:

  1. 下载来自https:/的最新CPPAN(C ++ Archive Networkhttps://cppan.org/)客户端 /cppan.org/客户/`.

2.运行cppan --build pvt.cppan.demo.google.tesseract-master.

####开发Tesseract

出于开发目的 Tesseract本身执行以下步骤:

1.下载并安装Git,CMake并将它们放入PATH.

  1. 下载来自https:/的最新CPPAN(C ++ Archive Networkhttps://cppan.org/`)客户端 /cppan.org/客户/`. CPPAN是源包分发系统. 在PATH中添加CPPAN客户端. (需要VS2015 redist.)

3.如果您有发布存档,请将其解压缩到tesseract目录.

如果您正在使用master branch(4.0)运行

git clone https://github.com/tesseract-ocr/tesseract tesseract

4.跑

```
cd tesseract
cppan
mkdir build && cd build
cmake ..
```
  1. Build a solution (tesseract.sln) in your Visual Studio version. If you want to build and install from command line (e.g. Release build) you can use this command:
cmake --build . --config Release --target install

If you want to install to other directory that C:\Program Files (you will need admin right for this), you need to specify install path during configuration:

cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=inst

For development purposes of training tools after cloning a repo from previous paragraph, run

cppan --build .

您将在Tesseract的根目录中看到解决方案链接.

##构建x64平台

If you're building with cppan+cmake, run cmake as follows:

mkdir win64 && cd win64
cppan ..
cmake .. -G "Visual Studio 14 2015 Win64"

If you're building with cppan, edit cppan.yml and uncomment this line:

#generator: Visual Studio 14 2015 Win64 -> generator: Visual Studio 14 2015 Win64

然后运行cppan --generate . - 它将为您创建解决方案链接.

(对于VS2017,请使用'15 2017'而不是'2015 2015'.)

3.05

如果您有Visual Studio 2015,请查看https://github.com/peirick/VS2015_Tesseract存储库以获取[Visual Studio 2015项目的Tessearct和依赖项.](https://github.com/peirick/VS2015_Tesseract)并单击build_tesseract .蝙蝠. 之后,您仍然需要下载语言包.

3.03rc-1

看看博客如何使用Visual Studio 2013构建Tesseract 3.03.

3.02

对于tesseract-ocr 3.02,请按照[Visual Studio 2008开发人员Notes for Tesseract-OCR]中的说明进行操作(http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/setup.html#using-the-latest -tesseractocr来源).

3.01

从SourceForge上的Downloads Archive页面下载这些软件包:

*tesseract-3.01.tar.gz - Tesseract来源    *tesseract-3.01-win_vs.zip - Visual Studio(2008和2010)解决方案,带有必要的库    *tesseract-ocr-3.01.eng.tar.gz - Tesseract的英语语言文件(或下载其他语言培训文件)

将它们解压缩到一个目录(例如tesseract-3.01). 请注意,tesseract-ocr-3.01.eng.tar.gz将根目录命名为''tesseract-ocr',而不是`'tesseract-3.01'.

Windows相关文件位于vs2008目录中(例如'tesseract-3.01 \ vs2008'). 与往常一样的构建过程适用:使用VC ++ Express 2008打开tesseract.sln并构建所有(或只是Tesseract.)它应该编译(至少在发布模式下)而不必进一步安装. 包括dll依赖项和Leptonica. 输出将在tesseract-3.01 \ vs2008 \ bin(或tesseract-3.01 \ vs2008 \ bin.rd或tesseract-3.01 \ vs2008 \ bin.dbg,基于配置构建).

Mingw + Msys

对于Mingw + Msys看看博客用Mingw + Msys编译Leptonica和Tesseract-ocr .

Msys2

https://msys2.github.io/下载并安装MSYS2安装程序

如果您希望从PKGBUILD构建,则需要安装的核心软件包组是:

  • 任何建筑物的基础设施

  • 用于构建msys2包的msys2-devel

  • 用于构建mingw32软件包的mingw-w64-i686-toolchain

  • 用于构建mingw64包的mingw-w64-x86_64工具链

要构建tesseract-ocr发行包,请使用https://github.com/Alexpux/MINGW-packages/tree/master/mingw-w64-tesseract-ocr中的PKGBUILD.

Cygwin

要在Cygwin上构建,请查看博客如何在Cygwin上构建Tesseract.

Tesseract以及3.04.00以后的培训实用程序可作为Cygwin软件包提供.

Tesseract specific packages to be installed:

tesseract-ocr                           3.04.01-1
tesseract-ocr-eng                       3.04-1
tesseract-training-core                 3.04-1
tesseract-training-eng                  3.04-1
tesseract-training-util                 3.04.01-1

Mingw-w64

Mingw-w64允许为Windows构建32位或64位可执行文件.

它可以用于Windows上的本机编译,

但也适用于Linux上的交叉编译(比本机编译更容易,更快).

大多数大型Linux发行版都已包含具有交叉构建所需工具的软件包.

在构建Tesseract之前,有必要构建一些先决条件.

对于Debian和类似的发行版(例如Ubuntu),可以像这样安装交叉工具:

# Development environment targeting 32- and 64-bit Windows (required)
apt-get install mingw-w64
# Development tools for 32- and 64-bit Windows (optional)
apt-get install mingw-w64-tools

这些先决条件是必需的:

苹果系统

除了Apple的Xcode之外,通常还需要像Fink,Homebrew或MacPorts这样的包管理器.

Xcode和相关的命令行工具提供了编译器(llvm-gcc)和链接器,还提供了像zlib这样的库. 包管理器提供不属于Xcode的免费软件包.

可以通过运行xcode-select --install来安装Xcode命令行工具.

请注意,Tesseract 4可以使用OpenMP支持构建,但这需要额外的安装.

与Fink的## macOS

Fink(截至2017-04)既没有提供Leptonica也没有提供Tesseract培训工具所需的软件包,

因此不建议用于构建Tesseract.

使用MacPorts的## macOS

###准备对OpenMP的支持(可选)

Install OpenMP:

sudo port install libomp

不再需要以下手动获取,编译和安装OpenMP的方法:

# Install cmake if it is not available.
sudo port install cmake
git clone https://github.com/llvm-mirror/openmp.git
cd openmp
mkdir build
cd build
cmake ..
make
sudo make install

###安装所需的包

sudo port install autoconf \
                  automake \
                  libtool \
                  pkgconfig \
                  leptonica

###编译

编译本身依赖于Autotools套件:

  git clone https://github.com/tesseract-ocr/tesseract.git
  cd tesseract
  ./autogen.sh
  ./configure
  make
  sudo make install

如果要支持多线程,则必须先安装OpenMP(参见上文)

并告诉编译器和链接器如何激活OpenMP支持.

这是通过将该信息添加到configure的选项来完成的:

  ./configure CXXFLAGS="-Xpreprocessor -fopenmp -I/opt/local/include/libomp -Wall -O2" LDFLAGS=-L/opt/local/lib/libomp LIBS=-lomp

如果make命令的编译失败,并且libtool错误的指令错误,你可能正在构建MacPort的g ++'编译器,[已知问题](https://github.com/tesseract-ocr/tesseract/拉/1474). 社区建议使用clang,但是g ++`的解决方法是重新配置构建:

./configure CXXFLAGS = -Wa,-q

然后继续make.

###使用培训工具安装Tesseract

在上面的培训工具中没有安装. 您不仅可以安装Tesseract,还可以安装下面的培训工具.

####安装培训工具所需的软件包

sudo port install cairo pango 
sudo port install icu +devel

Build and Install

git clone https://github.com/tesseract-ocr/tesseract/
cd tesseract
./autogen.sh
./configure
make training
sudo make install training-install

macOS with Homebrew

###安装依赖项

brew install automake autoconf libtool
brew install pkgconfig
brew install icu4c
brew install leptonica
brew install gcc

###使用培训工具安装Tesseract

In the above, training tool dependencies are not installed. You can install them like below.

brew install pango

###编译

截至2017年1月,clang构建但OpenMP将仅使用单个线程,从而降低性能. 为获得最佳效果,请使用gcc.

CPPFLAGSLDFLAGS的确切值可以从brew info icu4c中读取. 如果要构建训练模块,还应将pango库链接为-L/usr/local/Cellar/pango/1.42.4/lib.

git clone https://github.com/tesseract-ocr/tesseract/
cd tesseract
./autogen.sh
./configure CC=gcc-8 CXX=g++-8 CPPFLAGS=-I/usr/local/opt/icu4c/include LDFLAGS=-L/usr/local/opt/icu4c/lib
make -j
sudo make install  # if desired
make training # if installed with training dependencies

#常见错误

  • To fix this error
./configure: line 4237: syntax error near unexpected token `-mavx,'
./configure: line 4237: `AX_CHECK_COMPILE_FLAG(-mavx, avx=1, avx=0)'

ensure that autoconf-archive is installed. Don't forget to run ./autogen.sh after the installation of autoconf-archive. Note this error happens often under CentOS, where autoconf-archive is missing and no package is available. Some projects help with installing.

来自GitHub的最新代码不需要autoconf-archive.

*如果配置失败并出现此类错误“configure:error:需要Leptonica 1.74或更高版本.” 尝试安装libleptonica-dev软件包.

*如果您确定已经安装了leptonica(例如在/usr/local中),那么pkg-config可能不会查看您的安装文件夹(请查看pkg-config --variable pc_path pkg-config).

解决方案是设置PKG_CONFIG_PATH:例如:PKG_CONFIG_PATH =/usr/local/lib/pkgconfig

*在某些系统上,autotools不会自动创建m4目录(给出错误:“configure:error:找不到宏目录'm4'”).

在这种情况下,您必须创建m4目录(mkdir m4),然后以./configure开头重新运行上述命令.

#杂项