白皮書

使用機器學習軟體從圖像中提取文字

TECHNOLOGY

利用機器學習從拍攝或掃描的圖像文件中提取文字,是電腦視覺與自然語言處理交匯處的一個新興領域。 這項技術運用先進的機器學習、物件識別演算法、進階圖形軟體、深網與暗網,以及神經網路架構,能精準識別並從圖像及掃描的紙本文件中擷取文字資訊。 透過運用各種機器學習技術,例如OCR與深度學習,它能夠將視覺場景中的文字偵測自動化且高效地轉換為可編輯且可搜尋的結構化資料,並進行物件偵測。

在這不斷演進的領域中,研究人員與實務工作者持續致力於提升準確性、速度與多功能性,使得從圖像、機器可讀資料及掃描文件中進行文字偵測與擷取,成為印刷文件數位化、內容索引、翻譯及無障礙功能強化等應用中的關鍵環節。

本文將探討如何使用 IronOcr(一款由強大的機器學習演算法及文字相關功能驅動的 OCR 程式庫)從圖片中擷取文字。 文字擷取(亦稱關鍵字擷取)基於機器學習技術,能自動掃描並從非結構化資料或公司的中央資料庫中,擷取相關或基礎的詞彙與短語。

如何利用機器學習從圖片中擷取文字?

  1. 下載用於從圖片中擷取文字的 C# 函式庫。
  2. 透過建立 OcrInput 物件來載入特定圖片,以進行場景文字辨識。
  3. 使用 ocrTesseract.Read 方法從圖片中擷取資料。
  4. 使用 Console.WriteLine 方法將擷取的文字 PRINT 至控制台。
  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(一款用於光學字元辨識 (OCR) 的程式庫)的使用方式。 以下是逐步說明:

  1. 匯入函式庫

    using IronOcr;
    using System;
    using IronOcr;
    using System;
    Imports IronOcr
    Imports System
    $vbLabelText   $csharpLabel

    程式碼首先導入必要的函式庫,包括提供 OCR 功能的 IronOCR,以及用於一般功能的 System 命名空間。

  2. 初始化 IronTesseract 並載入圖片

    var ocrTesseract = new IronTesseract();
    var ocrTesseract = new IronTesseract();
    Dim ocrTesseract As New IronTesseract()
    $vbLabelText   $csharpLabel

    此行代碼建立 IronTesseract 的實例,該引擎由 IronOCR 提供。

    using (var ocrInput = new OcrInput(@"images\image.png"))
    using (var ocrInput = new OcrInput(@"images\image.png"))
    Using ocrInput As New OcrInput("images\image.png")
    $vbLabelText   $csharpLabel

    OcrInput 物件會透過指定待處理影像的路徑來建立實例。 在此情況下,圖片檔案為"images"目錄中的"image.png"。

  3. 執行 OCR 並擷取文字

    var ocrResult = ocrTesseract.Read(ocrInput);
    var ocrResult = ocrTesseract.Read(ocrInput);
    Dim ocrResult = ocrTesseract.Read(ocrInput)
    $vbLabelText   $csharpLabel

    此行呼叫 IronTesseract 實例的 Read 方法,並傳入 OcrInput 物件。 此方法會對提供的圖片執行 OCR 並提取文字。

  4. 顯示擷取的文字

    Console.WriteLine(ocrResult.Text);
    Console.WriteLine(ocrResult.Text);
    Console.WriteLine(ocrResult.Text)
    $vbLabelText   $csharpLabel

    最後,透過 Console.WriteLine 將擷取的文字 PRINT 至控制台,顯示從圖片中取得的 OCR 結果。

此程式碼片段使用 IronOCR 對指定圖片中的文字進行 OCR 辨識,並將擷取的文字輸出至控制台。

輸入圖片

發票

輸出

客戶發票輸出

對圖片中的指定區域執行 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 將生成的文字 PRINT 至控制台。

輸出

輸出

結論

透過機器學習從圖像中提取文字——特別是運用 IronOCR 等光學字元辨識 (OCR) 程式庫——標誌著在電腦視覺與自然語言處理交匯處的一項變革性進展。 無論是 OCR 技術還是深度學習技術,都在將視覺化文字高效轉換為可編輯且可搜尋的資料方面發揮關鍵作用,並在文件數位化、內容索引及無障礙功能強化等重要領域中發揮關鍵作用。

IronOCR 作為一款知名的 OCR 程式庫,正是這種融合潛力的典範,在將掃描圖像和 PDF 檔案精準轉換為多語言、多種字型樣式的數位可編輯內容方面表現卓越。 其與 C# 等程式語言的無縫整合,使實作流程更為簡化,進一步強化了從圖像中擷取文字在眾多應用程式與領域中的變革性影響。

如欲進一步了解 IronOCR 及所有相關功能,請點擊此處連結。 有關從圖片中擷取文字的完整教學,請參閱以下連結。 IronOCR 授權可透過此連結購買。