docker中基於centos7.3搭建tesseract5環境以及制作鏡像


  由於需要離線安裝tesseract,於是想到用docker鏡像的方式部署tesseract。采用docker commit 基於容器構建鏡像。

  總體思路是先拉一個基礎的centos鏡像,然后啟動之后進入容器安裝好tesseract之后commit為鏡像。然后鏡像save為tar包。這樣就可以實現離線安裝。(這也可以作為常見的一種制作自己的鏡像的思路)

  其實linux環境安裝tesseract也是下面步驟,只是不用下載一個基礎的centos鏡像以及最后將容器制作為鏡像。

0.准備文件

(1)tesseract源碼文件:

git下載地址:https://github.com/tesseract-ocr/tesseract

 (2)leptonica-1.79.0.tar.gz。 tesseract依賴該項目

 (3)語言包

其實只需要 chi_sim.traineddata 和 eng.traineddata

 

可以從git下載。git有兩個版本,一個fast版本、一個best版本。

fast:https://github.com/tesseract-ocr/tessdata_fast

best:https://github.com/tesseract-ocr/tessdata_best

  這兩個的區別是fast版本語言庫文件比較小,識別速度比較快。best版本語言庫文件比較大,識別速度比較慢。識別准確率的話,待驗證。

(4)准備兩張驗證的圖片

 

1.下載centos鏡像

  最好是基於centos8鏡像,我第二次制作訓練環境的時候是采用centos8鏡像。

docker pull hub.c.163.com/library/centos:latest

 

2.啟動鏡像並進入容器查看centos版本:

C:\Users\Administrator>docker run -i -t hub.c.163.com/library/centos:latest /bin/bash
[root@86867025ffc7 /]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@86867025ffc7 /]# uname -a
Linux 86867025ffc7 4.14.154-boot2docker #1 SMP Thu Nov 14 19:19:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

 

3.將tesseract-master.zip 上傳到容器的/opt/tesseract 目錄(宿主機執行)

docker cp ./tesseract-master.zip 86867025ffc7:/opt/tesseract

 86867025ffc7是centos容器ID

 

4. 在容器中解壓

需要先下載unzip:

[root@86867025ffc7 tesseract]# yum list unzip
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.cqu.edu.cn
 * extras: mirror.lzu.edu.cn
 * updates: mirrors.njupt.edu.cn
updates/7/x86_64/primary_db                                                         | 1.3 MB  00:00:00
Available Packages
unzip.x86_64                                        6.0-21.el7                                         base
[root@86867025ffc7 tesseract]# yum install unzip

解壓:

unzip ./tesseract-master.zip

5.開始安裝編譯環境

(1)安裝編譯環境: gcc gcc-c++ make

yum install gcc gcc-c++ make  

查看g++版本,如果是4.X最好先升個到8以上版本:

[root@0f76915a8f71 tesseract]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)

(2)安裝tesseract-ocr編譯必須的包

yum install autoconf automake libtool   libjpeg-devel libpng-devel libtiff-devel

6.安裝leptonica

(1)上傳leptonica-1.79.0.tar.gz到容器中:(宿主機執行)

docker cp ./leptonica-1.79.0.tar.gz 86867025ffc7:/opt/tesseract

(2)容器中解壓並且安裝:

tar -zxvf ./leptonica-1.79.0.tar.gz

 

cd leptonica-1.79.0

 

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

(3)添加環境變量(如果vim不是命令,yum install vim 安裝一下)

vim /etc/profile 

最后添加如下內容:

export LD_LIBRARY_PATH=$LD_LIBRARY_PAYT:/usr/local/lib
export LIBLEPT_HEADERSDIR=/usr/local/include
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

 

執行如下命令使之立即生效:

source /etc/profile

 

(4)執行如下命令,確認有lept輸出,如果沒有,再檢查上面的步驟

[root@86867025ffc7 leptonica-1.79.0]# pkg-config --list-all | grep lept
lept             leptonica - An open source C library for efficient image processing and image analysis operations

 

7.安裝tesseract,進入到tesseract-master,執行如下命令:

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

 

我在makemake install的時候報錯,我升級了下GCC版本,原來的GCC版本是4.X,升級到8之后重新make && make install即可:

第一步:安裝scl源:
yum install centos-release-scl scl-utils-build
第二步: 列出scl可用源
yum list all --enablerepo='centos-sclo-rh'
yum list all --enablerepo='centos-sclo-rh' | grep "devtoolset-"
第三步: 安裝8版本的gcc、gcc-c++、gdb工具鏈(toolchian):
yum install -y devtoolset-8-toolchain
scl enable devtoolset-8 bash

 

查看gcc版本:

[root@86867025ffc7 tesseract-master]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)

 

  升級完gcc之后要重新configure和make、make install。否則會報錯無效指針。如果make、make install 執行時沒報錯就不用升級gcc。

 

8.  測試tesseract版本:

[root@86867025ffc7 tesseract-master]# tesseract -v
tesseract 5.0.0-alpha
 leptonica-1.79.0
  libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7
 Found AVX2
 Found AVX
 Found SSE
 Found OpenMP 201511

 

9.將語言包拷貝到容器中 /usr/local/share/tessdata

tesseract --list-langs

我執行的時候報錯:

