白皮書

使用機器學習軟體從影像中萃取文字

技術

利用機器學習從捕獲或掃描的文檔中的文本圖像中提取文本,是電腦視覺和自然語言處理交叉領域的一個新興領域。 這項技術利用先進的機器學習、物件辨識演算法、先進的圖形軟體、深網和暗網以及神經網路架構,準確地從影像和掃描的紙本文件中識別和提取文字資訊。 透過採用光學字元辨識 (OCR) 和深度學習等各種機器學習技術,它可以自動有效地將視覺場景中的文字偵測轉換為可編輯和可搜尋的結構化數據,並實現物件偵測。

在這個不斷變化的環境中,研究人員和從業人員不斷努力提高準確性、速度和多功能性,使得從圖像、機器可讀資料和掃描文件中檢測和提取文字成為印刷文件數位化、內容索引、翻譯和輔助功能增強等應用的關鍵組成部分。

在本文中,我們將討論如何使用IronOCR (一個由強大的機器學習演算法和文字相關特徵驅動的 OCR 庫)從圖像中提取文字。 文字擷取,也稱為關鍵字擷取,是基於機器學習的,它能夠自動掃描非結構化資料或公司中央資料庫中的相關或基本字詞和短語。

如何使用機器學習從圖像中提取文字?

  1. 下載用於從圖像中提取文字的 C# 庫。
  2. 透過實例化OcrInput物件載入特定圖像以進行場景文字辨識。
  3. 使用ocrTesseract.Read方法從影像中擷取資料。
  4. 使用Console.WriteLine方法將擷取的文字列印到控制台。
  5. 使用CropRectangle物件對影像區域執行 OCR。

IronOCR - 一個 OCR(光學字元辨識)庫

IronOCR 是一款傑出且先進的光學字元辨識 (OCR) 軟體,在影像和文件文字擷取技術領域處於領先地位。 這款由 Iron Software 開發的強大 OCR 引擎旨在準確且有效率地將掃描影像、PDF 甚至照片中的文字轉換為可編輯和可搜尋的數位內容。 IronOCR 憑藉其對機器學習演算法和神經網路的熟練運用,為各種應用提供了強大的解決方案,包括資料擷取、內容索引以及需要精確文字辨識的自動化流程。

它能夠處理多種語言和各種字體,對於希望在軟體和應用程式中實現簡化的文字辨識演算法提取功能的開發人員和企業來說,它是一款用途廣泛的工具。 您可以使用 IronOCR 透過常見的文本識別技術自動掃描文本,該技術使用文本提取演算法將非結構化資料轉換為完美的掃描頁面。

安裝 IronOCR

IronOCR 可以使用 NuGet 套件管理器進行安裝。 以下是安裝 IronOCR 的步驟:

  1. 首先,建立一個新的 C# Visual Studio 專案或開啟一個現有的專案。

Visual Studio

  1. 專案建立完成後,前往頂部功能表中的"工具",然後選擇"NuGet 套件管理器",再選擇"解決方案的 NuGet 套件管理器"。

工具選單

  1. 螢幕上將出現一個新視窗。 前往"瀏覽"選項卡,然後在搜尋欄中輸入 IronOCR。
  2. 將顯示 IronOCR 軟體包清單。 選擇最新版本,然後點選安裝。

IronOCR

  1. 這需要幾秒鐘時間,具體取決於您的網路速度; 之後,IronOCR 就可以在您的 C# 專案中使用了。

從圖像中檢測文本,得到可編輯和可搜尋的數據

使用 IronOCR,您可以利用影像處理技術和機器學習輕鬆擷取文字。 在本節中,我們將討論如何使用 IronOCR 從圖像中提取文字。

using IronOcr;
using System;

// Create a new instance of the IronTesseract class
var ocrTesseract = new IronTesseract();

// Specify the image path and perform OCR on the image
using (var ocrInput = new OcrInput(@"images\image.png"))
{
    var ocrResult = ocrTesseract.Read(ocrInput);

    // Print the extracted text to the console
    Console.WriteLine(ocrResult.Text);
}
using IronOcr;
using System;

// Create a new instance of the IronTesseract class
var ocrTesseract = new IronTesseract();

// Specify the image path and perform OCR on the image
using (var ocrInput = new OcrInput(@"images\image.png"))
{
    var ocrResult = ocrTesseract.Read(ocrInput);

    // Print the extracted text to the console
    Console.WriteLine(ocrResult.Text);
}
Imports IronOcr
Imports System

' Create a new instance of the IronTesseract class
Private ocrTesseract = New IronTesseract()

' Specify the image path and perform OCR on the image
Using ocrInput As New OcrInput("images\image.png")
	Dim ocrResult = ocrTesseract.Read(ocrInput)

	' Print the extracted text to the console
	Console.WriteLine(ocrResult.Text)
End Using
$vbLabelText   $csharpLabel

這段 C# 程式碼示範了 IronOCR 的用法,IronOCR 是一個用於光學字元辨識 (OCR) 的函式庫。 以下是詳細步驟說明:

