OCR
OCR,即Optical Character Recognition,光學字符識別,是指通過掃描字符,然后通過其形狀將其翻譯成電子文本的過程。對於圖形驗證碼來說,它們都是一些不規則的字符,這些字符確實是由字符稍加扭曲變換得到的內容。
例如,對於驗證碼,我們可以使用OCR技術來將其轉化為電子文本,然后爬蟲將識別結果提交給服務器,便可以達到自動識別驗證碼的過程。
安裝tessoract
tesserocr需要安裝tessoract依賴庫,所以安裝tesserocr前需要安裝tessoract。
相關文件下載
- tesserocr GitHub:https://github.com/sirfz/tesserocr
- tesserocr PyPI:https://pypi.python.org/pypi/tesserocr
- tesseract下載地址:http://digi.bib.uni-mannheim.de/tesseract
- tesseract GitHub:https://github.com/tesseract-ocr/tesseract
- tesseract語言包:https://github.com/tesseract-ocr/tessdata
- tesseract文檔:https://github.com/tesseract-ocr/tesseract/wiki/Documentation
在Windows下,首先需要下載tesseract,它為tesserocr提供了支持。http://digi.bib.uni-mannheim.de/tesseract
進入下載頁面,可以看到有各種.exe文件的下載列表,這里可以選擇下載3.0版本。圖所示為3.05版本。
其中文件名中帶有dev的為開發版本,不帶dev的為穩定版本,可以選擇下載不帶dev的版本,例如可以選擇下載tesseract-ocr-setup-3.05.01.exe。
下載完成后雙擊,此時會出現如圖所示的頁面。全部勾選
安裝tesserocr
windows不能用pip install tesserocr所以我這里是安裝.whl文件,需要的道友請下載:
鏈接:https://pan.baidu.com/s/1i72kK1Wcc77B1BOvOC2pmg 提取碼:pvuq
然后通過一下命令安裝即可完成
pip install XXXX.whl(這是文件路徑)
測試一下
代碼:
import tesserocr from PIL import Image img = Image.open('1.png') result = tesserocr.image_to_text(img) print(result)
驗證碼圖片來源:http://my.cnki.net/elibregister/CheckCode.aspx
結束語:Windows不能通過pip install tesserocr 來安裝tesserocr庫,所以采用了.whl安裝