Riconoscere il testo in un'immagine utilizzando un'applicazione .NET per Linux

Categoria del blog: ImagingOCR.NETLinux

23.12.2022

Questo articolo spiega come creare un'applicazione console .NET e riconoscere il testo dalle immagini in Ubuntu. Per il riconoscimento del testo dalle immagini vengono utilizzati VintaSoft Imaging .NET SDK e i suoi Plug-in PDF, OCR e Document Cleanup.

Ecco i passaggi per completare questa operazione:
  1. Aprire il desktop di Ubuntu.

  2. Creare una cartella in cui archiviare i file dell'applicazione .NET. Creiamo la cartella "Recognize_Text_In_Image" sul desktop dell'utente corrente e procediamo alla sua apertura.


  3. Aprire il terminale di comando della console. Questo può essere fatto selezionando la voce "Apri nel terminale" nel menu contestuale o premendo la combinazione di tasti Ctrl+Alt+T.


  4. Richiamare il comando nel terminale, che crea un progetto per una nuova applicazione .NET della console:
    dotnet new console --framework net6.0
    



    Il progetto creato contiene il file di progetto "Recognize_Text_In_Image.csproj" e il file "Program.cs", che contiene il codice C# dell'applicazione. Chiudere il terminale.

  5. Aprire il file di progetto "Recognize_Text_In_Image.csproj" nell'editor di testo e modificare il testo del file nel seguente modo:
    <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>
    



    Il progetto modificato fa riferimento ai pacchetti nuget per VintaSoft Imaging .NET SDK (Vintasoft.Shared.dll, Vintasoft.Imaging.dll, Vintasoft.Imaging.Drawing.SkiaSharp.dll), al VintaSoft Document Cleanup .NET Plug-in (Vintasoft.Imaging.DocCleanup.dll), al VintaSoft OCR .NET Plug-in (Vintasoft.Imaging.Ocr.dll, Vintasoft.Imaging.Ocr.Tesseract.dll) e al VintaSoft PDF .NET Plug-in (Vintasoft.Imaging.Pdf, Vintasoft.Imaging.Pdf.Ocr).

  6. Aprire il file "Program.cs" e modificare il suo codice nel seguente codice 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();
                }
            }
        }
    }
    



    Il codice dell'applicazione riconoscerà il testo dall'immagine e salverà il risultato in un documento PDF ricercabile.

  7. Ottieni il codice per utilizzare la versione di valutazione in Linux seguendo il metodo descritto nella documentazione e inserisci il codice ottenuto nel codice C# del file "Program.cs".


  8. Copia il file "OCR.tif" nella cartella del progetto.


    Puoi utilizzare qualsiasi altro file contenente un'immagine di documento al posto del file "OCR.tif".

  9. Apri il terminale e compila il progetto .NET utilizzando il seguente comando:
    dotnet build Recognize_Text_In_Image.csproj
    



    Chiudi il terminale.

  10. Vai alla cartella "bin/Debug/net6.0/".


  11. Apri il terminale ed esegui l'applicazione .NET utilizzando il seguente comando:
    dotnet ./Recognize_Text_In_Image.dll
    



    Chiudi il terminale.

  12. Apri il documento PDF creato e guarda i risultati: