pytesser:圖片驗證碼識別
pytesser是一個用于圖片文本識別的python模塊:http://code.google.com/p/pytesser/,即從文本的截圖中還原出文本信息;
網上在windows上安裝、使用的資料比較多,而沒有linux的資料;
作者雖然沒有說明pytesser在linux環境下測試過,但也表示“The scripts should work in Linux as well.”;
今天在我的ubuntu9.10上編譯、安裝、使用了一把,過程中遇到一些問題并解決,記在這里:
- pytesser依賴于PIL,因此需要先安裝PIL模塊,詳見:http://wenyue.me/blog/278
- pytesser調用了tesseract,因此需要安裝tesseract:
先用包管理器安裝這幾個庫:1234</td>sudoapt-getinstalllibpng12-devsudoapt-getinstalllibjpeg62-devsudoapt-getinstalllibtiff4-devsudoapt-getinstallzlibg-dev</div> </td> </tr> </tbody> </table> </div> </div>下載tesseract的源碼包:http://tesseract-ocr.googlecode.com/files/tesseract-3.00.tar.gz
解壓、cd到解壓后目錄下tesseract-3.00/
運行./configure -prefix=你想要安裝到的路徑,比如:1</td>./configure--prefix=/home/pf-miles/installation/install/tesseract</div> </td> </tr> </tbody> </table> </div> </div>然后make & make install
將tesseract的運行腳本加到環境變量中,比如:1</td>exportPATH=$PATH:/home/pf-miles/installation/install/tesseract/bin</div> </td> </tr> </tbody> </table> </div> </div>, 這個路徑與剛才你configure的時候設置的路徑有關
到http://code.google.com/p/tesseract-ocr/downloads/list頁 面去下載最新的eng.traineddata.gz文件,解壓后的eng.traineddata放到/home/pf-miles /installation/install/tesseract/share/tessdata目錄下,注意,雖然tesseract的svn trunk里也有這個文件,但那個用不得,會報1</td>actual_tessdata_num_entries_ <= TESSDATA_NUM_ENTRIES:Error:Assert failed:infiletessdatamanager.cpp, line 55</div> </td> </tr> </tbody> </table> </div> </div>錯誤,詳見:http://www.uluga.ubuntuforums.org/showthread.php?p=10248384,所以一定要用http://code.google.com/p/tesseract-ocr/downloads/list這里下載的那一份
試一試:12</td>pf-miles@pf-miles-desktop:~/downloads$ tesseractUsage:tesseract imagename outputbase [-l lang] [configfile [[+|-]varfile]...]</div> </td> </tr> </tbody> </table> </div> </div>OK,tesseract安裝完畢
</li>- 下載pytesser包:http://pytesser.googlecode.com/files/pytesser_v0.0.1.zip(目前是0.0.1版本), 解壓…并cd到解壓后的目錄下
- 目錄下有個“phototest.tif”圖片文件作為測試用,直接在目錄下寫一個python腳本進行測試:
test.py:1234</td>frompytesserimport*im=Image.open('phototest.tif')text=image_to_string(im)printtext</div> </td> </tr> </tbody> </table> </div> </div>運行:
1</td>pf-miles@pf-miles-desktop:~/downloads/pytesser$ pythontest.py 2>/dev/null</div> </td> </tr> </tbody> </table> </div> </div>結果:
</li> </ol>
Thls IS a lot of 12 pornt text to test the
ocr code and see lf It works on all types
of frle format
lazy fox The qurck brown dog jumped
over the lazy fox The qulck brown dog
jumped over the lazy fox The QUICK
brown dog jumped over the lazy fox
The quick brown dog jumped over the應該說準確率還令人滿意吧.
</div>pytesser的驗證碼識別能力比較低,只能對規規矩矩不歪不斜數字和字母驗證碼進行識別,這里還是要介紹下它的用法。有關它的安裝和python對應的模塊可以參考http://wenyue.me/blog/tag/pytesser。
pytesser只能對tiff(tif)格式的圖片文件進行識別,大部分網站的驗證碼圖片不是tiff格式的,所以需要進行轉換。
可使用Image模塊轉化圖片格式
#需要保存成tmp.tiff,發現保存成tmp.tif的話pytesser無法識別
Image.open(‘tmp.gif’).convert(‘RGB’).save(‘tmp.tiff’)
獲取驗證碼的時候需要讓對方服務器寫如cookie,所以需要以下這段
jk = cookielib.LWPCookieJar()
cookies = urllib2.HTTPCookieProcessor(jk)
opener = urllib2.build_opener(cookies)
然后再需要拿著這個opener去登錄, 登錄成功后的,再去請求其他需要登錄的頁面的時候也需要使用這個opener去urlopen
來自:http://blogread.cn/it/article/5393本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!相關資訊
sesese色