如何在 C# 中進行 Tesseract 5 的自定字體訓練

C## 適用於 Windows 開發人員的 Tesseract 5 自訂字型訓練。

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

Tesseract 5 中的自訂字型訓練可提高特定字型的 OCR 準確度。 這個過程會建立訓練資料,教導引擎字型特性。 產生的 .traineddata 檔案可與 IronOCR 搭配使用,以準確辨識裝飾性或特殊字型。

快速入門:在 C# 中使用您的 .traineddata 字型檔

在 IronOCR 中使用您自訂訓練的 Tesseract 字型檔案,只需要幾行文字。 非常適合在特殊或裝飾性字型上進行精確的 OCR。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  2. 複製並運行這段程式碼。

    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. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer

如何下載 IronOCR 的最新版本?

我應該使用哪種安裝方法?

直接將IronOcr DLL下載到您的電腦。

為何要改用 NuGet?

或者,使用此指令透過 NuGet 進行安裝:

Install-Package IronOcr

IronOCR 為Tesseract 5 功能自訂語言實作提供全面支持,使其成為滿足特殊 OCR 需求的理想選擇。


如何安裝和設定 WSL2 和 Ubuntu?

請參閱設定 WSL2 和 Ubuntu 的教學。

[{i:(自訂字型訓練需要 Linux。

訓練需要使用 Linux,但所產生的 .traineddata 檔案可在所有平台上無縫運作。 如需詳細的 Linux 設定說明,請參閱我們的 Linux 部署指南

如何在 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

這些套件提供訓練所需的核心 Tesseract OCR 引擎和開發函式庫。 如需進階的 Tesseract 配置選項,請參閱我們的 詳細配置指南

我應該下載哪一種字型來進行訓練?

本教程使用 AMGDT 字體。 字型檔案可以是 .ttf 或 .otf 格式。 !Windows 檔案總管顯示下載的 AMGDT Regular.ttf 字型檔案,以紅色方塊突出顯示以供訓練

為訓練選擇字型時:

  • 選擇與標準 Tesseract 模式有顯著差異的字型
  • 確保字型有適當的授權
  • 考慮使用裝飾性、手寫或專門的行業字型
  • 使用您的應用程式在生產中遇到的字型進行測試

如何安裝自訂字型訓練的磁碟機?

使用這些指令掛載磁碟機 D: 作為您的工作空間:

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

這可讓您直接從 Ubuntu WSL2 環境處理儲存在 Windows 磁碟機上的檔案。

如何將字體檔案複製到 Ubuntu 字體資料夾?

複製字型檔案到這些 Ubuntu 目錄:/usr/share/fonts/usr/local/share/fonts

在 Ubuntu 系統中,可以透過在檔案總管的網址列中輸入\\wsl$來存取檔案。

Windows 檔案總管顯示從 Windows 存取 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

字型安裝對於訓練過程至關重要。 在產生訓練影像時,系統需要存取渲染字型。

我要如何從 GitHub 克隆 tesseract_tutorial?

使用此指令克隆 tesseract_tutorial 套件庫:

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

本資料庫包含訓練過程中不可或缺的 Python 腳本和組態檔案。 這些腳本可將字型訓練中的許多手動步驟自動化。

我要如何從 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 資料夾,其中有原始的 .traineddata 檔案,在自訂字型訓練過程中用作參考。

有關使用多種語言套件和自訂訓練資料的詳細資訊,請參閱我們的 國際語言指南

如何建立"資料"資料夾來儲存輸出資料?

tesseract_tutorial/tesstrain 內建立資料夾

mkdir tesseract_tutorial/tesstrain/data
mkdir tesseract_tutorial/tesstrain/data
SHELL

此資料夾儲存所有已產生的訓練檔案,包括 .box、.tif 及中階訓練工件。

我要如何執行 split_training_text.py?

返回 tesseract_tutorial 資料夾,並執行此指令:

python split_training_text.py
python split_training_text.py
SHELL

執行 split_training_text.py 之後,會在資料夾中建立 .box.tif 檔案。

如何修正 Fontconfig 警告?

終端機顯示 fontconfig 關於 Apex 字型遺失和字型目錄為空的錯誤警告。 如果您看到警告 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>

<dir>~/.fonts</dir>
<dir>/usr/share/fonts</dir>
<dir>/usr/local/share/fonts</dir>
<dir prefix="xdg">fonts</dir>

<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

我應該產生多少個訓練檔案?

目前的配置會產生 100 個訓練檔案。 您可以在 split_training_text.py 中修改。

Python 程式碼設定 count=100 並分割行陣列以限制訓練資料大小

針對生產品質的訓練:

  • 從 100-500 個樣本開始測試
  • 使用 1000-5000 個樣本以提高準確性
  • 包含涵蓋所有必要字元的多樣化文字範例
  • 平衡訓練時間與精確度要求

在哪裡下載 eng.traineddata?

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

基本模型提供語言上下文,可提高辨識準確度。 選擇符合目標語言的基本模式。 有關自訂語言套件問題的疑難排解,請參閱我們的 自訂 OCR 語言套件指南

如何建立我的自訂字型 .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 定義迭代的次數 (迭代次數越多,精確度越高)

What if I Get "Failed to Read Data" in Makefile?

若要解決"讀取資料失敗"的問題,請修改 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

此修改可確保 Makefile 在正確的輸出目錄結構中尋找檔案。

如何修正"載入腳本失敗"的問題?

Latin.unicharset插入tesstrain/data/langdata資料夾。 尋找 Latin.unicharset 這裡

unicharset 檔案定義您的語言或腳本的字元集。 確保符合您字型的字元涵蓋範圍。

我該如何驗證所建立的 .traineddata 的正確性?

使用 1000 個 .box.tif 檔案和 3000 次訓練迭代,輸出 AMGDT.traineddata 達到約 5.77% 的最小訓練錯誤率 (BCER)。

Tesseract 訓練日誌顯示 BCER 在迭代 2194-2298 期間由 6.388% 改善至 5.771%

要用 IronOCR 測試您訓練好的模型:

using IronOcr;

// Initialize IronOCR with custom trained data
var ocr = new IronTesseract();

// Load your custom trained font
ocr.UseCustomTesseractLanguageFile(@"path\to\AMGDT.traineddata");

// Configure for optimal results
ocr.Configuration.BlackListCharacters = "";
ocr.Configuration.WhiteListCharacters = "";
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;

// Process an image with your custom font
using var input = new OcrInput();
input.LoadImage("test-image-with-amgdt-font.png");

// Optional: Apply filters if needed
input.EnhanceResolution(300);
input.DeNoise();

// Perform OCR
var result = ocr.Read(input);
Console.WriteLine($"Recognized Text: {result.Text}");
Console.WriteLine($"Confidence: {result.Confidence}%");
using IronOcr;

// Initialize IronOCR with custom trained data
var ocr = new IronTesseract();

// Load your custom trained font
ocr.UseCustomTesseractLanguageFile(@"path\to\AMGDT.traineddata");

// Configure for optimal results
ocr.Configuration.BlackListCharacters = "";
ocr.Configuration.WhiteListCharacters = "";
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;

// Process an image with your custom font
using var input = new OcrInput();
input.LoadImage("test-image-with-amgdt-font.png");

// Optional: Apply filters if needed
input.EnhanceResolution(300);
input.DeNoise();

// Perform OCR
var result = ocr.Read(input);
Console.WriteLine($"Recognized Text: {result.Text}");
Console.WriteLine($"Confidence: {result.Confidence}%");
Imports IronOcr

' Initialize IronOCR with custom trained data
Dim ocr As New IronTesseract()

' Load your custom trained font
ocr.UseCustomTesseractLanguageFile("path\to\AMGDT.traineddata")

' Configure for optimal results
ocr.Configuration.BlackListCharacters = ""
ocr.Configuration.WhiteListCharacters = ""
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd

' Process an image with your custom font
Using input As New OcrInput()
    input.LoadImage("test-image-with-amgdt-font.png")

    ' Optional: Apply filters if needed
    input.EnhanceResolution(300)
    input.DeNoise()

    ' Perform OCR
    Dim result = ocr.Read(input)
    Console.WriteLine($"Recognized Text: {result.Text}")
    Console.WriteLine($"Confidence: {result.Confidence}%")
End Using
$vbLabelText   $csharpLabel

若要在生產應用程式中實作自訂字型,請參閱我們的 使用自訂語言檔案指南。

如需進一步閱讀和參考,請觀看教學: YouTube 影片

常見問題解答

如何在 C# 中使用自訂訓練的字型檔案?

只需幾行程式碼,您就可以在 IronOCR 中使用自訂訓練的 Tesseract 字型檔案。只需建立一個 IronTesseract 範例,以您的 .traineddata 檔案路徑呼叫 UseCustomTesseractLanguageFile(),然後使用 Read() 方法對包含您特殊字型的影像執行 OCR。

訓練 OCR 自訂字型有哪些要求?

自訂字型訓練需要 Linux 環境 (Windows 使用者建議使用 Ubuntu 的 WSL2)、已安裝開發函式庫的 Tesseract 5,以及您想要訓練的字型檔案 (.ttf或 .otf 格式)。在 Linux 中建立的 .traineddata 檔案可在所有平台上與 IronOCR 無縫搭配使用。

為何要訓練自訂字型而非使用標準 OCR?

訓練自訂字型可提高特定字型的 OCR 準確度,尤其是與標準 Tesseract 模型有顯著差異的裝飾字型或特殊字型。IronOCR 就可以使用這些訓練過的字型檔案,準確辨識包含這些獨特字型的影像中的文字,否則標準 OCR 模型就很難讀取這些文字。

我可以在不同的平台上使用自訂訓練的字型嗎?

是的,雖然訓練過程需要使用 Linux,但所產生的 .traineddata 檔案可透過 IronOCR 在所有平台上無縫運作。這表示您可以在 Linux 上訓練一次,然後在 Windows、macOS 或 Linux 部署上使用訓練好的資料檔案。

建議使用何種安裝方式開始使用?

為了快速安裝,您可以直接下載 IronOCR DLL 或透過 NuGet Package Manager 安裝。建議使用 NuGet,因為它會自動處理相依性,讓更新更容易。IronOCR 提供對 Tesseract 5 功能和自訂語言實作的全面支援。

Kannaopat Udonpant
軟體工程師
在成為軟體工程師之前,Kannapat 完成了日本北海道大學的環境資源博士學位。在攻讀學位期間,Kannapat 也成為生物製造工程系車輛機器人實驗室的成員。2022 年,他利用自己的 C# 技能加入 Iron Software 的工程團隊,主要負責 IronPDF 的開發。Kannapat 非常重視他的工作,因為他可以直接向撰寫 IronPDF 使用的大部分程式碼的開發者學習。除了同儕學習之外,Kannapat 也很享受在 Iron Software 工作的社交生活。不寫程式碼或文件時,Kannapat 通常會用 PS5 玩遊戲或重看《最後的我們》。
審核人
Jeff Fritz
Jeffrey T. Fritz
首席計畫經理 - .NET 社群團隊
Jeff 也是 .NET 和 Visual Studio 團隊的首席計畫經理。他是 .NET Conf 虛擬會議系列的執行製作人,並主持「Fritz and Friends」開發人直播串流,每週播出兩次,與觀眾一起討論技術和編寫程式碼。Jeff 為 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit 等大型 Microsoft 開發人員活動撰寫工作坊、簡報和規劃內容。
準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布