Reconnaissance de texte dans une image à l'aide d'une application .NET pour Linux

Catégorie du blog: ImagerieOCR.NETLinux

23.12.2022

Cet article explique comment créer une application console .NET et reconnaître du texte à partir d'images sous Ubuntu. La reconnaissance de texte utilise le VintaSoft Imaging .NET SDK et ses Plug-ins PDF, OCR et "Document Cleanup".

Voici les étapes à suivre:
  1. Ouvrez le bureau Ubuntu.

  2. Créez un dossier qui stockera les fichiers de l'application .NET. Créez le dossier "Recognize_Text_In_Image" sur le bureau de l'utilisateur actuel et accédez-y.


  3. Ouvrez le terminal de commande. Vous pouvez le faire en choisissant l'option "Ouvrir dans le terminal" dans le menu contextuel ou en appuyant sur la combinaison de touches Ctrl+Alt+T.


  4. Exécutez la commande dans le terminal pour créer un projet d'application console .NET:
    dotnet new console --framework net6.0
    



    Le projet créé contient le fichier projet "Recognize_Text_In_Image.csproj" et le fichier "Program.cs", qui contient le code C# de l'application. Fermez le terminal.

  5. Ouvrez le fichier projet "Recognize_Text_In_Image.csproj" dans un éditeur de texte et modifiez le texte du fichier comme suit:
    <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>
    



    Le projet modifié fait référence aux packages NuGet pour 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) et VintaSoft PDF .NET Plug-in (Vintasoft.Imaging.Pdf, Vintasoft.Imaging.Pdf.Ocr).

  6. Ouvrez le fichier "Program.cs" et modifiez son code pour utiliser le code C# suivant:
    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();
                }
            }
        }
    }
    



    Le code de l'application reconnaîtra le texte de l'image et enregistrera le résultat dans un document PDF consultable.

  7. Obtenez le code pour utiliser la version d'évaluation sous Linux en utilisant la méthode décrite dans documentation et insérez le code obtenu dans le code C# du fichier "Program.cs".


  8. Copiez le fichier "OCR.tif" dans le dossier du projet.


    Vous pouvez utiliser tout autre fichier contenant une image de document à la place du fichier "OCR.tif".

  9. Ouvrez le terminal et compilez le projet .NET à l’aide de la commande suivante:
    dotnet build Recognize_Text_In_Image.csproj
    



    Fermez le terminal.

  10. Accédez au dossier "bin/Debug/net6.0/".


  11. Ouvrez le terminal et exécutez l'application .NET à l'aide de la commande suivante:
    dotnet ./Recognize_Text_In_Image.dll
    



    Fermez le terminal.

  12. Ouvrez le document PDF créé et consultez les résultats: