使用 OpenCV 及 Tesseract 進行 OCR 辨識(3)-使用 Tesseract 進行 OCR 辨識

NTT DATA IDI+ Platform
NTT DATA IDI+ Platform
8 min readMay 6, 2021

本文為「使用 OpenCV 及 Tesseract 進行 OCR 辨識」系列文章的第三篇「使用 Tesseract 進行 OCR 辨識」,第一篇為「OCR 介紹」,第二篇則為「使用 OpenCV 進行影像前處理」。

使用 Tesseract 進行 OCR 辨識

經過前面兩篇文章「OCR 介紹」、「使用 OpenCV 進行影像前處理」後,我們終於要進到這系列文章的重點,也就是「OCR 辨識」的部分。本文將會使用由 OpenCV 進行影像前處理後的影像,透過 Tesseract 這套開源的 OCR 辨識引擎,來進行 OCR 光學字元辨識。

什麼是 Tesseract

在「使用 OpenCV 及 Tesseract 進行 OCR 辨識(1)-OCR 介紹」文章中曾介紹過 Tesseract 這套開源的 OCR 辨識軟體:

Tesseract 是惠普實驗室(HP Labs)在 1985 年開始研發的一套專利軟體,並於 2005 年開始由 Google 接手進行改造、debug、優化並將其開源,目前 Tesseract 的程式碼也在 github 上託管。

Tesseract + LSTM

由於其開源、免費、支持多語言、支持多平台的特性,Tesseract 是開發者進行 OCR 辨識服務開發的首選套件。而在 Tesseract 4.0 版本之後,新的 OCR 引擎加入了 LSTM(Long Short-Term Memory, 長短期記憶)神經網路,可以使用 LSTM 來訓練辨識引擎,進而提升辨識的準確率。

安裝 Tesseract

首先我們要先安裝 Tesseract,本文將使用最新版本的 Tesseract 5.0 Alpha 來做示範,筆者的電腦是 macOS,因此將示範如何在 macOS 下載安裝 Tesseract,使用其他作業系統的讀者可以參考官方文件的說明進行下載與安裝,網路上也有很多文章可以參考。

在 macOS 的終端機進行以下步驟來下載及安裝 Tesseract:

1. 安裝相依套件

# Packages which are always needed. 
brew install automake autoconf libtool
brew install pkgconfig
brew install icu4c
brew install leptonica
# Packages required for training tools.
brew install pango
# Optional packages for extra features.
brew install libarchive
# Optional package for builds using g++.
brew install gcc

2. 進行編譯

git clone https://github.com/tesseract-ocr/tesseract/ 
cd tesseract
./autogen.sh
mkdir build
cd build
# Optionally add CXX=g++-8 to the configure command if you really want to use a different compiler.
../configure PKG_CONFIG_PATH=/usr/local/opt/icu4c/lib/pkgconfig:/usr/local/opt/libarchive/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig
make -j
# Optionally install Tesseract.
sudo make install
# Optionally build and install training tools.
make training
sudo make training-install

本文我們將使用 pytesseract 這個 Google Tesseract-OCR 引擎的 Python Wrapper 來進行示範,因此也要先下載 pytesseract:

pip install pytesseract

使用 Tesseract 進行 OCR 辨識

本文使用 jupyter notebook 來做示範,主要分成三個步驟:

1. 初始設定
2. 使用 OpenCV 對影像進行前處理
3. 使用 pytesseract 對前處理後的影像進行 OCR 辨識

1. 初始設定

  • 導入相關套件
  • 設定 pytesseract 去讀取下載的 Tesseract CMD
Import packages & set up pytesseract

2. 使用 OpenCV 對影像進行前處理

使用 OpenCV 的前處理方法:

  1. 縮放成 300 x 300
  2. 灰階
  3. 二值化反轉
  4. 模糊化
  5. 開運算
OpenCV 影像前處理

影像經過上述前處理方法後的差異如下圖:

影像前處理前後的差異

3. 使用 pytesseract 對前處理後的影像進行 OCR 辨識

使用 pytesseract 進行 OCR 辨識

在 pytesseract.image_to_string() 中使用的參數說明如下:

  • lang:使用的語言包,預載的語言包會連同 Tesseract 一起下載,如需預測其他語言,可至此下載其他語言包,再放到相對應路徑
  • oem:OCR 引擎模式
- 0:Legacy engine only.
- 1:Neural nets LSTM engine only.
- 2:Legacy + LSTM engines.
- 3:Default, based on what is available.
  • psm:頁面分割模式
- 0:Orientation and script detection (OSD) only.
- 1:Automatic page segmentation with OSD.
- 2:Automatic page segmentation, but no OSD, or OCR.
- 3:Fully automatic page segmentation, but no OSD. (Default)
- 4:Assume a single column of text of variable sizes.
- 5:Assume a single uniform block of vertically aligned text.
- 6:Assume a single uniform block of text.
- 7:Treat the image as a single text line.
- 8:Treat the image as a single word.
- 9:Treat the image as a single word in a circle.
- 10:Treat the image as a single character.

因此我們剛使用了「eng 語言包」、「預設的 OCR 引擎模式」、「假設單一個一致的文字區塊來對頁面做分割」來進行 pytesseract 辨識。

訓練 Tesseract

由上一張圖可以看到我們使用 Tesseract 的辨識結果完全正確,那是因為原始圖片的文字清晰、背景單純、字體端正等因素。但實際應用場景中,所需要辨識的圖像可能不會如此單純,例如停車場的車牌辨識影像,就會有字體歪斜、光線不均等因素,進而導致辨識的結果不佳。

如果我們使用預載的語言包進行辨識,但辨識結果不如預期,在 Tesseract 4.0 之後的版本,因為加入了 LSTM 神經網路,我們可以透過深度學習的方式來訓練 Tesseract 辨識引擎。本文將不會介紹訓練 Tesseract 的部分,有興趣的讀者可以參考以下文章的介紹:

以上是「使用 OpenCV 及 Tesseract 進行 OCR 辨識」系列文章的最後一篇「使用 Tesseract 進行 OCR 辨識」,藉由這三篇文章的介紹,希望能讓讀者了解 OCR 是什麼、OCR 的流程以及使用 Python 來實作。若有其他主題與技術想了解,也歡迎留言告訴我們。

參考資料:

--

--