How to use Custom Language with Tesseract

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

When it comes to optical character recognition (OCR), you sometimes need to deal with custom languages, specialized scripts, or ciphers. To read an input image containing a custom language, the Tesseract engine must be provided with training data for that specific language. This data is stored in a special .traineddata file.

While the complex process of creating (training) this file is done using Tesseract's own tools, IronOCR fully supports using these custom language files. This lets you apply your trained model to decipher and read text from any input. In this how-to guide, we'll showcase how to load and use a custom .traineddata file with IronOCR.

Get started with IronOCR

今天在您的项目中使用 IronOCR,免费试用。

第一步:
green arrow pointer


Custom Language with Tesseract

To use a custom language with Tesseract, we must first load our .traineddata file by calling the UseCustomTesseractLanguageFile method. This is an essential step, as this file contains all the training data that allows Tesseract to recognize the custom language's unique characters.

Afterward, we load our input document just as we would for a regular OCR operation. In this instance, we are loading a PDF containing custom language paragraphs using LoadPdf.

Finally, we use the Read method to extract the text from the input. The result can then be printed to the console or, as the example shows, saved (piped) to a text file for reference.

Input

We'll use this sample PDF, which contains text in our custom language, as the input.

We'll be using this custom language .traindata for our example.

Code Example

:path=/static-assets/ocr/content-code-examples/how-to/ocr-custom-language.cs
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}");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

OCR Output text

This output shows the result from our custom language model. As you can see, by providing the correct training data, IronOCR successfully deciphered the text, and the result is in plain English. Additionally, this is the txt output generated by the code.

常见问题解答

在 IronOCR 中使用 Tesseract 的自定义语言的目的是什么?

在 IronOCR 中使用 Tesseract 的自定义语言功能,可以识别并提取图像或 PDF 文件中包含的特殊脚本或默认不支持的语言的文本。这可以通过加载包含该语言所需训练数据的自定义 `.traineddata` 文件来实现。

如何在 IronOCR 中加载自定义语言训练数据文件?

您可以使用 `UseCustomTesseractLanguageFile` 方法在 IronOCR 中加载自定义语言训练数据文件。这一步至关重要,因为它为 Tesseract 引擎提供了识别自定义语言独特字符所需的训练数据。

使用 IronOCR 对包含自定义语言的图像进行 OCR 识别的步骤是什么?

要使用 IronOCR 对自定义语言的图像执行 OCR,首先下载 C# 库,初始化 OCR 引擎,使用 `UseCustomTesseractLanguageFile` 加载自定义语言训练数据,使用 `LoadImage` 加载输入图像,最后使用 `Read` 方法提取文本。

IronOCR 能否处理包含自定义语言文本的 PDF 文件?

是的,IronOCR 可以处理包含自定义语言文本的 PDF 文件。您可以使用 `LoadPdf` 方法加载 PDF 文件,然后使用 `Read` 方法根据提供的自定义语言训练数据提取文本。

在 Tesseract 和 IronOCR 的上下文中,`.traineddata` 文件是什么?

`.traineddata` 文件是 Tesseract OCR 使用的数据文件,其中包含特定语言的训练数据。它使 OCR 引擎能够识别和处理该语言的字符,并且可以在 IronOCR 中用于处理自定义语言。

我是否需要为 IronOCR 中的每种自定义语言创建自己的 `.traineddata` 文件?

不,您无需为每种自定义语言都创建自己的 `.traineddata` 文件。您可以直接使用现有的 `.traineddata` 文件(如果有的话)。但是,如果某种语言不受支持,您可能需要使用 Tesseract 的工具创建一个。

使用自定义语言时,IronOCR 支持哪些输出格式?

IronOCR 在使用自定义语言时支持多种输出格式,例如纯文本输出,可以打印到控制台或保存到文本文件。提取的文本可以根据需要进行进一步处理。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,044,537 | 版本: 2025.11 刚刚发布