如何在C#中使用Tesseract的自定義語言
IronOCR通過.traineddata文件夾載的方法啟用自定義語言、專業腳本或移位的OCR,允許您從任何自定義訓練的語言模型中提取文字。
-
1Install IronOCR with NuGet Package Manager
-
2複製並運行這段程式碼片段。
using IronOcr; // Initialize OCR engine var ocr = new IronTesseract(); // Load custom language file ocr.UseCustomTesseractLanguageFile("custom.traineddata"); // Process document using var input = new OcrInput(); input.LoadImage("document.png"); // Extract text var result = ocr.Read(input); Console.WriteLine(result.Text);C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronOCR,透過免費試用
- 使用NuGet程式包管理員安裝
IronOcr - 使用
.traineddata文件 - 建立
OcrInput並載入您的文件 - 調用
Read()以提取自定義語言的文字 - 保存或處理提取的文字
光學字元識別(OCR)有時需要處理自定義語言、專門的腳本或密碼。 要讀取包含自定義語言的輸入圖像,必須為Tesseract引擎提供該特定語言的訓練資料。 這些資料儲存在一個特殊的.traineddata文件中。
雖然建立(訓練)此文件的複雜過程使用Tesseract自己的工具來完成,但IronOCR完全支持使用這些自定義語言文件。 這使您可以應用訓練好的模型來解讀和閱讀來自任何輸入的文字。 本指南演示了如何使用IronOCR載入和使用自定義.traineddata文件。
如何使用Tesseract的自定義語言
- 下載讀取自定義語言的C#程式庫
- 初始化OCR引擎
- 使用
UseCustomTesseractLanguageFile載入自定義語言訓練資料 - 使用
LoadImage載入輸入圖像 - 使用
Read讀取並提取自定義語言的輸入圖像
如何用Tesseract實現自定義語言OCR?
要使用Tesseract的自定義語言,首先調用.traineddata文件。 這是關鍵的一步,因為此文件包含所有讓Tesseract識別自定義語言獨特字元的訓練資料。
IronOCR的自定義語言支持超出標準語言。 無論您是處理歷史腳本,創造語言還是專業的符號系統,都適用於相同的過程。 對於需要多語言的專案,請參閱我們的指南閱讀多語言或了解125國際OCR語言,開箱即用的支持。
接下來,像處理常規OCR操作一樣載入輸入文件。 我們使用LoadPdf載入包含自定義語言段落的PDF。 IronOCR支持多種輸入格式,包括圖像(jpg, png, gif, tiff, bmp)和PDF。
最後,使用Read方法從輸入中提取文字。 然後可以將結果列印到控制台或保存到文字文件中以供參考。
我需要什麼訓練資料來處理自定義語言?
我們將使用這個包含自定義語言文字的樣本PDF作為輸入。
我們在例子中使用這個自定義語言.traindata。
訓練資料的質量和涵蓋範圍直接影響OCR的準確性。 準備自定義語言訓練資料時:
- 字元覆蓋率:確保您的訓練資料包含所有字元和符號
- 字體變化:如果您的文件字體變化,請包含多種字體風格
- 圖像品質:以與您在生產中處理的圖像相似的圖像進行訓練
- 上下文模式:包含常見的單詞組合和短語
有關高級配置選項,請參閱我們的Tesseract詳細配置指南。
如何載入和處理自定義語言文件?
using IronOcr;
using System;
using System.IO;
var ocrTesseract = new IronTesseract();
// Load the traineddata file for the custom language
ocrTesseract.UseCustomTesseractLanguageFile("AMGDT.traineddata");
using var ocrInput = new OcrInput();
// Load the PDF containing text in the custom language
ocrInput.LoadPdf("custom.pdf");
var ocrResult = ocrTesseract.Read(ocrInput);
// Print text to the console
Console.WriteLine("--- OCR Result ---");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("------------------");
// Pipe text to a .txt file
string outputFilePath = "ocr_output.txt";
File.WriteAllText(outputFilePath, ocrResult.Text);
Console.WriteLine($"\nSuccessfully saved text to {outputFilePath}");Imports IronOcr
Imports System
Imports System.IO
Dim ocrTesseract As New IronTesseract()
' Load the traineddata file for the custom language
ocrTesseract.UseCustomTesseractLanguageFile("AMGDT.traineddata")
Using ocrInput As New OcrInput()
' Load the PDF containing text in the custom language
ocrInput.LoadPdf("custom.pdf")
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Print text to the console
Console.WriteLine("--- OCR Result ---")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("------------------")
' Pipe text to a .txt file
Dim outputFilePath As String = "ocr_output.txt"
File.WriteAllText(outputFilePath, ocrResult.Text)
Console.WriteLine(vbCrLf & "Successfully saved text to " & outputFilePath)
End Using上面的程式碼演示了自定義語言OCR的基本工作流程。 對於更複雜的場景,請考慮以下增強功能:
優化性能:對於大型文件或批處理,實施多執行緒和異步支持以提高性能。
圖像預處理:如果您的來源文件存在質量問題,則在OCR處理之前應用圖像校正過濾器。 過濾器工具可以幫助您找到最佳的預處理設置。
特定區域的OCR:對於具有混合內容的文件,使用圖像的特定區域的OCR技術專注於包含自定義語言的特定區域。
自定義語言OCR會得到什麼結果?

