如何在 C# 中為 Tesseract 5 進行自訂字體訓練

C# Custom Font Training for Tesseract 5 (for Windows Users)

This article was translated from English: Does it need improvement?
Translated
View the article in English

利用 Tesseract 5 的自訂字體訓練來提高 OCR 引擎在處理可能預設不太支援的特定字體或字體風格時的準確性及識別能力。

此過程涉及提供 Tesseract 訓練數據,如字體樣本和對應文本,以便它學習自訂字體的特定特徵和模式。

as-heading:2(快速入門:在 C# 中使用您的 .traineddata 字體文件)

以下是如何在 IronOCR 中使用您自訂訓練的 Tesseract 字體文件的簡單步驟。 適合快速獲得特殊或裝飾性字體的準確 OCR。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    var ocr = new IronOcr.IronTesseract();
    ocr.UseCustomTesseractLanguageFile("path/to/YourCustomFont.traineddata");
    string text = ocr.Read(new IronOcr.OcrInput("image-with-special-font.png")).Text;
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

精簡工作流程(5個步驟)

  1. 下載用於 Tesseract 自訂字體訓練的 C# 庫
  2. 準備用於訓練的目標字體文件
  3. 按照文章中指定的步驟進行
  4. 包含常見錯誤的解決方案
  5. 導出訓練數據文件以供進一步使用

步驟 1:下載最新版本的 IronOCR

透過 DLL 安裝

直接將 IronOcr DLL 下載到您的機器。

透過 NuGet 安裝

或者,您可以通過以下命令通過 NuGet 安裝:

Install-Package IronOcr

步驟 2:安裝並設置 WSL2 和 Ubuntu

參考教程 設置 WSL2 和 Ubuntu

請注意自訂字體訓練目前只能在 Linux 上完成。

步驟 3:在 Ubuntu 上安裝 Tesseract 5

使用以下命令安裝 Tesseract 5:

sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
SHELL

步驟 4:下載您要訓練的字體

本教程中我們使用 AMGDT 字體。 字體文件可以是 .ttf 或 .otf。 下載的字體文件示例

步驟 5:掛載您的工作空間磁碟驅動器以進行自訂字體訓練

使用下列命令將驅動器 D: 掛載為您的工作空間。

cd /
cd /mnt/d
cd /
cd /mnt/d
SHELL

步驟 6:將字體文件複製到 Ubuntu 字體資料夾

將字體文件複製到 Ubuntu 的以下目錄:/usr/share/fonts/usr/local/share/fonts

在文件管理器的地址欄中輸入 \wsl$ 來訪問 Ubuntu 的文件。

Ubuntu 資料夾目錄

故障排除:目標資料夾拒絕訪問

如果遇到拒絕訪問錯誤,使用命令行來解決問題以複製文件。

cd /
su root
cd /c/Users/Admin/Downloads/'AMGDT Regular'
cp 'AMGDT Regular.ttf' /usr/share/fonts
cp 'AMGDT Regular.ttf' /usr/local/share/fonts
exit
cd /
su root
cd /c/Users/Admin/Downloads/'AMGDT Regular'
cp 'AMGDT Regular.ttf' /usr/share/fonts
cp 'AMGDT Regular.ttf' /usr/local/share/fonts
exit
SHELL

步驟 7:從 GitHub 克隆 tesseract_tutorial

使用以下命令克隆 tesseract_tutorial 儲存庫:

git clone https://github.com/astutejoe/tesseract_tutorial.git
git clone https://github.com/astutejoe/tesseract_tutorial.git
SHELL

步驟 8:從 GitHub 克隆 tesstraintesseract

導航到 tesseract_tutorial 目錄,然後克隆 tesstraintesseract 儲存庫:

git clone https://github.com/tesseract-ocr/tesstrain
git clone https://github.com/tesseract-ocr/tesseract
git clone https://github.com/tesseract-ocr/tesstrain
git clone https://github.com/tesseract-ocr/tesseract
SHELL
  • tesstrain 包含用於創建 .traineddata 文件的 "Makefile"。
  • tesseract 包含 "tessdata" 資料夾,其中包括自定字體訓練時用作參考的原始 .traindata 文件。

步驟 9:創建一個 "data" 資料夾來儲存輸出

tesseract_tutorial/tesstrain 內創建一個 "data" 資料夾。

步驟 10:運行 split_training_text.py

返回 tesseract_tutorial 資料夾並執行以下命令:

python split_training_text.py
python split_training_text.py
SHELL

在運行 split_training_text.py 後,會在 "data" 資料夾中創建 .box.tif 文件。

故障排除:Fontconfig 警告

Font Config 警告 如果看到警告 Fontconfig warning: "/tmp/fonts.conf, line 4: empty font directory name ignored",則是因為缺少字體資料夾。 透過編輯 tesseract_tutorial/fonts.conf 文件來解決這個問題,並添加:

<dir>/usr/share/fonts</dir>
<dir>/usr/local/share/fonts</dir>
<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
<dir>/usr/share/fonts</dir>
<dir>/usr/local/share/fonts</dir>
<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
XML

將其複製到 /etc/fonts 的方法:

cp fonts.conf /etc/fonts
cp fonts.conf /etc/fonts
SHELL

此外,更新 split_training_text.py

fontconf_dir = '/etc/fonts'
fontconf_dir = '/etc/fonts'
PYTHON

注意:訓練文件(.box 和 .tif)數量

目前訓練文件的數量設置為 100。您可以在 split_training_text.py 中修改這一點。

設置訓練文件數量

步驟 11:下載 eng.traineddata

從此 儲存庫 下載 eng.traineddata 並將其放置在 tesseract_tutorial/tesseract/tessdata

步驟 12:創建您的自訂字體 .traineddata

導航到 tesstrain 資料夾並在 WSL2 中使用以下命令:

TESSDATA_PREFIX=../tesseract/tessdata make training MODEL_NAME=AMGDT START_MODEL=eng TESSDATA=../tesseract/tessdata MAX_ITERATIONS=100
TESSDATA_PREFIX=../tesseract/tessdata make training MODEL_NAME=AMGDT START_MODEL=eng TESSDATA=../tesseract/tessdata MAX_ITERATIONS=100
SHELL
  • MODEL_NAME 是你的自訂字體名稱。
  • START_MODEL 是原始 .traineddata 參考。
  • MAX_ITERATIONS 定義了迭代次數(更多迭代次數可以提高 .traineddata 的準確性)。

故障排除:Makefile 中的 "Failed to Read Data"

為了解決 "Failed to read data" 問題,修改 Makefile:

WORDLIST_FILE := $(OUTPUT_DIR2)/$(MODEL_NAME).lstm-word-dawg
NUMBERS_FILE := $(OUTPUT_DIR2)/$(MODEL_NAME).lstm-number-dawg
PUNC_FILE := $(OUTPUT_DIR2)/$(MODEL_NAME).lstm-punc-dawg

故障排除:"Failed to Load Script Unicharset"

tesstrain/data/langdata 資料夾中插入 Latin.unicharsetLatin.unicharset 可以在 這裡 找到。

步驟 13:驗證所創建的 .traineddata 的準確性

使用 1000 個 .box.tif 文件及 3000 次訓練迭代,輸出 .traineddata (AMGDT.traineddata) 在最小訓練誤差率 (BCER) 上達到約 5.77。

Traineddata 準確性

欲了解更多閱讀和參考,請參見教程:YouTube 視頻

常見問題解答

如何在 C# 中訓練自訂字體以供 Tesseract 使用?

要使用 C# 為 Tesseract 訓練自訂字體,您必須先下載 IronOCR,準備您的字體文件,並透過 Windows 上的 WSL2 和 Ubuntu 設定 Linux 環境,因為 Tesseract 的自訂字體訓練僅在 Linux 上受支援。

如何在 Windows 系統上使用 WSL2 安裝 Tesseract 5?

要使用 WSL2 在 Windows 上安裝 Tesseract 5,您需要設定 Ubuntu,然後使用指令sudo apt install tesseract-ocrsudo apt install libtesseract-dev完成安裝。

複製字型檔案時如果遇到「目標資料夾存取被拒絕」錯誤,我該怎麼辦?

如果遇到「目標資料夾存取被拒絕」錯誤,請使用具有 root 權限的命令列將字體檔案複製到必要的目錄中,以繞過權限問題。

為什麼在 Tesseract 中進行自訂字體訓練需要 Linux 環境?

Tesseract 中的自訂字體訓練需要 Linux 環境,因為訓練工具設計用於在類別 Unix 系統上運行,而 WSL2 可用於在 Windows 上模擬此環境。

如何解決訓練自訂字型時出現的「Fontconfig 警告」錯誤?

要解決「Fontconfig 警告」錯誤,您應該將字體目錄路徑新增至fonts.conf檔案中,並確保將其複製到/etc/fonts目錄。

在自訂字體訓練中,「tesstrain」儲存庫的用途是什麼?

'tesstrain' 儲存庫用於建立 Tesseract 中自訂字體訓練所需的.traineddata文件,並提供該過程所需的腳本和 Makefile。

如何解決「載入腳本單字集失敗」錯誤?

要解決「無法載入腳本 Unicharset」錯誤,您需要將Latin.unicharset插入到tesstrain/data/langdata資料夾中,以確保必要的字元集可用。

如何在 Tesseract 中驗證我自訂訓練資料的準確性?

您可以透過檢查訓練錯誤率(稱為 BCER)來驗證自訂訓練資料的準確性,並確保在足夠的迭代和訓練檔案調整後 BCER 達到最小值。

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。
審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

">

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once