在 .NET 中从图像中识别 MRZ 字符

博客类别:OCR.NET

2022/02/17

VintaSoft Imaging .NET SDKVintaSoft OCR .NET Plug-in 插件允许使用 Tesseract OCR 引擎从图像中识别文本。Tesseract OCR 引擎拥有大量词典,支持 100 多种语言的文本识别。

互联网上有一些作者推荐了他们用于使用 Tesseract OCR 引擎识别 MRZ 符号(机读区)的免费词典。
我们测试了其中一些词典,并确保词典"mrz.traineddata"能够提供高质量的 MRZ 符号识别。
"mrz.traineddata"字典以BSD-3许可证提供,允许免费使用和重新分发此文件。
我们还提供"mrz.traineddata"字典的下载,可从下载。 target="_blank"]我们的网站,该词典也可以从其他互联网资源下载。

自VintaSoft OCR .NET插件11.0.5.1版本起,"mrz.traineddata"字典已添加到支持的字典列表(Vintasoft.Imaging.Ocr.OcrLanguage枚举中的MRZ项)中。

关于 MRZ 符号的更多详细信息,您可以在维基百科上阅读:https://en.wikipedia.org/wiki/Machine-readable_passport

以下是来自维基百科的一张图片,它展示了一个包含机器可读区 (MRZ) 的文档:



以下是 C# 代码,演示了如何使用 Tesseract OCR 引擎从图像中识别 MRZ 符号:
/// <summary>
/// Recognizes MRZ characters from image using Tesseract OCR engine.
/// </summary>
/// <param name="filename">The name of file, which stores image with MRZ characters.</param>
public static void RecognizeMRZCharactersUsingTesseractOCR(string filename)
{
    // create an image collection
    using (Vintasoft.Imaging.ImageCollection images = 
        new Vintasoft.Imaging.ImageCollection())
    {
        // add images from file to the image collection
        images.Add(filename);

        System.Console.WriteLine("Create Tesseract OCR engine...");
        // create the Tesseract OCR engine
        using (Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr tesseractOcr = 
            new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr())
        {
            System.Console.WriteLine("Initialize OCR engine...");
            // init the Tesseract OCR engine for recognition of MRZ characters (machine-readable zones)
            tesseractOcr.Init(new Vintasoft.Imaging.Ocr.OcrEngineSettings(Vintasoft.Imaging.Ocr.OcrLanguage.MRZ));

            // for each image in image collection
            foreach (Vintasoft.Imaging.VintasoftImage image in images)
            {
                System.Console.WriteLine("Recognize the image...");
                
                // recognize text in image
                Vintasoft.Imaging.Ocr.Results.OcrPage ocrResult = tesseractOcr.Recognize(image);

                // output the recognized text

                System.Console.WriteLine("Page Text:");
                System.Console.WriteLine(ocrResult.GetText());
                System.Console.WriteLine();
            }

            // shutdown the Tesseract OCR engine
            tesseractOcr.Shutdown();
        }

        // free images
        images.ClearAndDisposeItems();
    }
}