Linux用.NETアプリケーションを使用して画像内のテキストを認識する

ブログ カテゴリ: イメージングOCR.NET ; Linux

2022/12/23

この記事では、Ubuntuでコンソール.NETアプリケーションを作成し、画像からテキストを認識する方法について説明します。画像からのテキスト認識には、VintaSoft Imaging .NET SDKと、そのPDF、OCR、Document Cleanup Plug-insを使用します。

タスクを完了する手順は次のとおりです。
  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 ドキュメントを開いて結果を確認します。