轉(zhuǎn)載請注明出處:http://www.reibang.com/u/5e6f798c903a
環(huán)境:
- Win10_64
- Python 3.6.6,安裝路徑
C:\Python36
(后面會用到該路徑)
1. 安裝 Tesserocr
tesserocr 是 Python 下的一個 OCR 識別庫渤闷,該庫本質(zhì)上是對 tesseract 做了一層 Python API 封裝。通過 tesserocr 的 PyPI頁面撮弧,可以找到該項目的 GitHub 倉庫 潘懊。倉庫的 README.rst
中介紹了 Windows 平臺的安裝方式,原文如下:
The proposed downloads consist of stand-alone packages containing all the Windows libraries needed for execution. This means that no additional installation of tesseract is required on your system.
--snip: 這里跳過了使用 Conda 安裝的方式贿衍,需要的話可以查看原文檔--
pip:Download the wheel file corresponding to your Windows platform and Python installation from simonflueckiger/tesserocr-windows_build/releases and install them via:
> pip install <package_name>.whl
按照文檔的意思授舟,由于 stand-alone packages 中包含了 Windows 下所需的所有庫。如果使用 stand-alone packages 安裝 tesserocr 舌厨,便無需再額外安裝 tesseract 岂却。
這里一定要通過獨立的 .whl
文件安裝,不要通過 pip3 install tesserocr
直接安裝裙椭,因為在 Windows 上會失敗——據(jù)說這種方式只能用于 Linux躏哩,但我沒有仔細研究過。
選擇安裝包時揉燃,tesserocr 和 tesseract 的版本要匹配扫尺,如 "tesserocr v2.2.2 (tesseract 4.0.0 master)" 釋放,表明 "tesserocr-2.2.2" 要配合 "tesseract 4.0.0 master" 使用炊汤。如果 tesserocr 與 tesseract 間版本不匹配正驻,識別結(jié)果中會出現(xiàn)非預(yù)期字符。例如抢腐,若是將 "tesserocr-2.2.2" 與 "tesseract 3.5.2" 搭配使用姑曙,結(jié)果中便會出現(xiàn)非預(yù)期字符。
由于迈倍,目前 Windows 下 tesseract 的最新穩(wěn)定版是 3.5.2伤靠,于是我試圖安裝 "tesserocr v2.2.2 (tesseract 3.5.1)" 版本的 .whl
文件,卻提示該 .whl
文件不支持當前平臺啼染,無法安裝宴合。所以,只能安裝 "tesserocr v2.2.2 (tesseract 4.0.0 master)" 版本迹鹅,在后文中需要配合 "tesseract 4.0.0 master" 使用卦洽。
1.1. 坑-1
如果依照官方文檔,只安裝了 tesserocr 的 .whl
文件斜棚,并嘗試運行如下測試代碼:
import tesserocr
from PIL import Image
image = Image.open('image.jpg') # 可在文末找到image.jpg
print(tesserocr.image_to_text(image))
便會得到如下錯誤提示:
Traceback (most recent call last):
File "c:/Users/iwhal/Documents/GitHub/python_notes/notes_of_crawler/code_of_learn_is_ignored/test_of_tesserocr .py", line 4, in <module>
print(tesserocr.image_to_text(image))
File "tesserocr.pyx", line 2401, in tesserocr._tesserocr.image_to_text
RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\\
Traceback 告訴我們:tessdata 路徑無效阀蒂,無法初始化 API。
錯誤的原因是:stand-alone packages 雖然包含了 Windows 下所需的所有庫弟蚀,但并是不包含語言數(shù)據(jù)文件(language data files)脂新。并且數(shù)據(jù)文件需要被統(tǒng)一放置在 tessdata\
文件夾中,并置于 C:\Python36
內(nèi)粗梭。
獲得數(shù)據(jù)文件有如下兩種方式:
- 方法一:按照下一節(jié)的方法安裝 "tesseract-ocr-w64-setup-v4.0.0-beta.1.20180608.exe"(因為要與 tesserocr-2.2.2 匹配)。然后级零,將
C:\Program Files (x86)\Tesseract-OCR\
下的tessdata\
文件夾復(fù)制到C:\Python36\
下即可 断医。 - 方法二:無需安裝 tesseract 滞乙,只需克隆 tesseract 倉庫的主分支,然后將其中的
tessdata\
文件夾復(fù)制到C:\Python36\
中鉴嗤。接下來斩启,通過 tessdata_fast 倉庫下載eng.traineddata
語言文件,并放置于C:\Python36\tessdata\
內(nèi)即可醉锅。
可見兔簇,解決此問題的關(guān)鍵在于獲得 tesseract 的 tessdata\
文件夾,并不一定要安裝 tesseract 硬耍,但 tesseract 的版本一定要正確垄琐。
接下來嘗試運行之前的代碼:
import tesserocr
from PIL import Image
image = Image.open('image.jpg') # 可在文末找到image.jpg
print(tesserocr.image_to_text(image))
便會輸出:
4VC7
1.2. 坑-2
為什么要使用 tessdata_fast 倉庫 中的語言數(shù)據(jù)文件,而不使用 tessdata_best 倉庫 或 tessdata 倉庫 中的文件喃经柴?
因為狸窘,我使用了三個倉庫各自的 eng.traineddata
文件,來識別了文末的驗證碼坯认,發(fā)現(xiàn)只有 tessdata_fast 倉庫 的識別結(jié)果與預(yù)期相同翻擒,另外兩個都沒有輸出。 但對于更加簡單的內(nèi)容牛哺,tessdata 倉庫 和 tessdata_best 倉庫 都有輸出陋气,但前者表現(xiàn)更好。 另外引润,"tesseract-ocr-w64-setup-v4.0.0-beta.1.20180608.exe" 安裝包中同樣使用的是 tessdata_fast 倉庫 中的 eng.traineddata
文件巩趁。
2. 安裝 tesseract
通過查看 tesseract 的 GitHub 倉庫 的 Wiki 主頁,可得知 Windows 下的安裝方法 椰拒,原文如下:
Installer for Windows for Tesseract 3.05-02 and Tesseract 4.00-beta are available from Tesseract at UB Mannheim. These include the training tools. Both 32-bit and 64-bit installers are available.
An installer for the OLD version 3.02 is available for Windows from our download page. This includes the English training data. If you want to use another language, download the appropriate training data, unpack it using 7-zip, and copy the .traineddata file into the 'tessdata' directory, probably
C:\Program Files\Tesseract-OCR\tessdata
.To access tesseract-OCR from any location you may have to add the directory where the tesseract-OCR binaries are located to the Path variables, probably
C:\Program Files\Tesseract-OCR
.
大意是在 Tesseract at UB Mannheim晶渠,可獲得 Windows 安裝包。 轉(zhuǎn)到 UB-Mannheim/tesseract/wiki 后可見到下載鏈接燃观,如下:
The latest installers can be downloaded here: tesseract-ocr-setup-3.05.02-20180621.exe, tesseract-ocr-w32-setup-v4.0.0-beta.1.20180608.exe and tesseract-ocr-w64-setup-v4.0.0-beta.1.20180608.exe (new, 64 bit, experimental). There are also older versions available.
如果需要 older versions 可以去到 https://digi.bib.uni-mannheim.de/tesseract/ 下載褒脯。 這里需要安裝 "tesseract-ocr-w64-setup-v4.0.0-beta.1.20180608.exe",因為要與 tesserocr-2.2.2 匹配缆毁。
另外番川,tesseract 的文檔位于 https://github.com/tesseract-ocr/tesseract/wiki/Documentation
2.1 語言包
通過 wiki 的 Data Files 部分,我們可以下載經(jīng)過訓(xùn)練的語言包脊框。將下載后的語言包颁督,直接放到C:\Program Files (x86)\Tesseract-OCR\tessdata
即可使用。
注意語言包有三個分支:
- https://github.com/tesseract-ocr/tessdata_best
- https://github.com/tesseract-ocr/tessdata_fast
- https://github.com/tesseract-ocr/tessdata
在使用語言數(shù)據(jù)時要注意區(qū)分 Tesseract 的版本浇雹,3.04 或 3.05 的語言數(shù)據(jù)需要從 3.04 tree 獲取沉御。在 Data Files 中可以了解到更多語言包的分支,及其區(qū)別昭灵。