華為云計算 云知識 Tesseract如何配置
Tesseract如何配置

簡介

Tesseract是一個光學字符識別引擎,支持多種操作系統(tǒng)。Tesseract是基于Apache 許可證 的自由軟件,自2006年起由Google贊助開發(fā)。2006年,Tesseract被認為是最精準的開源光學字符識別引擎之一。

配置流程

1.編譯工具配置

執(zhí)行如下命令,安裝Tesseract所使用的依賴軟件包。

yum install automake libtool gcc-c++libjpeg-devel libpng-devel libtiff-devel-y

2.獲取源代碼

1)執(zhí)行如下命令,獲取Tesseract原代碼。

wget https://github.com/tesseract-ocr/tesseract/archive/4.0.0.tar.gz

2)通過華為云發(fā)放的彈性 云服務器 默認已安裝GCC安裝,無需單獨安裝配置。

3)下載tesseract依賴的圖像處理庫leptonica源碼包。

wget http://www.leptonica.org/source/leptonica-1.78.0.tar.gz

4)解壓leptonica。

tar-xvf leptonica-1.78.0.tar.gz

5)編譯和安裝leptonica。

cd leptonica-1.78.0

./configure--prefix=/usr/&&make-j4&&make install

6)配置leptonica涉及的環(huán)境變量。

vi/etc/profile

在后面插入如下內容:

export PKG_CONFIG_PATH=/usr/lib/pkgconfig

輸入“:wq”保存。

執(zhí)行如下命令使環(huán)境變量有效:

source/etc/profile

ldconfig

3.編譯源代碼

1)執(zhí)行下面命令,解壓軟件包。

cd/root

tar-xvf tesseract-4.0.0.tar.gz

2)執(zhí)行如下命令,進入Tesseract的安裝目錄。

cd tesseract-4.0.0

3)執(zhí)行以下命令,編譯安裝tesseract。

./autogen.sh&&./configure&&make-j4&&make install

4.測試已完成編譯的軟件

1)執(zhí)行如下命令,創(chuàng)建目錄。

cd/root

mkdir lang

cd lang

2)執(zhí)行如下命令,獲取Tesseract語言包。

wget https://github.com/tesseract-ocr/tessdata/archive/4.0.0.tar.gz

3)執(zhí)行如下命令,解壓語言包。

tar-zxvf tessdata-4.0.0.tar.gz

4)進入Tessdata目錄。

cd tessdata-4.0.0

5)執(zhí)行以下命令,將Tesseract安裝目錄放入復制到/usr/local/share/tessdata目錄。

cp*/usr/local/share/tessdata-r

6)執(zhí)行以下命令,將緩存數(shù)據(jù)刷新到硬盤。

sync

7)執(zhí)行如下命令,獲取kunpeng.png圖片(本文以獲取保存在華為云OBS桶上的kunpeng.bmp圖片為例)。

wget https://portal-www-software.obs.cn-north-1.myhuaweicloud.com:443/kunpeng.bmp

8)執(zhí)行如下命令,運行Tesseract識別圖片文字。

tesseract kunpeng.bmp result

9)執(zhí)行如下命令,查看 文字識別 結果。

cat result.txt

當系統(tǒng)回顯顯示如下圖片文字時,表示使用Tesseract識別文字成功。

Hello Kunpeng

問題處理

1)問題現(xiàn)象:

如在編譯leptonica時未安裝基礎包,則在測試會出現(xiàn)“Error in pixReadMemTiff:function not pres”等如下錯誤:

解決方法:

執(zhí)行如下命令,安裝各種基礎包,重新編譯leptonica。

yum install libtiff-y

yum install libtiff-devel-y

yum install libpng libpng-devel-y

yum install libjpeg-turbo libjpeg-turbo-devel-y

./configure--prefix=/usr/

make;make install

其他使用技巧

1)編譯過程中出現(xiàn)“/autogen.sh:line 59:bail_out:command not found”問題處理。

問題現(xiàn)象:

如下圖所示編譯過程出現(xiàn)"/autogen.sh:line 59:bail_out:command not found":

解決方法:

yum install libtool-y

./autogen.sh

2)依賴包leptonica系統(tǒng)未安裝問題處理。

問題現(xiàn)象:

如下圖所示編譯過程出現(xiàn)"configure:error:Leptonica 1.74 or higher is required":

問題解決(當操作系統(tǒng)是Euler時):

由于當前Euler的源中無leptonica,需要下載leptonica軟件包,手動編譯。具體方法如下:

wget https://github.com/DanBloomberg/leptonica/releases/download/1.78.0/leptonica-1.78.0.tar.gz

tar-zxvf leptonica-1.78.0.tar.gz

cd leptonica-1.78.0/

./configure--prefix=/usr/

說明:configure時把--prefix=/usr/帶上,系統(tǒng)默認路徑為/usr/local;如果采用默認路徑在編譯tesseract時,需要手動聲明環(huán)境明量。CentOS安裝完成后請手動執(zhí)行“export PKG_CONFIG_PATH=/usr/lib/pkgconfig”。

此時,如果出現(xiàn)運行./configure時,提示如下,表示無基礎包:

請安裝依賴包并重新編譯leptonica后再試:

yum install libtiff-y

yum install libtiff-devel-y

yum install libpng libpng-devel-y

yum install libjpeg-turbo libjpeg-turbo-devel-y

./configure--prefix=/usr/

make;make install

問題解決(當操作系統(tǒng)是CentOS時):

1)執(zhí)行如下命令,通過CentOS源安裝leptonica。

yum install leptonica-y

yum install leptonica-devel-y

2)重新編譯Tesseract并執(zhí)行./configure。

在執(zhí)行./configure如出現(xiàn)“Leptonica 1.74 or higher is required”,即報錯信息如下:

請通過以下方式解決:

執(zhí)行如下命令,查看leptonica版本信息。

rpm-qa|grep leptonica

系統(tǒng)回顯類似如下信息:

說明:該文檔采用的tesseract版本為4.0.0版本需要leptonica 1.74及以上版本,當前測試鯤鵬云服務器的Centos 7.5所自帶的leptonic為1.72版本,需源碼安裝leptonica高版本。

3)動態(tài)鏈接庫問題。

問題現(xiàn)象

執(zhí)行如下命令tesseract kunpeng.bmp result測試tesseract

需要執(zhí)行l(wèi)dconfig,ldconfig是一個動態(tài)鏈接庫管理命令,其目的為了讓動態(tài)鏈接庫為系統(tǒng)所共享。

ldconfig

執(zhí)行完成后,請再次測試。