Linux

Tesseract 文字辨識 OCR 引擎使用教學與範例

介紹如何在 Linux 中安裝與使用 Tesseract 文字辨識 OCR 引擎,自動辨識圖片中的文字。

Tesseract OCR 可以說是目前最普遍被使用的光學字元辨識(Optical Character Recognition,OCR)引擎,他可以自動辨識出圖片中的各種文字,支援 UTF8 編碼,可以辨識超過 100 種語言,辨識的結果可以輸出成各種檔案格式,包含文字檔、hOCR、PDF、TSV 等。

以下我們示範如何在 Ubuntu Linux 中安裝與使用 Tesseract OCR 指令工具,辨識含有英文與中文的圖片內容。

安裝 Tesseract OCR

若在 Ubuntu Linux 中,可以使用 apt 安裝 Tesseract OCR 工具:

# 安裝 tesseract-ocr 主要套件
sudo apt install tesseract-ocr

Tesseract OCR 在使用時要配合對應的語言(language)或手稿(script)訓練資料,這裡我們安裝英文與中文的部分:

# 安裝 tesseract-ocr 語言訓練資料套件
sudo apt install tesseract-ocr-eng tesseract-ocr-chi-tra tesseract-ocr-chi-tra-vert

# 安裝 tesseract-ocr 手稿訓練資料套件
sudo apt install tesseract-ocr-script-armn tesseract-ocr-script-hant tesseract-ocr-script-hant-vert

若需要其他國家的語言或手稿,可以使用 apt search 搜尋 tesseract-ocr-* 相關的套件。

通常在進行影像的文字辨識之前,都會需要一些前處理來增加辨識的成功率,ImageMagick 是最常用的工具之一:

# 安裝 ImageMagick
sudo apt install imagemagick

基本英文文字辨識

這是從早期的論文掃瞄檔中擷取出來的摘要,文字基本上都很清晰。

英文文字

若要使用 Tesseract OCR 進行純英文字的文字辨識,只要執行 tesseract 指令,並指定輸入的圖片,以及輸出的檔案名稱即可:

# 對 input.jpg 進行文字辨識,輸出至 output.txt
tesseract input.jpg output

這樣 tesseract 就會讀取 input.jpg 圖檔進行英文的文字辨識,並將結果輸出至 output.txt 這個文字檔案中。如果將輸出的檔案名稱指定為減號(-),就會直接把結果輸出至標準輸出(standard output)。

產出的 output.txt 內容如下:

ABSTRACT

Users of process control applications need two
kinds of information: data and events. Data is
handled by the process control data base management
system (PCDB), while events, i.e., software
triggering, by the event handling means (EHM). Data
and events are gained by the data acquisition and
control package (DCP), and can be represented by the
display communication system (DICOM).

In a distributed real-time system a local area
network interconnects the different control
computers, so the system software tools are
implemented in a distributed way too.

The software architecture of a large
distributed process control systems is presented in
the paper.

Keywords. Distributed process control; data base
management system; event handling; data acquisition;
digital control; man-machine communication; local
area network.

對於這種白底黑字的文字圖片來說,直接交由 Tesseract OCR 就可以有相當高的文字辨識率。

中文文字辨識

以下這張圖片是一段報紙上的文字,以下示範如何使用 Tesseract OCR 來自動辨識其中的中文字。

報紙上的文字

Tesseract OCR 在進行辨識文字時,預設使用的語言是英文,如果要進行中文字的文字辨識,就要另外指定使用中文的訓練資料進行辨識。

我們可以使用 tesseract--list-langs 參數來查詢目前可用的語言:

# 查詢可用的語言
tesseract --list-langs
List of available languages (7):
Armenian
HanT
HanT_vert
chi_tra
chi_tra_vert
eng
osd

Tesseract OCR 預設會使用英文(eng)進行辨識,若要辨識中文字可以選用橫書的繁體中文(chi_tra)或是直書的繁體中文(chi_tra_vert)。

文字辨識所使用的語言可以透過 -l 參數來指定:

# 指定使用橫書繁體中文語言進行辨識
tesseract input.jpg output -l chi_tra

辨識完成後,產出的 output.txt 內容為:

香港傳統藝術界儘管傳承深厚且精英雲集,可惜在重商輕藝
社會下,始終未獲充分支持和重視,以至許多年青藝術家都得
身兼數職以維持藝術創作,更店論有充分機會去展出作品了。

幸運地,社會上亦總不乏有心人,正如既是金融業實幹派,
也是本地中國傳統藝術圈活躍分子的屈文汰,便以其過人的創
造力、執行力和親和力,近年來組織了眾多推廣藝術的活動 人
將消極湊合的「斜頂」以藝術化誤圓融萬物的「大極」 ,這個加
浸潤中國傳統文化藝術的商界精英認鳥:「藝術的教化足以消
馬社會上的屄氣和衝突,藝術的美善足以開啟年輕人,尤其是
青少年的心智與情懷,藝術的欣賞、學習及創作,砂只 是有
槓」彼我分明的跨界發展,而           1
更加是有如太極」般陰陽相
濟,你中有我、我中有你、圓
融契合、恒動不息,且充滿生
機的一個良性循環,相信這亦
正是「藝道之行』所謂的
自前襄有有語

