Linux용 .NET 애플리케이션을 사용하여 이미지에서 텍스트 인식

블로그 카테고리: 이미징OCR.NET리눅스

2022/12/23

이 문서에서는 콘솔 .NET 애플리케이션을 만들고 우분투에서 이미지에서 텍스트를 인식하는 방법을 설명합니다. 이미지에서 텍스트를 인식하기 위해 다음이 사용됩니다. VintaSoft Imaging .NET SDK 및 PDF, OCR, 문서 정리 플러그인.

다음은 작업을 완료하는 단계입니다.
  1. 우분투 데스크톱을 엽니다.

  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).

  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."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 문서를 열고 결과를 확인합니다.