使用 Python 識別並提取圖像中的文字

1. 介紹

介紹使用 python 進行圖像的文字識別,將圖像中的文字提取出來,能夠幫助咱們完成不少有趣的事情。python

2. 必備工具

  1. tesseract-ocr

下載地址: https://github.com/UB-Mannheim/tesseract/wikigit

tesseract-ocr 是一個開源的圖片OCR識別庫, 功能及其強大,支持多國語言。github

更高級的用法,它還支持機器學習算法,經過訓練的方式,使OCR識別更加智能化及準確。算法

  1. python 庫

使用安裝 pytesseract 和 pillow 庫:機器學習

pip install pytesseract
pip install pillow

3. 開發使用

使用 python 配合 tesseract 識別文字中的圖像能夠很是簡單,幾行代碼就能夠搞定。工具

例如,識別下面這張圖片:
學習

首先導入 pytesseract 和 pillow 庫code

import pytesseract
from PIL import Image

而後指定 tesseract 目錄:blog

pytesseract.pytesseract.tesseract_cmd = 'f:/tessert/tesseract.exe'

而後使用 pillow 庫加載圖片:圖片

img = Image.open('test.png')

最後使用 tesseract 識別圖像的文字:

text = pytesseract.image_to_string(img, lang='chi_sim')
print(text)

最後的結果是:

能夠看到,有誤差,可是基本上都識別出來了。

4. 總結

這裏只是入門級的介紹,固然還有問題,好比彩色圖像識別通常效果很差,對比度低的圖像識別也不必定好,這就須要咱們對圖片進行處理後再來識別。好比提取灰度圖片,銳化圖片等操做,具體涉及到的是數字圖像處理的領域了,這個後面有機會再討論。

相關文章
相關標籤/搜索