接觸Tesseract沒幾天,只是想用它做一下簡單的字符識別,雖然是HP 1995年的產品,后面google公開來使用,效果還是可以的,如果對字符分割好,僅僅只作為單個字符的識別,如果作為版面分析來使用的話,效果不算太好。這個庫國內的使用資料還是蠻少,如果在linux下面使用的話,還簡單點,但是在win下面的使用,資料少,也沒找到自己使用的簡介。廢話不說,我把我這幾天折騰的過程說一下。
一 。tesseract3.01在VS2008下面編譯生成DLL,LIB的過程
(1)和VS2008的使用,官方安裝過程可以參考:http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/setup.html
安裝需要的軟件:VS2008,Leptonica 1.68源碼,Tesseract-OCR Visual Studio 2008 source files ,Tesseract-OCR source ,在使用的時候先建立C:\BuildFolder文件夾,將Leptonica 1.68源碼,Tesseract-OCR Visual Studio 2008 source files ,Tesseract-OCR source解壓在這個文件夾下:文件夾中出現下面的結構:

C:\BuildFolder include\ leptonica\ stdint.h lib\ tesseract-3.01\ vs2008\ cntraining\ combine_tessdata\ libtesseract\ libtesseract.vcproj mftraining\ port\ tesseract\ tesseract.vcproj unicharset_extractor\ wordlist2dawg\ tesseract.sln
按照官方安裝文檔(地址http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/building.html)說明,打開C:\BuildFolder\tesseract-3.0x\vs2008\tesseract.sln,選擇編譯屬性,應該有DLL_Debug, DLL_Release, LIB_Debug, LIB_Release幾項選擇,但是實際上,使用官方的工程打開以后,選擇編譯屬性里面只有:debug,release.static,release.Dynamic這幾項,使用bulid編譯以后也並沒有dll文件,z只有靜態鏈接庫LIB文件,官方文檔也3.01版本在VS2008下不能使用DLL“The dll isn't supported in Tesseract-OCR 3.00/3.01.”對於這個問題,可以參考一下鏈接(http://groups.google.com/group/tesseract-dev/browse_thread/thread/75be5c97eb4d1b3c/a59952174f78c000?#a59952174f78c000),在VS2008中使用的時候,官方文檔也沒有做太多的說明,弄了一個晚上也沒弄明白。
后來在國外下載一個別人對源代碼框架修改一下,使用起來更加方便一點,下載地址:https://github.com/tinku99/tesseract-ocr ,包含了解壓好的Leptonica 1.68源碼,Tesseract-OCR Visual Studio 2008 source files ,Tesseract-OCR source,但是也對框架進行了修改,並且修改了代碼里面關於DLL輸出的問題,用python對文件做了一些歸類,省了很多麻煩。整個過程的安裝如下:
(1)這個工程要使用Python編譯器,所有要首先安裝 Python2.7(win平台)軟件,安裝在C盤,並且在VS2008上進行配置,將Python2.7安裝在C盤,打開VS2008,工具-》選項-》項目和解決方案-》VC++目錄,然后在“平台”選項下,選擇“Win32”;“顯示一下內容的目錄”選項下,然后選 擇“包含文件”,在下面的目錄中,添加“C:/Python27/include”;選擇“庫文件”,添“C:/Python27 /libs”。另外需要注意的問題是,VS2008在DEBUG下面會有“找不到python25_d.lib”的問題,可以做一下修改:在python目錄include文件夾下,打開pyconfig.h,
找到如下語句
# ifdef _DEBUG # pragma comment(lib,"python25_d.lib") # else # pragma comment(lib,"python25.lib") # endif /* _DEBUG */ 將python25_d.lib改成python25.lib b)找到 #ifdef _DEBUG # define Py_DEBUG #endif 將其用/**/注釋掉就可以了。
(2)代碼解壓后在C:\BuildFolder下:結構如下,

