1. 程式人生 > 其它 >python3使用ocr進行簡單的圖文識別

python3使用ocr進行簡單的圖文識別

1、安裝

pip install pytesseract

pytesseract 的使用是 基於 後端Tesseract的,故需要安裝 Tesseract 

2、安裝

Tesseract 

官方網站:https://github.com/tesseract-ocr/tesseract
官方文件:https://github.com/tesseract-ocr/tessdoc
語言包地址:https://github.com/tesseract-ocr/tessdata
下載地址:https://digi.bib.uni-mannheim.de/tesseract/

注:
1、並配置環境變數
2、安裝過程中,可以直接安裝簡體中文包,此步親測,沒有 那麼慢。 (安裝後 即可使用
lang='chi_sim')

參考 https://www.jianshu.com/p/f7cb0b3f337a

3、使用

# 圖文識字
import pytesseract
from PIL import Image


def imageToStr(image_url, lang):
im = Image.open(image_url)
im = im.convert('L')
im_str = pytesseract.image_to_string(im, lang=lang)
return im_str


img_url = r'C:\Users\peng\Desktop\50.png'

# img_str = imageToStr(img_url, 'eng')
# print('識別到的英文', img_str)

# print('識別到的中文')
cn_img_str = imageToStr(img_url, 'chi_sim')
print(cn_img_str)

 

4、結果

靜夜思
作者: 李

床前明月光,疑是地上入
舉頭望明月,低頭思故鄉

 

5、原圖

 

 

6、問題:

很顯然,
1、李白 的白沒打出來
2、霜 ,打成 入

機器學習不深入,可能是因為圖片解析度比較差吧

。。。