1.導入庫

```csharp
using IronOcr;
using System;
```

程式碼首先導入必要的函式庫,包括提供 OCR 功能的**IronOcr**和用於通用功能的**System**命名空間。

2.初始化 IronTesseract 並載入鏡像

```csharp
var ocrTesseract = new IronTesseract();
```

這行程式碼創建了**IronTesseract**的一個實例,它是 IronOCR 提供的 OCR 引擎。

```csharp
using (var ocrInput = new OcrInput(@"images\image.png"))
```

使用要處理的影像路徑實例化**OcrInput**物件。 在這種情況下,圖像檔案是"images"目錄中的"image.png"。

3.執行 OCR 和提取文字

```csharp
var ocrResult = ocrTesseract.Read(ocrInput);
```

這一行呼叫**IronTesseract**實例的**Read**方法,並將**OcrInput**物件作為參數傳入。 該方法對提供的圖像執行 OCR 並提取文字。

4.顯示提取的文字

```csharp
Console.WriteLine(ocrResult.Text);
```

最後,使用**Console.WriteLine**將提取的文字列印到控制台,顯示從圖像中獲得的 OCR 結果。

這段程式碼片段使用 IronOCR 對指定圖像進行 OCR 文字識別,並將提取的文字輸出到控制台。

輸入影像

Invoice

輸出

客戶發票輸出

對影像上的指定區域執行OCR辨識。

您也可以使用 IronOCR 對影像上的特定區域執行 OCR 操作。 以下是一個程式碼範例:

using IronOcr;
using IronSoftware.Drawing;
using System;

// Create a new instance of the IronTesseract class
var ocrTesseract = new IronTesseract();

// Specify the region on the image to be processed
using (var ocrInput = new OcrInput())
{
    var ContentArea = new CropRectangle(x: 20, y: 20, width: 400, height: 50);

    // Add the image with the defined content area
    ocrInput.AddImage("r3.png", ContentArea);

    // Perform OCR on the specified region and extract text
    var ocrResult = ocrTesseract.Read(ocrInput);

    // Print the extracted text to the console
    Console.WriteLine(ocrResult.Text);
}
using IronOcr;
using IronSoftware.Drawing;
using System;

// Create a new instance of the IronTesseract class
var ocrTesseract = new IronTesseract();

// Specify the region on the image to be processed
using (var ocrInput = new OcrInput())
{
    var ContentArea = new CropRectangle(x: 20, y: 20, width: 400, height: 50);

    // Add the image with the defined content area
    ocrInput.AddImage("r3.png", ContentArea);

    // Perform OCR on the specified region and extract text
    var ocrResult = ocrTesseract.Read(ocrInput);

    // Print the extracted text to the console
    Console.WriteLine(ocrResult.Text);
}
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

' Create a new instance of the IronTesseract class
Private ocrTesseract = New IronTesseract()

' Specify the region on the image to be processed
Using ocrInput As New OcrInput()
	Dim ContentArea = New CropRectangle(x:= 20, y:= 20, width:= 400, height:= 50)

	' Add the image with the defined content area
	ocrInput.AddImage("r3.png", ContentArea)

	' Perform OCR on the specified region and extract text
	Dim ocrResult = ocrTesseract.Read(ocrInput)

	' Print the extracted text to the console
	Console.WriteLine(ocrResult.Text)
End Using
$vbLabelText   $csharpLabel

這段 C# 程式碼使用了 IronOCR 函式庫進行光學字元辨識 (OCR)。 它首先導入必要的庫,包括 IronOCR 和 System。 建立了一個 IronTesseract 實例,即 OCR 引擎。 程式碼使用 CropRectangle 設定影像中要處理的特定 ContentArea,從而聚焦於定義的區域。 然後,將指定區域內的影像("r3.png")加入 OCR 處理中。 OCR引擎讀取指定的內容區域,提取文本,並將結果文本使用Console.WriteLine列印到控制台。

輸出

Output

結論

透過機器學習從圖像中提取文本,特別是利用 IronOCR 等光學字元辨識 (OCR) 庫,標誌著電腦視覺和自然語言處理交叉領域的一次變革性進步。 OCR 技術和深度學習技術在將視覺文字高效轉換為可編輯和可搜尋的資料方面都發揮著關鍵作用,為文件數位化、內容索引和輔助功能增強等重要用途服務。

IronOCR 作為一個傑出的 OCR 庫,充分展現了這種融合的潛力,它能夠精確地將掃描圖像和 PDF 文件轉換為多種語言和字體樣式的數位可編輯內容。 它與 C# 等程式語言的無縫集成,使得實現更加簡化,進一步增強了從圖像中提取文字在眾多應用和領域中的變革性影響。

要了解有關IronOCR及其所有相關功能的更多信息,訪問此連結。 關於如何從圖像中提取文字的完整教程,請點擊以下連結查看。 您可以透過此連結購買 IronOCR 許可證。