撰文:趨柏偉
攝影 :楊光(GOkyeung.cormn

大部分的中文字都有正確被辨識出來,但少數的文字與標點符號的地方就會出現一些小錯誤。

黑底白字

以下這一張是超音波的照片,圖片的背景是黑色,而文字則是白色。

超音波圖片

如果要辨識這種黑底白字的圖片,可以先用 ImageMagick 將圖片轉為白底黑字,再進行文字的辨識。

我們先使用 ImageMagick 的 convert 搭配 -negate 進行顏色的反相轉換,再加上 -colorspace Gray 將圖片轉為灰階:

# 將顏色反轉,並轉為灰階
convert -negate -colorspace Gray input.jpg gray.jpg

轉出來的圖片會像這樣:

將顏色反轉,並轉為灰階

如果圖片的對比度不夠,可能也會影像文字的辨識率,可以考慮以門檻值將圖片轉為黑白兩色:

# 以門檻值將圖片轉為黑白兩色
convert -threshold 50% gray.jpg bw.jpg

轉為黑白的圖片會像這樣:

以門檻值將圖片轉為黑白兩色

最後將圖片交給 Tesseract OCR 進行辨識:

# 對 bw.jpg 進行文字辨識,輸出至 bw.txt
tesseract bw.jpg bw

辨識的結果為:

Voluson 91.08.1984 RAB4-8-D/OB MI 1.1 Dr. Moroder ecofetale.com
@ Gaarawsd 8.3cm/1.4/16Hz Tis 0.1 02.02.2012 12:41:36

Routine
Har-high
97

Gn 10
ces M7
P5/ES

 

™

cRL 6.51cm
GA 12w6d 71.8%

兩種語言混合辨識

如果希望 Tesseract OCR 同時可以辨識中文與英文,可以在 -l 參數中以加號(+)同時指定中文與英文兩種語言:

# 同時使用英文與橫書繁體中文語言進行辨識
tesseract input.jpg output -l eng+chi_tra

使用兩種語言的時候,要注意語言指定順序也會影響辨識結果,也就是說 -l eng+chi_tra-l chi_tra+eng 的結果可能不同。

設定檔

Tesseract OCR 的設定檔可以用來控制許多細部的選項,例如控制辨識結果的輸出格式,以及辨識文字所使用的字元集合或樣板等,在 Tesseract OCR 的 tessdata/configstessdata/tessconfigs 目錄中有許多可用的設定檔,若在 Linux 中則可從 /usr/share/tesseract-ocr/ 尋找這些檔案,或是從 GitHub 網站上查看各種設定檔。

Tesseract OCR 預設會輸出純文字的辨識結果,如果希望輸出其他格式的辨識結果,可以在參數的最後指定對應的輸出格式設定檔,例如若要採用 hOCR 格式的輸出,就可以執行:

# 採用 hOCR 格式輸出結果
tesseract input.jpg output hocr

這樣辨識的結果就會以 hOCR 格式輸出至 output.hocr

我們也可以同時使用多個設定檔,例如同時輸出純文字與 hOCR 的結果:

# 同時輸出純文字與 hOCR 的結果
tesseract input.jpg output txt hocr

這樣就會同時產生 output.txtoutput.hocr 兩個檔案。

若想要了解設定檔中各種選項的意義,可以查閱 Tesseract OCR 的原始碼,或是輸出所有的參數設定值:

# 輸出所有參數設定值
tesseract --print-parameters
Tesseract parameters:
editor_image_xpos	590	Editor image X Pos
editor_image_ypos	10	Editor image Y Pos
editor_image_menuheight	50	Add to image height for menu bar
editor_image_word_bb_color	7	Word bounding box colour
editor_image_blob_bb_color	4	Blob bounding box colour
editor_image_text_color	2	Correct text colour
editor_dbwin_xpos	50	Editor debug window X Pos
editor_dbwin_ypos	500	Editor debug window Y Pos
editor_dbwin_height	24	Editor debug window height
[略]

除了以設定檔來變更設定之外,也可以使用 -c 參數直接更改特定參數的設定值,例如將 tessedit_create_txttessedit_create_hocr 參數都設定為 1,同時輸出純文字與 hOCR 的結果:

# 同時輸出純文字與 hOCR 的結果
tesseract input.jpg output -c "tessedit_create_txt=1" -c "tessedit_create_hocr=1"

提升文字辨識率

在實務上若要提升文字的辨識率,可以參考 Tesseract OCR 官方說明文件所整理的建議事項以及 FAQ:

參考資料

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago