Python 將圖片的內容轉換為文字

安裝 python 套件

本次會使用到兩個套件,請先使用下列指令安裝套件

PIL(Pillow) 影像處理套件
    
pip install pillow
    

Tesseract: 光學字元辨識(OCR)
    

pip install pytesseract
    

安裝 OCR 引擎

如果執行時出現下列錯誤就表示 OCR 引擎沒有安裝成功,或是路徑設定錯誤
    
Traceback (most recent call last):
  File "C:\Users\ruyut\PycharmProjects\side\PyAutoGuiTest\main.py", line 22, in <module>
    text = pytesseract.image_to_string(img, lang='eng')
  File "C:\Users\ruyut\PycharmProjects\side\PyAutoGuiTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 416, in image_to_string
    return {
  File "C:\Users\ruyut\PycharmProjects\side\PyAutoGuiTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 419, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\ruyut\PycharmProjects\side\PyAutoGuiTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 286, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\ruyut\PycharmProjects\side\PyAutoGuiTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 258, in run_tesseract
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.


先到這個 Github 頁面,下載 tesseract-ocr exe 檔案(檔案大小大約 53 MB) 安裝時請把安裝路徑記錄下來

筆者的安裝路徑如下:
    
C:\Program Files\Tesseract-OCR


裡面應該會有一個 tesseract.exe 檔案,等等會使用到這個檔案的路徑

將圖片轉換為文字

筆者將 pytesseract github 的 README 截圖一段下來,測試看看能不能正常辨識出文字

學習都是從模仿開始的,所以我們直接看程式碼。

程式碼:
    

from PIL import Image
import pytesseract

# 設定 OCR 引擎的路徑,如果有設定環境變數的話就不用這一行
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' 

img = Image.open(screenshot.png')
text = pytesseract.image_to_string(img, lang='eng') # lang 參數為目標語言, eng 為英文
print(f'OCR結果: {text}')

註: 記得替換 OCR 引擎的路徑

輸出:
    

python.exe main.py 
OCR結果: [= README.rst

Python Tesseract

@ pre-commit.ci ‘passed | Oca 'Passing)
Python-tesseract is an optical character recognition (OCR) tool for python. That is,
it will recognize and “read” the text embedded in images.

Python-tesseract is a wrapper for Google's Tesseract-OCR Engine. It is also useful
as a stand-alone invocation script to tesseract, as it can read all image types
supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif,
bmp, tiff, and others. Additionally, if used as a script, Python-tesseract will print the
recognized text instead of writing it to a file.


除了一開始標籤的部分沒有辨識正確,其他文字的部分除了引號以外其他全部一模一樣!(左側為輸出結果,右側為原始文字)

辨識中文內容

我們可以使用下面這行查看所有可以使用的語言
  
print(f'所有支援的語言: {pytesseract.get_languages()}')
所有支援的語言: ['eng', 'osd']


eng 代表英文,繁體中文為 chi_tra。

啊!沒有中文!

沒關係我們可以到 Github tesseract-ocr/tessdata 專案的頁面下載繁體中文的模型資料

將檔案放到剛剛安裝 OCR 引擎的 tessdata 資料夾中,筆者的路徑為: C:\Program Files\Tesseract-OCR\tessdata

詳細語言可以參考語言對照表

這時候再次查看語言列表就會發現已經支援繁體中文了(chi_tra)!
  
print(f'所有支援的語言: {pytesseract.get_languages()}')
所有支援的語言: ['chi_tra', 'eng', 'osd']


使用維基百科頁面的截圖做測試:

程式碼:
    

from PIL import Image
import pytesseract

# 設定 OCR 引擎的路徑,如果有設定環境變數的話就不用這一行
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' 

img = Image.open(screenshot.png')
text = pytesseract.image_to_string(img, lang='chi_tra') # lang 參數為目標語言, chi_tra 為繁體中文
print(f'OCR結果: {text}')

註: 記得替換 OCR 引擎的路徑

輸出: (左側為輸出結果,右側為原始文字)

嗯...就算先不看英文和標點符號的部份也真是慘不忍睹...不過畢竟是別人訓練的資料,改天我們來自己訓練繁體中文的文字辨識模型好了

參考資料:
StackOverflow: TesseractNotFound Error

留言