1. 安裝tesseract
OCR,即Optical Character Recognition,光學字符識別,是指經過掃描字符,而後經過其形狀將其翻譯成電子文本的過程。對於圖形驗證碼來講,它們都是一些不規則的字符,這些字符確實是由字符稍加扭曲變換獲得的內容。html
tesseract下載地址:https://digi.bib.uni-mannheim.de/tesseract/python
進入下載頁面,能夠看到有各類.exe文件的下載列表,這裏能夠選擇下載3.0版本。git
其中文件名中帶有dev的爲開發版本,不帶dev的爲穩定版本,能夠選擇下載不帶dev的版本,例如能夠選擇下載tesseract-ocr-setup-3.05.02.exe。github
下載完成後雙擊,此時會出現以下圖所示的頁面。chrome
此時能夠勾選Additional language data(download)選項來安裝OCR識別支持的語言包,這樣OCR即可以識別多國語言。而後一路點擊Next按鈕便可。windows
接下來,爲了在python代碼中使用tesseract功能,使用pip安裝pytesseract:session
pip install pytesseractide
二、配置環境變量
爲了在全局使用方便,好比安裝路徑爲D:\Program Files (x86)\Tesseract-OCR,將該路徑添加到環境變量的path中測試
配置完成後在命令行輸入tesseract -v,若是出現以下圖所示,說明環境變量配置成功url
三、驗證安裝
接下來,咱們可使用tesseract和pytesseract來分別進行測試。
咱們以以下圖所示的圖片爲樣例進行測試。
該圖片的連接爲https://raw.githubusercontent.com/Python3WebSpider/TestTess/master/image.png,能夠直接保存或下載。
首先用命令行進行測試,將圖片下載到D盤chromeDownload文件夾,保存爲image.png,而後在該文件夾中打開命令行,用tesseract命令測試:
tesseract image.png result
運行結果以下:
D:\chromeDownload>tesseract image.png result
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
這裏咱們調用了tesseract命令,其中第一個參數爲圖片名稱,第二個參數result 爲結果保存的目標文件名稱。
運行結果即是圖片的識別結果:Python3WebSpider。能夠在chromeDownload文件夾中看到result.txt,這時已經成功將圖片文字轉爲電子文本了。
而後還能夠利用Python代碼來測試,這裏就須要藉助於pytesseract庫了,測試代碼以下:
-
from PIL import Image
-
import pytesseract
-
-
text = pytesseract.image_to_string(Image.open( r'D:\chromeDownload\image.png'))
-
print(text)
咱們首先利用Image讀取了圖片文件,而後調用了pytesseract的image_to_string()方法,再將其識別結果輸出。
運行結果以下:
Python3WebSpider
若是成功輸出結果,則證實tesseract和pytesseract都已經安裝成功。
四、使用時遇到的坑
在使用tesseract命令行進行測試時,會議開始報如下的錯誤
Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!
Could not initialize tesseract.
報錯是意思是缺乏環境變量TESSDATA_PREFIX,致使沒法加載任何語言,就不能初始化tesseract。
解決的方法也很簡單,在環境變量中添加TESSDATA_PREFIX,以下圖
注意:變量值中的路徑爲「D:/Program Files (x86)/Tesseract-OCR」,使用正斜槓「/」。windows中複製過來的路徑默認是反斜槓「\」
配置完成後,從新打開命令行,便可正常使用。
第二個坑是使用pytesseract時,出現如下錯誤
Traceback (most recent call last):
File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "D:\Python36\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "D:\Python36\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系統找不到指定的文件。During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/python/20180911.py", line 4, in <module>
text = pytesseract.image_to_string(Image.open(r'D:\chromeDownload\image.png'))
File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
return run_and_get_output(*args)
File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
run_tesseract(**kwargs)
File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 172, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path
這就很坑,添加了全局變量,仍是提示tesseract沒有安裝或者不在PATH中。
百度了一下,解決方案以下。
pytesseract安裝後,在python的Lib目錄下site-packges下會生成一個pytesseract文件夾,文件夾中找到pytesseract.py,路徑爲:D:\Python36\Lib\site-packages\pytesseract,使用notepad之類軟件打開pytesseract.py,找到以下兩行:
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'
將tesseract_cmd = 'tesseract'修改成:tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
表示tesseract_cmd配置的是你安裝tesseract的絕對路徑,這樣就能找到tesseract了。修改後保存,再去運行python代碼,就能夠成功了。
原文地址:https://blog.csdn.net/showgea/article/detail/82656515