Python2.7利用Tesseract進行中英文圖像識別
背景環境: win8.1 64位 python2.7.13
本以為會很簡單,結果在配置環境這塊上花了很多時間,踩了幾個坑,最後自己看英文文檔和log才解決問題。
打開網站
https://pypi.python.org/pypi/pytesseract
https://github.com/tesseract-ocr/tesseract/wiki
https://github.com/tesseract-ocr/tesseract/wiki/Downloads
http://www.pythonware.com/products/pil/
找到並下載安裝tesseract-ocr-setup-4.00.00dev.exe文件 下載中文訓練庫chi_sim.traineddata
將安裝文件路徑 添加到環境變量中的PATH 和 Path中去 ,在系統變量中添加一個TESSDATA_PREFIX,變量值還是文件路徑
我的是D:\programfiles\tesseract\Tesseract-OCR
打開cmd安裝 pip install pytesseract
去C:\Python27\Lib\site-packages 下找到PIL卸載 然後 去下載 PIL-1.1.7.win32-py2.7.exe 並安裝
# -*- coding: utf-8 -*- try: import Image except ImportError: from PIL import Image import pytesseract img = Image.open(‘test2.png‘) img.load() text = pytesseract.image_to_string(img, lang=‘chi_sim‘) print(text)
最後找張png的圖放在和這個文件同目錄下 完事了
幾個坑:
:from . import VERSION, PILLOW_VERSION, _plugins ueError: Attempted relative
不知道什麽鬼,後來直接把C:\Python27\Lib\site-packages\PIL 給刪了 重新安裝這個庫
pytesseract.pytesseract.TesseractError: (1, u‘Error opening data file D:\\programfiles\\tesseract\\Tesseract-OCR/chi_sim.traineddata‘)
這裏需要將chi_sim.traineddata放在指定目錄下,而不是 $path\tessdata\tessconfigs下
總結: 看官方教程 百度上搜索的太舊了 stackoverflow 也是
Python2.7利用Tesseract進行中英文圖像識別