C:\BuildFolder include\ leptonica\ lib\ tesseract-3.01\ api\ ccmain\ ccstruct\ ccutil\ classify\ config\ contrib\ cube\ cutil\ dict\ doc\ image\ java\ image\ neural_networks\ tessdata\ testing\ textord\ training\ viewer\ vs2008\ wordrec\
打開C:\BuildFolder\tesseract-3.0x\vs2008\tesseract.sln,選擇DLL_Debug, DLL_Release, LIB_Debug, LIB_Release編譯選擇。
點擊libtesseract301工程右鍵,進行bulid,如下的DLL,LIB文件,
static libraries: libtesseract301-static.lib libtesseract301-static-debug.lib DLLs: libtesseract301.lib (import library) libtesseract301.dll libtesseract301d.lib (import library) libtesseract301d.dll
選擇soluton主功能,選擇編譯選項,編譯后得到:
LIB_Release: tesseract.exe LIB_Debug: tesseractd.exe DLL_Release: tesseract-dll.exe DLL_Debug: tesseract-dlld.exe
三 測試編譯結果
建立文件夾,C:\BuildFolder include\ lib\ tesseract-3.01\ testing,以DEBUG模式為例,將一下幾個文件copy到testing文件夾下面
liblept168d.dll
libtesseract301d.dll
tesseractd.exe
eurotext.tif(圖片)
需要注意的是:需要在testing文件夾中包含tesseract3的語言包,不然會出錯,官方下載漢語文件包,在testing文件夾下建立文件夾tessdata,將語言包解壓在這個文件夾下。
測試需要用命令行指令,在dos下,找到testing文件夾,輸入:
tesseractd.exe eurotext.tif eurotext
這個指令將輸入的圖片提取出來的字符輸出到文本文件eurotext中,測試結果如下:
輸出的文本:
The (quick) [brown] {fox} jumps!
Over the $43,456.78 <lazy> #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
Der ,,schnelle” braune Fuchs springt
fiber den faulen Hund. Le renard brun
<<rapide» saute par-dessus le chien
paresseux. La volpe marrone rapida
salta sopra il cane pigro. El zorro
marrén répido salta sobre el perro
perezoso. A raposa marrom rzipida
salta sobre 0 cfio preguieoso.
四:在VS2008下的使用方法
(1)工程設置,tool-->工程與方案里面增加tesseract3.0使用的頭文件以及庫文件
在include files里面添加有文件
C:\BuildFolder\include C:\BuildFolder\include\leptonica(and all its sub-directories) C:\BuildFolder\include\tesseract (and all its sub-directories)
在library files里面添加有文件: C:\BuildFolder\lib
(2)設置類屬性
在VS2008工程-->屬性-->C++-->preprocesser中添加宏,__MSW32__;USE_STD_NAMESPACE;TESSDLL_IMPORTS;CCUTIL_IMPORTS;LIBLEPT_IMPORTS
在VS2008工程-->屬性-->linker-->additional depandence添加附加庫:
ws2_32.lib
user32.lib
liblept$(LIBLEPT_VERSION)d.lib
libtesseract$(LIBTESS_VERSION)d.lib
(3)C:\BuildFolder\include中的tesseract_versionnumbers.vsprops可以用來設置工程的庫引用,原文:Using tesseract_versionnumbers.vsprops (which automatically inherits leptonica_versionnumbers.vsprops) can make it easier to specify the libraries you need to import.在工程中,
Property Sheet-->Configuration Properties -->General -->Inherited Project Property Sheets item to-->添加tesseract_versionnumbers.vsprops的引用地址,
(4)添加=頭文件
#include "allheaders.h"(必須)
#include "baseapi.h"
#include "resultiterator.h"(必須)
#include "strngs.h"
#include "blobs.h"
如果有文件中沒有"strngs.h"中,可以再次下載另外一份文件,來替換C:\BuildFolder\include\tesseract 文件(包含一個測試文件)tesseract-3.01-win32-include-dir-sampleapp-2011-11-08.zip
主要的程序和引用主要在baseapi.h文件中
推薦鏈接:
http://code.google.com/p/tesseract-ocr/
http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/programming.html
https://github.com/tinku99/tesseract-ocr