*** Error in `tesseract': free(): invalid pointer: 0x000000000065bff0 ***

解決辦法:(到tesseract-master目錄重新執行configure、make、make install)

 

再次執行查看語言:

[root@86867025ffc7 tesseract-master]# tesseract --list-langs
List of available languages (2):
chi_sim
eng

 

10.識別進行驗證:將准備好的圖片上傳至容器的/opt/ocrtemplate

[root@86867025ffc7 ocrtemplate]# tesseract ./zh.png result -l chi_sim -c preserve_interword_spaces=1 --dpi 300 --oem 1
Tesseract Open Source OCR Engine v5.0.0-alpha with Leptonica
[root@86867025ffc7 ocrtemplate]# ls
result.txt  zh.png
[root@86867025ffc7 ocrtemplate]# cat result.txt
中文服務器

 

關於tesseract命令如下:

#控制台接收
tesseract ./normal.png stdout -l chi_sim

#只指定語言。指定語言需要用到chi_sim_vert.traineddata
tesseract ./normal.png chi_sim__simhei_result -l chi_sim
tesseract ./normal.png chi_sim__simhei_result -l chi_sim -c preserve_interword_spaces=1
tesseract ./normal.png chi_sim__simhei_result -l chi_sim -c preserve_interword_spaces=1 --dpi 300
tesseract ./normal.png chi_sim__simhei_result -l chi_sim -c preserve_interword_spaces=1 --dpi 300 --oem 1

# 指定 --psm 1 需要用到 osd.traineddata
tesseract ./normal.png chi_sim__simhei_result -l chi_sim -c preserve_interword_spaces=1 --dpi 300 --oem 1 --psm 1

 

11.將該容器制作為鏡像:(查到容器ID,然后commit為鏡像)

Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest
$ docker ps -a
CONTAINER ID        IMAGE                                 COMMAND             CREATED             STATUS              PORTS               NAMES
86867025ffc7        hub.c.163.com/library/centos:latest   "/bin/bash"         18 hours ago        Up 18 hours                             objective_swanson

Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest
$ docker commit 86867025ffc7 zdtesseract
sha256:46153c6cb7da8deb023aacaa46413822ee921a147151a235375e1a7f4ad64fda

Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest
$ docker images | grep zdte
zdtesseract                             latest               46153c6cb7da        About a minute ago   1.64GB

Administrator@MicroWin10-1535 MINGW64 ~/Desktop/dockertest
$ docker history zdtesseract
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
46153c6cb7da        7 minutes ago       /bin/bash                                       1.44GB
328edcd84f1b        2 years ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           2 years ago         /bin/sh -c #(nop)  LABEL name=CentOS Base Im…   0B
<missing>           2 years ago         /bin/sh -c #(nop) ADD file:63492ba809361c51e…   193MB

 

  至此,容器中安裝tesseract已經完成。實際linux中安裝tesseract也是上面的步驟。

 

  下面的操作都是為了docker方便進行的操作。如果我們想不進入容器就運行我們可能會執行如下命令:

docker run --rm -v /opt/ocrtemplate:/opt/ocrtemplate zdtesseract tesseract /opt/ocrtemplate/normal.png /opt/ocrtemplate/result -l chi_sim --oem 1 --dpi 300

  --rm 指定容器結束后自動刪除,-v 是指定宿主機和docker容器掛載目錄關系(宿主機目錄:docker容器目錄)。執行完畢后會在容器內部的/opt/ocrtemplate目錄下面生成result.txt,宿主機和容器/opt/ocrtemplate 目錄具有掛載關系,所以會在宿主機目錄也生成該文件,這就得到最終的OCR結果文件。

 

12. 進行測試

如果是windowsvirtualbox虛擬機需要先進入虛擬機。

 

docker-machine ssh default  #進入虛擬機
sudo -i    #切換用戶

 

然后執行:

root@default:/opt/ocrtemplate# docker run --rm zdtesseract tesseract -v
tesseract 5.0.0-alpha
 leptonica-1.79.0
  libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7
 Found AVX2
 Found AVX
 Found SSE
 Found OpenMP 201511
root@default:/opt/ocrtemplate# docker run --rm zdtesseract tesseract --list-langs
List of available languages (2):
chi_sim
eng

 

為了方便,我們給上面命令起個別名:

root@default:/opt/ocrtemplate# alias tesseract='docker run --rm -v /opt/ocrtemplate:/opt/ocrtemplate zdtesseract tesseract' #起別名
root@default:/opt/ocrtemplate# alias  #查看別名
alias tesseract='docker run --rm -v /opt/ocrtemplate:/opt/ocrtemplate zdtesseract tesseract'
root@default:/opt/ocrtemplate# tesseract -v  #用別名查看版本
tesseract 5.0.0-alpha
 leptonica-1.79.0
  libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7
 Found AVX2
 Found AVX
 Found SSE
 Found OpenMP 201511

識別文件進行測試:

(1)控制台接收:

tesseract /opt/ocrtemplate/normal.png stdout -l chi_sim --oem 1 --dpi 300

(2)生成文件:

tesseract /opt/ocrtemplate/normal.png /opt/ocrtemplate/result -l chi_sim --oem 1 --dpi 300

解釋:上面實際上是執行命令

docker run --rm -v /opt/ocrtemplate:/opt/ocrtemplate zdtesseract tesseract /opt/ocrtemplate/normal.png /opt/ocrtemplate/result -l chi_sim --oem 1 --dpi 300

  --rm 指定容器結束后自動刪除,-v 是指定宿主機和docker容器掛載目錄關系(宿主機目錄:docker容器目錄)。執行完畢后會在容器內部的/opt/ocrtemplate目錄下面生成result.txt,宿主機和容器/opt/ocrtemplate 目錄具有掛載關系,所以會在宿主機目錄也生成該文件,這就得到最終的文件。

 

13.將鏡像打包歸檔,便於離線安裝

docker save -o zdtesseract.tar zdtesseract

 

  接下來tar包可以隨便遷移,離線安裝。

  當然可以提交到鏡像倉庫。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM