Tesseract를 사용하여 이미지에서 텍스트를 추출하는 방법
IronOCR 및 Tesseract와 같은 라이브러리를 활용하면 개발자가 이미지 및 스캔된 문서에서 텍스트 정보를 추출하는 데 고급 알고리즘 및 기계 학습 기술에 접근할 수 있습니다. 이 튜토리얼에서는 Tesseract 라이브러리를 사용하여 이미지에서 텍스트를 추출하는 방법을 독자에게 보여주고 IronOCR의 독특한 접근 방식을 소개할 것입니다.
1. Tesseract와 함께하는 OCR
1.1. Tesseract 설치
NuGet 패키지 관리자 콘솔을 사용하여 다음 명령어를 입력하세요:
Install-Package Tesseract
NuGet 패키지 관리자에서 패키지를 다운로드하세요.
NuGet 패키지 관리자에서 Tesseract Install-Package
NuGet 패키지를 설치한 후 프로젝트 폴더에 언어 파일을 수동으로 설치하고 저장해야 합니다. 이것은 이 특정 라이브러리의 단점으로 간주될 수 있습니다.
언어 파일을 다운로드하려면 이 웹사이트를 방문하세요. 다운로드 후 파일을 압축 해제하고 "tessdata" 폴더를 프로젝트의 디버그 폴더에 추가하세요.
1.2. Tesseract 사용법(빠른 시작)
주어진 이미지에서 OCR은 아래 소스 코드를 사용하여 수행할 수 있습니다:
using Tesseract;
class Program
{
static void Main()
{
// Initialize Tesseract engine with English language data
using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default);
// Load the image to be processed
using var img = Pix.LoadFromFile("Demo.png");
// Process the image to extract text
using var res = ocrEngine.Process(img);
// Output the recognized text
Console.WriteLine(res.GetText());
Console.ReadKey();
}
}
using Tesseract;
class Program
{
static void Main()
{
// Initialize Tesseract engine with English language data
using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default);
// Load the image to be processed
using var img = Pix.LoadFromFile("Demo.png");
// Process the image to extract text
using var res = ocrEngine.Process(img);
// Output the recognized text
Console.WriteLine(res.GetText());
Console.ReadKey();
}
}
Imports Tesseract
Friend Class Program
Shared Sub Main()
' Initialize Tesseract engine with English language data
Dim ocrEngine = New TesseractEngine("tessdata", "eng", EngineMode.Default)
' Load the image to be processed
Dim img = Pix.LoadFromFile("Demo.png")
' Process the image to extract text
Dim res = ocrEngine.Process(img)
' Output the recognized text
Console.WriteLine(res.GetText())
Console.ReadKey()
End Sub
End Class
- 먼저, 엔진에 언어 데이터를 로드하는
TesseractEngine객체를 생성해야 합니다. - 그런 다음,
Pix.LoadFromFile의 도움으로 원하는 이미지 파일을 로드합니다. - 이미지를
TesseractEngine에 전달하여Process메서드를 사용해 텍스트를 추출합니다. GetText메서드를 사용하여 인식된 텍스트를 얻고 콘솔에 출력합니다.
이미지에서 추출된 텍스트
1.3 Tesseract 고려사항
- Tesseract는 버전 3.00부터 출력 텍스트 서식 지정, OCR 위치 데이터, 페이지 레이아웃 분석을 지원합니다.
- Tesseract는 Windows, Linux, MacOS에서 사용할 수 있지만, 제한된 개발 지원으로 인해 주로 Windows 및 Ubuntu에서 의도한 대로 작동하는 것이 확인되었습니다.
- Tesseract는 고정 폭 및 비례 간격 텍스트를 구별할 수 있습니다.
- OCRopus와 같은 프론트엔드를 사용하면, Tesseract는 백엔드로 사용하기에 이상적이며 레이아웃 분석과 같은 더 까다로운 OCR 작업에 사용할 수 있습니다.
- Tesseract의 몇 가지 단점:
- 최신 빌드는 Windows에서 컴파일하도록 설계되지 않았습니다.
- Tesseract의 C# API 래퍼는 자주 유지 관리되지 않으며 새로운 Tesseract 릴리스보다 몇 년 뒤떨어져 있습니다.
C#의 Tesseract에 대해 더 알고 싶다면 Tesseract 튜토리얼을 방문하세요.
2. IronOCR와 함께하는 OCR
2.1. IronOCR 설치
NuGet 패키지 관리자 콘솔에 다음 명령을 입력하세요:
Install-Package IronOcr
또는 IronOCR 라이브러리를 다른 언어용 추가 패키지와 함께 NuGet 패키지 관리자를 통해 설치하세요. 이는 간단하고 편리합니다.
NuGet 패키지 관리자를 통해 IronOcr 및 언어 Install-Package
2.2. IronOCR 사용하기
아래는 주어진 이미지에서 텍스트를 인식하는 샘플 코드입니다:
using IronOcr;
class Program
{
static void Main()
{
// Create an IronTesseract instance with predefined settings
var ocr = new IronTesseract()
{
Language = OcrLanguage.EnglishBest,
Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
};
// Create an OcrInput instance for image processing
using var input = new OcrInput();
// Load the image to be processed
input.AddImage("Demo.png");
// Process the image and extract text
var result = ocr.Read(input);
// Output the recognized text
Console.WriteLine(result.Text);
Console.ReadKey();
}
}
using IronOcr;
class Program
{
static void Main()
{
// Create an IronTesseract instance with predefined settings
var ocr = new IronTesseract()
{
Language = OcrLanguage.EnglishBest,
Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
};
// Create an OcrInput instance for image processing
using var input = new OcrInput();
// Load the image to be processed
input.AddImage("Demo.png");
// Process the image and extract text
var result = ocr.Read(input);
// Output the recognized text
Console.WriteLine(result.Text);
Console.ReadKey();
}
}
Imports IronOcr
Friend Class Program
Shared Sub Main()
' Create an IronTesseract instance with predefined settings
Dim ocr = New IronTesseract() With {
.Language = OcrLanguage.EnglishBest,
.Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
}
' Create an OcrInput instance for image processing
Dim input = New OcrInput()
' Load the image to be processed
input.AddImage("Demo.png")
' Process the image and extract text
Dim result = ocr.Read(input)
' Output the recognized text
Console.WriteLine(result.Text)
Console.ReadKey()
End Sub
End Class
- 이 코드는 언어 및 Tesseract 버전을 설정하는
IronTesseract객체를 초기화합니다. - 그 다음으로,
OcrInput객체를 생성하여AddImage메서드를 사용해 이미지 파일을 로드합니다. IronTesseract의Read메서드는 이미지를 처리하고 텍스트를 추출하며, 이는 콘솔에 출력됩니다.
IronOCR 라이브러리를 사용한 추출된 텍스트 출력
2.3 IronOCR 고려사항
- IronOCR는 안정성과 높은 정확도를 제공하는 Tesseract 라이브러리의 확장입니다.
- IronOCR는 PDF와 사진에서 텍스트 콘텐츠를 읽을 수 있습니다. 20가지 이상의 다양한 바코드와 QR 코드를 읽을 수도 있습니다.
- 출력은 일반 텍스트, 구조적 데이터, 바코드 또는 QR 코드로 렌더링될 수 있습니다.
- 라이브러리는 전 세계 125개의 언어를 인식합니다.
- IronOCR는 모든 .NET 환경에서 유연하게 작동하며(콘솔, 웹, 데스크탑 등), Mono, Xamarin, Azure, MAUI와 같은 최신 모바일 프레임워크도 지원합니다.
- IronOCR는 무료 체험판을 제공하며 저렴한 개발 에디션을 갖추고 있습니다. 라이선싱에 대해 더 알아보세요.
상세한 IronOCR 튜토리얼은 C#에서 이미지를 통한 텍스트 읽기를 위한 기사를 참조하세요.




