python 識別圖片上的數字
python 識別圖片上的數字,使用pytesseract庫從圖像中提取文本,而識別引擎采用 tesseract-ocr。
Tesseract是一款由Google贊助的開源OCR。OCR,即Optical Character Recognition,光學字符識別,是指通過掃描字符,然后通過其形狀將其翻譯成電子文本的過程。
pytesseract是python包裝器,它為可執行文件提供了pythonic API。
1、安裝必要的包:
pip install pillow
pip install pytesseract
2、安裝tesseract-ocr的識別引擎
* 下載地址:https://github.com/UB-Mannheim/tesseract/wiki
或者更多版本的tesseract下載地址:https://digi.bib.uni-mannheim.de/tesseract/
* 裝完成后配置環境變量: 我的電腦 ->屬性 -> 高級系統設置 ->環境變量 ->系統變量 ,在 path 中添加 安裝路徑。
在命令行 WIN+R 輸入cmd :輸入 tesseract -v ,出現版本信息,則配置成功。
3、解決pytesseract 找不到路徑的問題。
在自己安裝的pytesseract包中,找到pytesseract.py文件
打開pytesseract.py文件,修改 tesseract_cmd 的值:tesseract.exe 的安裝路徑 。為了避免其他的錯誤,使用雙反斜杠,或者斜杠
4、簡單使用
import pytesseract from PIL import Image if __name__ == '__main__': text = pytesseract.image_to_string(Image.open("D:\\test.png"),lang="eng") print(text)
測試圖片:
輸出結果:
轉載:https://www.cnblogs.com/BackingStar/p/11254120.html