如何使用 IronOCR 構建 Azure OCR 服務

This article was translated from English: Does it need improvement?
Translated
View the article in English

Iron Software 已創建了一個 OCR (光學字符識別) 解決 Azure OCR 整合相容性問題的庫。在 Azure 上使用 OCR 庫一直是開發人員的一大麻煩。解決這個以及許多其他 OCR 頭痛問題的方案是 IronOCR。

IronOCR 在 Microsoft Azure 上的特點

IronOCR 包含以下功能,可在 Microsoft Azure 上構建和 OCR 服務:

  • 將 PDF 轉換為可搜索的文檔,便於提取文本
  • 通過從圖像中提取文本,將圖像轉換為可搜索的文檔
  • 讀取條形碼和 QR 碼
  • 卓越的準確性
  • 本地運行,不需要 SaaS (軟體即服務) 這是一種軟體分發模型,其中雲供應商(如 Microsoft Azure )託管各種應用程式並將這些應用程式提供給終端使用者。

*閃電般的速度

讓我們看看最好的 OCR 引擎,Iron Software 的 IronOCR,如何使開發人員更輕鬆地從任何輸入文件中提取文字。

讓我們開始使用 Azure OCR 服務

為了開始這個示例,我們需要先安裝 IronOCR。

  1. 使用 C# 創建一個新的控制台應用程序

  2. 通過 NuGet 安裝 IronOCR,可以通過輸入以下命令:Install-Package IronOcr 或選擇管理 NuGet 套件並搜索 IronOCR。如下所示

  3. 編輯您的 Program.cs 文件,使其看起來像下面這樣:
    • 我們導入 IronOcr 命名空間,以便利用其 OCR 功能讀取和提取 PDF 文件的內容。
    • 我們創建一個新的 IronTesseract 對象,以便我們可以從圖像中提取文本。
using IronOcr;
using System;

namespace IronOCR_Ex
{
    class Program
    {
        static void Main(string [] args)
        {
            var ocr = new IronTesseract();
            using (var Input = new OcrInput("..\\Images\\Purgatory.PNG"))
            {
                var result = ocr.Read(Input); //Read PNG image File
                Console.WriteLine(result.Text); //Write Output to PDF document
                Console.ReadLine();
            }
        }
    }
}
using IronOcr;
using System;

namespace IronOCR_Ex
{
    class Program
    {
        static void Main(string [] args)
        {
            var ocr = new IronTesseract();
            using (var Input = new OcrInput("..\\Images\\Purgatory.PNG"))
            {
                var result = ocr.Read(Input); //Read PNG image File
                Console.WriteLine(result.Text); //Write Output to PDF document
                Console.ReadLine();
            }
        }
    }
}
Imports IronOcr
Imports System

Namespace IronOCR_Ex
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim ocr = New IronTesseract()
			Using Input = New OcrInput("..\Images\Purgatory.PNG")
				Dim result = ocr.Read(Input) 'Read PNG image File
				Console.WriteLine(result.Text) 'Write Output to PDF document
				Console.ReadLine()
			End Using
		End Sub
	End Class
End Namespace
VB   C#

接著,我們打開名為 Purgatory.PNG 的圖像。這幅圖像是但丁的《神曲》的一部分——這是我最喜愛的書之一。圖片看起來像下一張圖片。

以IronOCR的光學字符識別功能提取的文本

圖 2 - 使用IronOCR的光學字符識別功能提取的文本

  1. 從上述輸入圖像文本中提取上述文本後的輸出。

提取的文字

  • 圖3 - 提取的文本*
  1. 我們來對PDF文件做同樣的處理。PDF文件包含與圖相同的待提取文本。

唯一的區別是我們將使用PDF文件而不是圖像。輸入以下代碼:

 var Ocr = new IronTesseract();
            using (var input = new OcrInput())
            {
                input.Title = "Divine Comedy - Purgatory"; //Give title to input document 
                //Supply optional password and name of document
                input.AddPdf("..\\Documents\\Purgatorio.pdf", "dante");
                var Result = Ocr.Read(input); //Read the input file

                Result.SaveAsSearchablePdf("SearchablePDFDocument.pdf"); 
            }
 var Ocr = new IronTesseract();
            using (var input = new OcrInput())
            {
                input.Title = "Divine Comedy - Purgatory"; //Give title to input document 
                //Supply optional password and name of document
                input.AddPdf("..\\Documents\\Purgatorio.pdf", "dante");
                var Result = Ocr.Read(input); //Read the input file

                Result.SaveAsSearchablePdf("SearchablePDFDocument.pdf"); 
            }
