如何使用IronOCR建構Azure OCR服務
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。
使用C#創建一個新的控制台應用程序。
透過 NuGet 安裝 IronOCR,可以輸入:Install-Package IronOcr,或選擇管理 NuGet 套件並搜尋 IronOCR。 以下所示
- 將您的 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
接下來,我們打開一個名為 Purgatory.PNG 的圖片。 這張圖片是但丁的《神曲》之一部分 - 這是我最喜歡的書之一。 這張圖片看起來像下一張圖像。
圖 2 - 使用 IronOCR 的光學字符識別功能提取的文本
從上述輸入圖像文本中提取的文本之後的輸出。
圖 3 - 提取的文本
讓我們對 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
幾乎與先前從圖像中提取文字的代碼相同。
在這裡我們使用 OcrInput 方法來讀取當前的 PDF 文件,本例中為:Purgatorio.pdf。 如果PDF文件中有元數據,如標題或密碼,我們也可以加入這些信息。
結果將被保存為 PDF 文件,我們可以在其中搜索文本。
注意,如果 PDF 檔案太大,可能會拋出異常。
已經夠多關於Windows應用程式的內容了; 讓我們來看看如何在Microsoft Azure中使用OCR。
IronOCR的美妙之處在於它與Microsoft Azure配合得非常好,可作為微服務架構中的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
這將函數接收到的影像直接饋送到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() - 旋轉影像使其正確朝上並呈正交。 這對於光學字符識別非常有用,因為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
價格和許可選項
基本上有三個付費授權級別,全部按照一次性購買、終身授權的原則運作。
而且是的,這些都是免費用於開發目的的。
更多信息
IronOCR 為 .NET 應用程式提供在 Azure 及其他系統上運行 OCR 的功能
IronOCR 支持 127 種國際語言。 每種語言都有快速、標準和最佳品質可供選擇。 有些可用的语言包包括:
保加利亞語
亞美尼亞語
克羅埃西亞語
南非語
丹麥文
捷克
菲律賓语
芬蘭語
法文
- 德文
- 有更多語言包可供查看,請按照以下連結。 IronOCR 語言包
它在 .NET 中即開即用。
支援 Xamarin
支持 Mono
支持 Microsoft Azure
支持在 Microsoft Azure 上使用 Docker
支援 PDF 文件
- 支援多幀 Tiff 圖像
- 支援所有主要圖片格式
支援以下 .NET Frameworks:
.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 - 針對手寫文本的智能文字識別技術。