安裝所需的庫
sudo apt-get install libpng12-dev sudo apt-get install libjpeg62-dev sudo apt-get install libtiff4-dev sudo apt-get install gcc sudo apt-get install g++ sudo apt-get install automake
pytesser 調用了 tesseract,因此需要安裝 tesseract,安裝 tesseract 需要安裝 leptonica,否則編譯tesseract 的時候出現 "configure: error: leptonica not found"。
以下都是解壓編譯安裝的老步驟:
./configure make -j4 sudo make install
下載安裝leptonica
http://www.leptonica.org/download.html 或者
http://code.google.com/p/leptonica/downloads/list
下載安裝tesseract
http://code.google.com/p/tesseract-ocr/
最新的是 tesseract-ocr-3.02.02.tar.gz
下載安裝 tesseract 的語言數據包
http://code.google.com/p/tesseract-ocr/downloads/list
最新的是 tesseract-ocr-3.01.eng.tar.gz
解壓tessdata目錄下的文件(9個)到 "/usr/local/share/tessdata"目錄下
注意:這個網址下載到的只有一個,不能用,使用中會報錯,http://tesseract-ocr.googlecode.com/files/eng.traineddata.gz
下載安裝 pytesser
http://code.google.com/p/pytesser/
最新的是 pytesser_v0.0.1.zip
測試pytesser
到pytesser的安裝目錄,創建一個test.py,python test.py 查看結果。
from pytesser import * #im = Image.open('fnord.tif') #im = Image.open('phototest.tif') #im = Image.open('eurotext.tif') im = Image.open('fonts_test.png') text = image_to_string(im) print text
tesseract 目錄還有其他tif文件,也可以復制過來測試,上面測試的tif,png文件正確識別出文字。
pytesser的驗證碼識別能力較低,只能對規規矩矩不歪不斜數字和字母驗證碼進行識別。測試了幾個網站的驗證碼,顯示 Empty page,看來用它來識別驗證碼是無望了。
測試發現提高對比度后再識別有助於提高識別准確率。
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(4)
參考: