從 Git 安裝 Tesseract
目錄
以下是從 git 儲存庫安裝 Tesseract 的說明。您應該準備好面對意料之外的問題。從原始碼建置 Tesseract 需要良好 C++17 支援的 C++ 編譯器。
使用 Autoconf 工具安裝
為了做到這一點,您必須安裝 automake、libtool、leptonica、make 和 pkg-config。此外,您還需要 Git 和 C++ 編譯器。
在 Debian 或 Ubuntu 上,您可以使用以下方式安裝所有需要的套件
apt-get install automake ca-certificates g++ git libtool libleptonica-dev make pkg-config
可選的 manpage 是使用 asciidoc 建置的
apt-get install --no-install-recommends asciidoc docbook-xsl xsltproc
如果您也想建置 Tesseract 訓練工具,您還需要 Pango
apt-get install libpango1.0-dev
之後,若要將 master 分支複製到您的電腦,請執行以下操作
git clone https://github.com/tesseract-ocr/tesseract.git
或進行淺層複製,將提交歷史記錄截斷為僅保留最新的提交
git clone --depth 1 https://github.com/tesseract-ocr/tesseract.git
或複製不同的分支/版本
git clone https://github.com/tesseract-ocr/tesseract.git --branch <branchName> --single-branch
注意:您在建置 GitHub 上的最新版本時可能會遇到問題。如果是這種情況,請從此處下載最新的發佈版本:https://github.com/tesseract-ocr/tesseract/releases。
注意:Tesseract 需要 Leptonica v1.74 或更新版本。如果您的系統只有較舊版本的 Leptonica,您必須從 DanBloomberg/leptonica 提供的原始碼手動編譯。
最後,執行這些指令
cd tesseract
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
重要:請參閱以下「安裝後說明」章節。
如果您收到此錯誤
make all-recursive
Making all in ccstruct
/bin/sh ../libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -
I.. -I../ccutil -I../cutil -I../image -I../viewer -I/opt/local/
include -I/usr/local/include/leptonica -g -O2 -MT blobbox.lo -MD -MP -
MF .deps/blobbox.Tpo -c -o blobbox.lo blobbox.cpp
mv -f .deps/blobbox.Tpo .deps/blobbox.Plo
mv: rename .deps/blobbox.Tpo to .deps/blobbox.Plo: No such file or
directory
make[3]: *** [blobbox.lo] Error 1
make[2]: *** [all-recursive] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
請嘗試在執行 ./autogen.sh
之後執行 autoreconf -i
。
使用訓練工具建置
以上操作不會建置 Tesseract 訓練工具。如果您計劃安裝訓練工具,您還需要以下程式庫
sudo apt-get install libicu-dev
sudo apt-get install libpango1.0-dev
sudo apt-get install libcairo2-dev
若要使用訓練工具建置 Tesseract,請執行以下操作
cd tesseract
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
make training
sudo make training-install
您可以根據需要為 configure 指定額外的選項。例如:
./configure --disable-openmp --disable-debug --disable-opencl --disable-graphics --disable-shared 'CXXFLAGS=-g -O2 -Wall -Wextra -Wpedantic'
安裝後說明
Tesseract 的安裝分為兩個部分,引擎本身和語言的訓練資料。
以上安裝指令會安裝 Tesseract 引擎和訓練工具。它們也會安裝組態檔案,例如:用於 pdf、tsv、hocr、alto
等輸出的檔案,或用於建立框檔案的檔案,例如 lstmbox、wordstrbox
。除了這些之外,還需要語言的訓練資料才能辨識影像中的文字。
在 tesseract-ocr GitHub 儲存庫中,有三種訓練資料檔案 (tessdata、tessdata_best 和 tessdata_fast) 可用於 130 多種語言和 35 種以上的腳本。
當在 Linux 上從原始碼建置時,tessdata 組態將安裝在 /usr/local/share/tessdata
中,除非您使用 ./configure --prefix=/usr
。一旦 tesseract 完成安裝,請務必下載您需要的語言訓練資料檔案,並將它們放在此 tessdata 目錄 (/usr/local/share/tessdata
) 中。
如果您想要同時支援舊版 (–oem 0) 和 LSTM (–oem 1) 引擎,請從 tessdata 下載訓練資料檔案。
如果您只想要支援 LSTM 引擎 (–oem 1),請使用 tessdata_best 或 tessdata_fast 中的訓練資料檔案。
請務必使用下載連結或 wget raw
檔案。例如,這是 tessdata 儲存庫中的 eng.traineddata 直接下載連結,它支援 tesseract 的舊版和 LSTM 引擎。
現在您已準備好使用 tesseract
!
從 https://github.com/zdenop/tessdata_downloader 可以取得用於下載訓練資料檔案的 python3 腳本
如果您想要將訓練資料檔案放在與安裝期間定義的目錄不同的目錄中,即 /usr/local/share/tessdata
,那麼您需要設定一個名為 TESSDATA_PREFIX
的本機變數,以指向 tesseract tessdata
目錄。
-
範例:在 Linux Ubuntu 上,透過將以下內容新增至檔案底部來修改您的
~/.bashrc
檔案。根據您的情況修改路徑export TESSDATA_PREFIX="/home/$USER/Downloads/tesseract/tesseract-4.1.0/tessdata"
-
然後,關閉並重新開啟您的終端機,使其生效,或僅呼叫
. ~/.bashrc
或export ~/.bashrc
(相同作用) ,使其立即在目前的終端機中生效。 -
將您需要的任何語言訓練資料也放入此
tessdata
資料夾中。例如,英文的稱為eng.traineddata
。從此處的 tessdata 儲存庫下載它,並將其移動到您剛在上述TESSDATA_PREFIX
變數中指定的tessdata
目錄。
使用 TensorFlow 建置
使用 TensorFlow 建置需要 Protocol Buffers 和 TensorFlow 的額外套件。在 Debian 或 Ubuntu 上,您可以使用以下方式安裝它們
apt-get install libprotoc-dev libtensorflow-dev
如果找到必要的開發檔案,所有建置都會自動使用 TensorFlow 建置 Tesseract 和訓練工具。這可以被覆寫
# Enforce build with TensorFlow (will fail if requirements are not met).
./configure --with-tensorflow [...]
# Don't build with TensorFlow.
./configure --without-tensorflow [...]
使用 TensorFlow 建置支援是 Git master 中的新功能。產生的程式碼仍未經過測試。
單元測試建置
此類建置可以用於執行自動回歸測試,這些測試有額外的需求。這包括訓練工具的額外相依性 (如上所述) ,以及下載所有 git 子模組以及模型儲存庫 (*.traineddata
)
# Clone the Tesseract source tree:
git clone https://github.com/tesseract-ocr/tesseract.git
# Clone repositories with model files (from the same directory):
git clone https://github.com/tesseract-ocr/tessdata.git
git clone https://github.com/tesseract-ocr/tessdata_best.git
git clone https://github.com/tesseract-ocr/tessdata_fast.git
git clone https://github.com/tesseract-ocr/langdata_lstm.git
# Change to the Tesseract source tree and get all submodules:
cd tesseract
git submodule update --init
# Build the training tools (see above). Here we use a release built with sanitizers:
./autogen.sh
mkdir -p bin/unittest
cd bin/unittest
../../configure --disable-shared 'CXXFLAGS=-g -O2 -Wall -Wextra -Wpedantic -fsanitize=address,undefined -fstack-protector-strong -ftrapv'
make training
# Run the unit tests:
make check
cd ../..
這會在 bin/unittest/unittest
下建立所有單元測試 (包括個別測試和累積測試) 的記錄檔。它們也可以單獨執行,例如
bin/unittest/unittest/stringrenderer_test
失敗的測試會明顯顯示為分段錯誤或 SIGILL 處理常式 (取決於平台)。
偵錯建置
此類建置產生的 Tesseract 二進位檔執行速度非常慢。它們對生產沒有用處,但有助於尋找或分析軟體問題。這是一個經過驗證的建置序列
cd tesseract
./autogen.sh
mkdir -p bin/debug
cd bin/debug
../../configure --enable-debug --disable-shared 'CXXFLAGS=-g -O0 -Wall -Wextra -Wpedantic -fsanitize=address,undefined -fstack-protector-strong -ftrapv'
# Build tesseract and training tools. Run `make` if you don't need the training tools.
make training
cd ../..
這會啟用偵錯程式碼,不使用共用的 Tesseract 程式庫 (這使得可以在不安裝的情況下執行 tesseract
) ,停用編譯器最佳化 (允許使用 gdb
進行更好的偵錯) ,啟用大量編譯器警告,並啟用多個執行時間檢查。
效能分析建置
此類建置可以用於調查效能問題。Tesseract 的執行速度會比沒有效能分析時慢,但在可接受的速度範圍內。這是一個經過驗證的建置序列
cd tesseract
./autogen.sh
mkdir -p bin/profiling
cd bin/profiling
../../configure --disable-shared 'CXXFLAGS=-g -p -O2 -Wall -Wextra -Wpedantic'
# Build tesseract and training tools. Run `make` if you don't need the training tools.
make training
cd ../..
這不使用共用的 Tesseract 程式庫 (這使得可以在不安裝的情況下執行 tesseract
) ,啟用效能分析程式碼,啟用編譯器最佳化,並啟用大量編譯器警告。
您可以選擇透過新增 --enable-debug
並將 -O2
替換為 -O0
,將此功能與偵錯程式碼一起使用。
當 Tesseract 終止時,效能分析程式碼會在目前目錄中建立一個名為 gmon.out
的檔案。GNU gprof 用於顯示該檔案中的效能分析資訊。
大量生產的發佈版本
預設建置會建立一個 Tesseract 可執行檔,該執行檔適用於處理單一影像。然後,Tesseract 會使用 4 個 CPU 核心,以盡可能快的速度取得 OCR 結果。
對於數百或數千個影像的大量生產,預設值是不好的,因為多執行緒執行會產生非常大的額外負荷。最好執行單執行緒的 Tesseract 執行個體,以便每個可用的 CPU 核心都會處理不同的影像。
這是一個經過驗證的建置序列
cd tesseract
./autogen.sh
mkdir -p bin/release
cd bin/release
../../configure --disable-openmp --disable-shared 'CXXFLAGS=-g -O2 -fno-math-errno -Wall -Wextra -Wpedantic'
# Build tesseract and training tools. Run `make` if you don't need the training tools.
make training
cd ../..
這會停用 OpenMP (多執行緒) ,不使用共用的 Tesseract 程式庫 (這使得可以在不安裝的情況下執行 tesseract
) ,啟用編譯器最佳化,停用數學函數的 errno
設定 (執行速度更快!) ,並啟用大量編譯器警告。
用於模糊測試的建置
模糊測試用於測試 Tesseract API 是否有錯誤。Tesseract 使用 OSS-Fuzz,但模糊測試也可以在本機執行。需要較新的 Clang++ 編譯器。
建置範例 (修正可用 clang++ 的 CXX 值)
cd tesseract
./autogen.sh
mkdir -p bin/fuzzer
cd bin/fuzzer
../../configure --disable-openmp --disable-shared CXX=clang++-7 CXXFLAGS='-g -O2 -Wall -Wextra -Wpedantic -D_GLIBCXX_DEBUG -fsanitize=fuzzer-no-link,address,undefined'
# Build the fuzzer executable.
make fuzzer-api
cd ../..
範例 (顯示說明資訊)
bin/fuzzer/fuzzer-api -help=1
範例 (使用已知的測試案例執行模糊測試器)
bin/fuzzer/fuzzer-api clusterfuzz-testcase-minimized-fuzzer-api-5670045835853824
範例 (執行模糊測試器以尋找新錯誤)
nice bin/fuzzer/fuzzer-api -jobs=16 -workers=16
使用 Windows Visual Studio 建置
請參閱針對 Windows 編譯。