tesserocr驗證碼識別

https://digi.bib.uni-mannheim.de/tesseract/

https://blog.csdn.net/SpuerCheng/article/details/79323620

https://github.com/simonflueckiger/tesserocr-windows_build/releases

https://blog.csdn.net/taiyangmiaomiao/article/details/81945158

 

1.初識tesserocr

 tesserocr是Python的一個OCR識別庫,但其實是對tesseract做的一層Python API封裝,所以它的核心是tesseract。因此,在安裝tesserocr之前,我們需要先安裝tesseract。

2. 相關鏈接

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
3. Windows下的安裝

在Windows下,首先需要下載tesseract,它爲tesserocr提供了支持。其中文件名中帶有dev的爲開發版本,不帶dev的爲穩定版本,可以選擇下載不帶dev的版本。

下載完成後雙擊,此時會出現如圖1-25所示的頁面。 

 此時可以勾選Additional language data(download)選項來安裝OCR識別支持的語言包,這樣OCR便可以識別多國語言。

注: 建議只選Additional language data 中自己需要的語言包,大部分是用不到的。個人選的是中文跟英文

4.配置tesseract環境變量

將以下兩個路徑添加到path中,命令行輸入tesseract沒有錯誤即安裝成功

D:\Program Files\Tesseract-OCR
D:\Program Files\Tesseract-OCR\tessdata
5.安裝tesserocr

首先 pip install pillow
pip3 install tesserocr 會出現以下錯誤:
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

解決方法: 下載tesserocr-2.2.2-cp36-cp36m-win_amd64.whl
pip3 install tesserocr-2.2.2-cp36-cp36m-win_amd64.whl
下載地址 https://pan.baidu.com/s/1_tgn_FP7IcP6ddi3UVK0Hw 密碼:rpbr
6.tesserocr簡單使用

import tesserocr
from PIL import Image
 
image = Image.open('code.png')
result = tesserocr.image_to_text(image)
print(result)
注: 如果出現以下錯誤 ,添加以下環境變量

變量名:TESSDATA_PREFIX,變量值: D:\Program Files\Tesseract-OCR\tessdata

RuntimeError:Failed to init API, possibly an invalid tessdata path: D:\Users\Administrator\AppData\Local\Programs\Python\

不出意外,首次運行總是不順利,相信我遇到的坑大多數人都會遇到,大抵錯誤類似:

 

Traceback (most recent call last):
  File "c:\Users\NewJune\test.py", line 4, in <module>
    print(tesserocr.image_to_text(image))
  File "tesserocr.pyx", line 2400, in tesserocr._tesserocr.image_to_text
RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Python36\

 

不難看出 tesserocr.py文件沒有指定正確的tessdata 路徑,本人的python也並非安裝在C:\Python36\,網上看到的方法都是添加D:\Program Files\Tesseract-OCR這個到系統環境變量,依然不成功,目前本人沒找到如何修改tesserocr.py關聯的tessdata path有效方式,但是比較簡單粗暴的方法是,可以根據提示,直接手工新建C:\Python36\,並將D:\Program Files\Tesseract-OCR對應的tessdata文件夾整個拷貝到C:\Python36\即可。親測有效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章