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檔案 下載中文訓練庫
將安裝檔案路徑 新增到環境變數中的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 也是
I'm fish, I'm on.