使用适用于 Linux 的 .NET 应用程序识别图像中的文本

博客分类:成像光学字符识别.NETLinux

2022/12/23

本文介绍如何在 Ubuntu 系统中创建 .NET 控制台应用程序并识别图像中的文本。图像文本识别使用VintaSoft Imaging .NET SDK及其 PDF、OCR 和文档清理插件。

以下是完成此任务的步骤:
  1. 打开 Ubuntu 桌面。

  2. 创建一个文件夹,用于存储 .NET 应用程序的文件。让我们在当前用户的桌面上创建一个名为"Recognize_Text_In_Image"的文件夹,并进入该文件夹。


  3. 打开控制台命令终端。可以通过在上下文菜单中选择"在终端中打开"或按组合键 Ctrl+Alt+T 来完成此操作。


  4. 在终端中调用命令,创建一个新的 .NET 控制台应用程序项目:
    dotnet new console --framework net6.0
    



    创建的项目包含项目文件"Recognize_Text_In_Image.csproj"和包含应用程序 C# 代码的"Program.cs"文件。关闭终端。

  5. 在文本编辑器中打开项目文件"Recognize_Text_In_Image.csproj",并将文件文本更改为以下文本:
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <RootNamespace>ConsoleApp1</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="SkiaSharp" Version="2.88.0" />
        <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.0" />
        <PackageReference Include="Vintasoft.Imaging" Version="12.1.5.1" />
        <PackageReference Include="Vintasoft.Imaging.Drawing.SkiaSharp" Version="12.1.5.1" />
        <PackageReference Include="Vintasoft.Imaging.DocCleanup" Version="7.1.5.1" />
        <PackageReference Include="Vintasoft.Imaging.Ocr" Version="7.1.5.1" />
        <PackageReference Include="Vintasoft.Imaging.Ocr.Tesseract" Version="7.1.5.1" />
        <PackageReference Include="Vintasoft.Imaging.Pdf" Version="9.1.5.1" />
        <PackageReference Include="Vintasoft.Imaging.Pdf.Ocr" Version="9.1.5.1" />
        <PackageReference Include="Vintasoft.Shared" Version="3.3.1.1" />
      </ItemGroup>
    
      <ItemGroup>
        <Content Include="OCR.tif">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    
    </Project>
    



    更改后的项目引用了 VintaSoft Imaging .NET SDK (Vintasoft.Shared.dll、Vintasoft.Imaging.dll、Vintasoft.Imaging.Drawing.SkiaSharp.dll)、VintaSoft Document Cleanup .NET Plug-in (Vintasoft.Imaging.DocCleanup.dll)、VintaSoft OCR .NET Plug-in (Vintasoft.Imaging.Ocr.dll、Vintasoft.Imaging.Ocr.Tesseract.dll) 和 VintaSoft PDF .NET Plug-in (Vintasoft.Imaging.Pdf、Vintasoft.Imaging.Pdf.Ocr) 的 NuGet 包。

  6. 打开文件"Program.cs",并将其代码更改为以下 C# 代码:
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Vintasoft.Imaging.ImagingGlobalSettings.Register("%EVAL_LIC_USER_NAME%", "%EVAL_LIC_USER_EMAIL%", "%EVAL_LIC_DATE%", "%EVAL_LIC_REG_CODE%");
    
                string imageFilePath = "OCR.tif";
    
                string tesseractOcrPath = "TesseractOCR";
                // create the OCR engine
                using (Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr tesseractOcr = new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr(tesseractOcrPath))
                {
                    // specify that OCR engine will recognize English text
                    Vintasoft.Imaging.Ocr.OcrLanguage language = Vintasoft.Imaging.Ocr.OcrLanguage.English;
                    // create the OCR engine settings
                    Vintasoft.Imaging.Ocr.Tesseract.TesseractOcrSettings settings = new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcrSettings(language);
                    // initialize the OCR engine
                    tesseractOcr.Init(settings);
    
                    // load an image with text
                    using (Vintasoft.Imaging.VintasoftImage image = new Vintasoft.Imaging.VintasoftImage(imageFilePath))
                    {
                        // preprocess image before text recognition
    
                        // remove noise from image
                        Vintasoft.Imaging.ImageProcessing.Document.DespeckleCommand despeckleCommand = new Vintasoft.Imaging.ImageProcessing.Document.DespeckleCommand();
                        despeckleCommand.ExecuteInPlace(image);
                        // remove lines from image
                        Vintasoft.Imaging.ImageProcessing.Document.LineRemovalCommand lineRemovalCommand = new Vintasoft.Imaging.ImageProcessing.Document.LineRemovalCommand();
                        lineRemovalCommand.ExecuteInPlace(image);
    
                        // specify an image with text
                        tesseractOcr.SetImage(image);
    
                        // recognize text in image
                        Vintasoft.Imaging.Ocr.Results.OcrPage ocrResult = tesseractOcr.Recognize();
    
                        // create PDF document
                        using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = new Vintasoft.Imaging.Pdf.PdfDocument("OCR.pdf", Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
                        {
                            // create PDF document builder
                            Vintasoft.Imaging.Pdf.Ocr.PdfDocumentBuilder documentBuilder = new Vintasoft.Imaging.Pdf.Ocr.PdfDocumentBuilder(pdfDocument);
                            documentBuilder.ImageCompression = Vintasoft.Imaging.Pdf.PdfCompression.Auto;
                            documentBuilder.PageCreationMode = Vintasoft.Imaging.Pdf.Ocr.PdfPageCreationMode.ImageOverText;
    
                            // add OCR result to the PDF document
                            documentBuilder.AddPage(image, ocrResult);
    
                            // save changes in PDF document
                            pdfDocument.SaveChanges();
                        }
    
                        // clear the image
                        tesseractOcr.ClearImage();
                    }
                    // shutdown the OCR engine
                    tesseractOcr.Shutdown();
                }
            }
        }
    }
    



    应用程序代码将从图像中识别文本,并将结果保存到可搜索的 PDF 文档中。

  7. 按照文档中描述的方式获取在 Linux 中使用评估版本的代码,并将获取的代码插入到"Program.cs"文件的 C# 代码中。


  8. 将"OCR.tif"文件复制到项目文件夹。


    您可以使用任何其他包含文档图像的文件代替"OCR.tif"文件。

  9. 打开终端并使用以下命令编译 .NET 项目:
    dotnet build Recognize_Text_In_Image.csproj
    



    关闭终端。

  10. 转到"bin/Debug/net6.0/"文件夹。


  11. 打开终端并使用以下命令运行 .NET 应用程序:
    dotnet ./Recognize_Text_In_Image.dll
    



    关闭终端。

  12. 打开创建的 PDF 文档并查看结果: