最近要实现OCR功能, 决定用tesseract。最新的稳定版本是3.04. github上有很好的安装教程,但是公司的server是centos 6,和ubuntu还有一点区别。
leptonica-1.73
tessaract需要用到leptonica,安装leptonica需要另外一些库,ubuntu下的这些库,不知为何centos要用不同的名字:
#ubuntu sudo apt-get install autoconf automake libtool sudo apt-get install pkg-config sudo apt-get install libpng12-dev sudo apt-get install libjpeg8-dev sudo apt-get install libtiff5-dev sudo apt-get install zlib1g-dev
#centos sudo yum -y install libtiff libtiff-devel libjpeg-devel libpng-deve
下载
git clone https://github.com/DanBloomberg/leptonica.git git tag -l git checkout tags/v1.73 git checkout -b 1.73
官方安装教程。1.73提供了cmake编译,但是不知道问什么不支持cmake之后不能make install(??)。所以最后还是选择autoconf,之后configure指定安装路径(统一用/local/ydong/ocrlib/libs),得到头文件和库:
./configure --prefix=/local/ydong/ocrlib/libs make make install
Tesseract 3.04
下载:
git clone https://github.com/tesseract-ocr/tesseract.git git branch -v -a git checkout -b 3.04 remotes/origin/3.04
安装
./autogen.sh \\ 生成configure file export LIBS="stdc++" \\ 或者添加 LIBS=“stdc++”到configure file的头部。否则会编译出错 ./configure --with-extra-libraries=/local/ydong/ocrlib/libs/lib/ --prefix=/local/ydong/ocrlib/libs/ \\extra lib里是leptonica make make install
下载pretrained model
cd /local/ydong/ocrlib/ mkdir data cd data wget https://github.com/tesseract-ocr/tessdata/archive/master.zip unzip master.zip mv /local/ydong/ocrlib/data/tessdata-master /local/ydong/ocrlib/data/tessdata export TESSDATA_PREFIX=/local/ydong/ocrlib/data //用环境变量指定model的路径
调用API
官方提供了一个简单的调用API的教程。试着编译一下,顺便复习下Makefile和LD_LIBARRY_PATH etc.
cd /local/ydong/ocrlib/ mkdir app
main.cpp:
#include <tesseract/baseapi.h> #include <leptonica/allheaders.h> int main() { char *outText; tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); // Initialize tesseract-ocr with English,without specifying tessdata path if (api->Init(NULL,"eng")) { fprintf(stderr,"Could not initialize tesseract.\n"); exit(1); } // Open input image with leptonica library Pix *image = pixRead("/local/ydong/ocrlib/tesseract/testing/phototest.tif"); //use your own path api->SetImage(image); // Get OCR result outText = api->GetUTF8Text(); printf("OCR output:\n%s",outText); // Destroy used object and release memory api->End(); delete [] outText; pixDestroy(&image); return 0; }
Makefile:
CXX = g++ CFLAGS = -g -O2 -std=c++11 LIBS = -lstdc++ -llept -ltesseract INCLUDES = -I/local/ydong/ocrlib/libs/include/ LFLAGS = -L/local/ydong/ocrlib/libs/lib/ all: ocr-test ocr-test: main.cpp $(CXX) $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -o ocr-test main.cpp
编译运行:
make ./ocr-test
动态库链接,rpath,LD_LIBRARY_PATH
查找动态库的优先级,rpath优先级高于LD_LIBRARY_PATH
查看ocr-test用到的动态库
ldd ocr-test ... liblept.so.5 => /local/ydong/ocrlib/libs/lib/liblept.so.5 (0x00007f26891a0000) libtesseract.so.3 => /local/ydong/ocrlib/libs/lib/libtesseract.so.3 (0x00007f2688a03000) ...
查看rpath
chrpath -l ocr-test ocr-test: no rpath or runpath tag found.
rpath会领写一篇。
rpath
-Wl,-rpath -Wl,/local/ydong/ocrlib/libs/lib
$origin
g++ -g -O2 -std=c++11 -o .libs/tesseract tesseract-tesseractmain.o -L/local/ydong/ocrlib/libs/lib/ ./.libs\ /libtesseract.so -lrt /local/ydong/ocrlib/libs/lib/liblept.so -lm -lz -lpng -ljpeg -ltiff -lpthread -Wl,-rpath -Wl\,/local/ydong/ocrlib/libs/lib原文链接:https://www.f2er.com/centos/380230.html