這個輸出顯示了來自我們的自定義語言模型的結果。 通過提供正確的訓練資料,IronOCR成功地解碼了文字,結果是純英文。 此外,這是由程式碼生成的txt輸出。
自定義語言OCR的準確性取決於幾個因素:
- 訓練資料的質量:更好的訓練資料會產生更好的結果
- 文件一致性:與訓練資料匹配的文件效果最佳
- 圖像解析度:更高的DPI圖像會產生更精確的結果 - 請參閱我們的DPI設置指南
自定義語言實施的最佳實踐
在生產環境中實施自定義語言OCR時,請考慮這些最佳實踐:
錯誤處理和驗證:在嘗試載入之前,總是驗證您的.traineddata文件是否存在並可被存取。 為自定義語言文件可能遺失或損壞的情況實施適當的錯誤處理。
性能優化:自定義語言模型可能比標準語言包大。 為了獲得最佳性能:
與標準語言結合:如果您的文件同時包含自定義和標準語言,您可以同時載入多種語言。 這對於具有混合內容的文件特別有用。
測試和驗證:建立一個測試框架來驗證OCR的準確性:
進階應用案例
自定義語言OCR開啟很多可能性:
歷史文件保存:數字化古老手稿或以過時文字書寫的文字 專業符號系統:處理數學公式、樂譜或技術圖表 - 請參閱我們的方程式疑難排解指南 安全應用:解碼專有編碼系統或密碼 可訴性:將專業點字或觸覺書寫系統轉換為標準文字
對於更先進的場景,探索我們的全面程式碼範例,展示Tesseract 5的各種IronOCR功能。
常見問題
如何在文件上執行帶有自訂語言或腳本的OCR?
IronOCR透過UseCustomTesseractLanguageFile方法載入Tesseract .traineddata文件,實現自訂語言OCR。這允許您從任何自訂訓練的語言模型中提取文字,包括專門腳本、歷史文字或密碼。
自訂語言識別需要什麼文件格式?
IronOCR需要包含您自訂語言訓練資料的.traineddata文件。此文件使用UseCustomTesseractLanguageFile方法載入,並包含Tesseract識別您自訂語言獨特字元所需的所有資訊。
我可以在單次OCR操作中使用多個自訂語言嗎?
是的,IronOCR支援多語言識別。您可以載入多個自訂語言文件或將自訂語言與IronOCR內建支援的125種國際語言結合使用。
可以識別哪些型別的自訂腳本?
IronOCR可以識別任何被正確訓練到.traineddata文件中的自訂腳本,包括歷史腳本、自創語言、專門記號系統和密碼。此靈活性擴展至任何可用Tesseract工具訓練的書寫系統。
如何在我的C#應用程式中實現自訂語言OCR?
要用IronOCR實現自訂語言OCR:1)初始化一個IronTesseract實例,2)使用UseCustomTesseractLanguageFile載入您的自訂.traineddata文件,3)建立一個OcrInput物件並載入您的文件,4)呼叫Read()方法提取文字,5)根據需要處理提取的文字。
What are some best practices for using custom language models in IronOCR?
Best practices for using custom language models in IronOCR include validating the accessibility of `.traineddata` files, optimizing performance by caching the language model, using progress tracking, and combining custom languages with standard languages if needed.
How can IronOCR handle large documents or batch processing?
For large documents or batch processing, IronOCR supports multithreading and async operations to optimize performance. This way, you can handle more data concurrently, improving the processing efficiency.
Can IronOCR be used for historical document preservation?
Yes, IronOCR can be utilized for historical document preservation by digitizing and recognizing texts from ancient manuscripts or obsolete scripts. Providing accurate training data for these scripts will enhance recognition quality.
What should I include in my custom language training data?
Your custom language training data should cover all relevant characters, symbols, font variations, and common word patterns or phrases. This ensures that the OCR engine can accurately recognize and extract text from your input documents.
How do IronOCR's advanced configuration options enhance custom language OCR?
IronOCR's advanced configuration options include image preprocessing filters, region-specific OCR techniques, and progress-tracking methods that help optimize performance and accuracy when processing custom language documents.

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。