Dim Ocr = New IronTesseract()
			Using input = New OcrInput()
				input.Title = "Divine Comedy - Purgatory" 'Give title to input document
				'Supply optional password and name of document
				input.AddPdf("..\Documents\Purgatorio.pdf", "dante")
				Dim Result = Ocr.Read(input) 'Read the input file

				Result.SaveAsSearchablePdf("SearchablePDFDocument.pdf")
			End Using
VB   C#

幾乎與先前從圖像中提取文字的程式碼相同。

在這裡,我們利用 OcrInput 方法來讀取當前的 PDF 文件,在這個案例中是:Purgatorio.pdf。如果 PDF 文件中有如標題或密碼等元數據,我們也可以將其輸入。

結果會保存為 PDF 文檔,並且我們可以在其中搜尋文本。

注意,如果 PDF 文件太大,可能會引發異常。

  1. 關於 Windows 應用程式的介紹已經足夠;讓我們看看如何在 Microsoft Azure 中使用 ocr。

IronOCR 的美妙之處在於,它作為微服務架構中的 Azure 功能,與 Microsoft Azure 配合得很好。這裡是一個簡單的例子,展示了一個與 IronOCR 協作的 Microsoft Azure 功能的樣子。這個 Microsoft Azure 功能從圖像中提取文本。

public static class OCRFunction
{
    public static HttpClient hcClient = new HttpClient();

    [FunctionName("IronOCRFunction_EX")]
    public static async Task<IActionResult> Run([HttpTrigger] HttpRequest hrRequest, ExecutionContext ecContext)
    {
        var URI = hrRequest.Query ["image"];
        var saStream = await hcClient.GetStreamAsync(URI);

        var ocr = new IronTesseract();
        using (var inputOCR = new OcrInput(saStream))
        {
            var outputOCR = ocr.Read(inputOCR);
            return new OkObjectResult(outputOCR.Text);
        }
    }
} 
public static class OCRFunction
{
    public static HttpClient hcClient = new HttpClient();

    [FunctionName("IronOCRFunction_EX")]
    public static async Task<IActionResult> Run([HttpTrigger] HttpRequest hrRequest, ExecutionContext ecContext)
    {
        var URI = hrRequest.Query ["image"];
        var saStream = await hcClient.GetStreamAsync(URI);

        var ocr = new IronTesseract();
        using (var inputOCR = new OcrInput(saStream))
        {
            var outputOCR = ocr.Read(inputOCR);
            return new OkObjectResult(outputOCR.Text);
        }
    }
} 
Public Module OCRFunction
	Public hcClient As New HttpClient()

	<FunctionName("IronOCRFunction_EX")>
	Public Async Function Run(<HttpTrigger> ByVal hrRequest As HttpRequest, ByVal ecContext As ExecutionContext) As Task(Of IActionResult)
		Dim URI = hrRequest.Query ("image")
		Dim saStream = Await hcClient.GetStreamAsync(URI)

		Dim ocr = New IronTesseract()
		Using inputOCR = New OcrInput(saStream)
			Dim outputOCR = ocr.Read(inputOCR)
			Return New OkObjectResult(outputOCR.Text)
		End Using
	End Function
End Module
VB   C#

這會將函數接收的圖片直接傳送到OCR引擎,以提取文本的形式輸出。

快速回顧 Microsoft Azure。

根據Microsoft所說:Microsoft Azure微服務是一種構建應用程序的架構方法,其中每個核心功能或服務都是獨立構建和部署的。微服務架構是分散且鬆散耦合的,因此即使一個組件出現故障也不會破壞整個應用程序。獨立的組件一起工作,並通過定義良好的API協定進行通信。構建微服務應用程序以滿足迅速變化的業務需求並更快地將新功能推向市場

IronOCR 與 .NET 或 Microsoft Azure 的一些更多功能包括以下:

能夠對幾乎任何文件、圖像或 PDF 執行光學字符識別(OCR)。

  • 閃電般的 OCR 輸入處理速度
  • 卓越的準確性
  • 讀取條形碼和 QR 碼
  • 本地運行,無需 SaaS
  • 可以將 PDF 和圖像轉換為可搜索的文檔
  • 微軟認知服務中的 Azure OCR 的優秀替代品

提高 OCR 性能的圖像過濾器

  • OcrInput.Rotate - 將圖像順時針旋轉幾度。對於逆時針旋轉,使用負數。
  • OcrInput.Binarize()** - 這個影像篩選器將每個像素轉換為黑色或白色,沒有中間值。這提高了OCR的性能。
  • OcrInput.ToGrayScale() - 此圖片濾鏡將每個像素變成灰度。這可提高 OCR 的速度
  • OcrInput.Contrast() - 自動增加對比度。此過濾器可提高低對比掃描的 OCR 速度和準確性。
  • OcrInput.DeNoise() - 去除數字噪音。此過濾器僅應用於預期輸入文件中有噪音的情況。
  • OcrInput.Invert() - 反轉每個顏色。
  • OcrInput.Dilate() - 擴張會向圖像中任何物體的邊界添加像素。
  • OcrInput.Erode() - 侵蝕移除物體邊界上的像素。
  • OcrInput.Deskew() - 將圖像旋轉至正確方向和正交。這對於OCR非常有用,因為Tesseract對於偏斜掃描的容忍度可能低至5度。
  • OcrInput.DeepCleanBackgroundNoise()** - 高強度背景噪音消除。
  • OcrInput.EnhanceResolution - 增強低質量圖像的解析度。

速度效能

範例如下:

    var Ocr = new IronTesseract();
    Ocr.Configuration.BlackListCharacters = "~`$#^*_}{][
\\";
    Ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
    Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
    Ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly;
    Ocr.Language = OcrLanguage.EnglishFast;
    using (var Input = new OcrInput("..\\Images\\Purgatory.PNG"))
    {
        var Result = Ocr.Read(Input);
        Console.WriteLine(Result.Text);
    }
    var Ocr = new IronTesseract();
    Ocr.Configuration.BlackListCharacters = "~`$#^*_}{][
\\";
    Ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
    Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
    Ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly;
    Ocr.Language = OcrLanguage.EnglishFast;
    using (var Input = new OcrInput("..\\Images\\Purgatory.PNG"))
    {
        var Result = Ocr.Read(Input);
        Console.WriteLine(Result.Text);
    }
Dim Ocr = New IronTesseract()
	Ocr.Configuration.BlackListCharacters = "~`$#^*_}{][ \\"
	Ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
	Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5
	Ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly
	Ocr.Language = OcrLanguage.EnglishFast
	Using Input = New OcrInput("..\Images\Purgatory.PNG")
		Dim Result = Ocr.Read(Input)
		Console.WriteLine(Result.Text)
	End Using
VB   C#

價格和許可選項

基本上有三種 付費授權等級 一律採用一次購買,終身授權的原則。

是的,這些對於開發用途是免費的。

更多資訊

IronOCR 功能適用於在 Azure 和其他系統上運行 OCR 的 .NET 應用程式

  • IronOCR 支援 127 種國際語言。每種語言都有快速、標準和最佳品質可供選擇。部分可用的語言包包括:

    • 保加利亞語

    • 亞美尼亞語

    • 克羅埃西亞語

    • 南非語

    • 丹麥語

    • 捷克語

    • 菲律賓語

    • 芬蘭語

    • 法語

    • 德語
  • 還有許多其他語言包可用,請點擊以下鏈接進行查看。 IronOCR 語言包
  • 它在 .NET 中開箱即用

    • 支援 Xamarin

    • 支援 Mono

    • 支援 Microsoft Azure

    • 支援 Microsoft Azure 上的 Docker

    • 支援 PDF 文件

    • 支援多幀 Tiffs
  • 支援所有主要的圖像格式
  • 支援以下 .NET 框架:

    • .NET Framework 4.5 及更高版本

    • .NET Standard 2

    • .NET Core 2

    • .NET Core 3
  • .NET Core 5
  • 你不需要擁有 Tesseract (一個支持Unicode並支持超過100種語言的開源OCR引擎) 安裝 IronOCR 以運行。
  • 比 Tesseract 擁有更高的準確性
  • 比 Tesseract 擁有更快的速度
  • 修正低品質的文件或檔案掃描
  • 修正低品質傾斜的文件或檔案掃描

什麼是 (光學字符識別) OCR?

根據維基百科:光學字符識別是將打字、印刷文本的圖像轉換成機器編碼文本的電子或機械過程,無論是來自掃描的文檔、文檔的照片、場景照片或疊加在圖像上的字幕文本。OCR代表光學字符識別。基本上有四種類型的光學字符識別,它們是:

  • OCR - 光學字符識別,針對輸入文檔中的打字文本,逐個字符或字形 (在約定的符號集中,元素符號,例如不同字體中的「a」) 同時。
  • OWR - 光學單詞識別,針對輸入文件中的打字文本,一次一個單詞
  • ICR - 智能字符識別,針對打印腳本等印刷文本 (不與其他字母連接的字元) 以及草書字體,一次一個字母或字形
  • IWR - 智能文字識別,針對草書字體。