跳過到頁腳內容
OCR 工具

發票 OCR 機器學習(逐步教學)

在當今快節奏的商業環境中,任務和非結構化資料的自動化已成為提高效率和減少人為錯誤的關鍵策略。 其中一項任務是從發票或採購訂單中提取訊息,這個過程傳統上需要大量的人工投入。 然而,由於機器學習、深度學習模型和光學字元辨識 (OCR) 軟體技術的進步,企業現在可以使用 IronOCR 等工具簡化發票資訊擷取流程。 在本文中,我們將探討如何利用機器學習和 IronOCR 來徹底改變發票處理方式。

了解發票 OCR 工具

OCR 技術已經存在一段時間了,但隨著機器學習的出現,其在發票處理和資料提取方面的應用得到了顯著提升。 OCR(光學字元辨識)是一種將不同類型的文件(例如包含發票資訊的掃描紙本文件、PDF 文件、財務文件或數位相機拍攝的輸入影像)轉換為可編輯和可搜尋資料的技術。 它本質上是利用影像預處理技術將影像中的文字轉換為機器可讀文字。

IronOCR是一個功能強大的 OCR 庫,它基於機器學習演算法構建,可以整合到各種應用程式和程式語言中,使其成為發票處理的多功能工具。 透過使用 IronOCR,企業可以自動提取發票數據,如發票號碼、日期、供應商詳情和項目行,準確性顯著。

使用 IronOCR 進行發票 OCR 的優勢

使用IronOCR 進行發票處理具有許多優勢,可顯著提高貴公司財務營運(例如應付帳款)的效率和準確性。 讓我們更詳細地探討一下這些好處:

1. 準確度和減少誤差

IronOCR 利用先進的機器學習演算法,準確地識別和提取發票中的文字。 這樣可以最大限度地減少資料輸入過程中人為錯誤的發生機率,確保關鍵財務資訊得到正確記錄。

2. 節省時間和成本

使用 IronOCR 實現發票處理自動化,可大幅減少手動資料輸入所需的時間和資源。 透過優化員工時間並減少對體力勞動的需求,這可以大幅節省成本。

3. 效率提升

IronOCR能夠快速且有效率地處理大量發票。 這樣就省去了員工手動輸入每張發票資料的麻煩,使他們能夠專注於更具戰略意義的任務。

4. 可擴展性

IronOCR 具有可擴展性,能夠隨著您的業務擴展而處理不斷增長的發票數量。 您無需擔心工作量增加和邊界框過多導致發票文件處理系統不堪重負。

5. 全球影響力

IronOCR 支援 125 多種語言,使企業能夠處理來自世界各地供應商和客戶的發票。 無論發票是用何種語言書寫的,IronOCR 都能準確地提取資料。

6. 多格式支持

IronOCR 可以處理各種格式的發票,包括掃描影像、基於影像的 PDF 和基於文字的 PDF。 這種多功能性確保您可以輕鬆處理來自不同來源和格式的發票。

7. 客製化和資料提取

您可以自訂 IronOCR,從發票中提取特定資料字段,例如發票編號、日期、供應商詳細資料和行項目資訊。 這種程度的客製化使您可以根據您的特定業務需求來客製化解決方案。

8. 合規性和審計追踪

使用 IronOCR 進行自動發票處理有助於保持準確的記錄並提供審計追蹤。 這對於遵守財務法規和簡化審計流程至關重要。

9. 縮短發票處理週期

IronOCR 的精簡和自動化特性減少了處理發票所需的時間,從而縮短了發票處理週期。 這可以加快向供應商付款的速度,並改善雙方關係。

10. 增強型資料分析

透過將發票資料以結構化的數位格式存儲,您可以進行更深入的資料分析。 這有助於識別趨勢、優化支出並做出明智的財務決策。

實施 IronOCR 進行發票處理

若要實施 IronOCR 進行發票處理,請依照以下一般步驟操作:

步驟 1:建立一個新的 C

首先,在您首選的開發環境(例如 Visual Studio 或 Visual Studio Code)中建立一個新的 C# 專案或開啟一個現有專案。 本次示範我使用的是 Visual Studio 2022 IDE 和控制台應用程式。 您可以在任何專案類型中使用相同的實作,例如 ASP.NET Web API、ASP.NET MVC、ASP.NET Web Forms 或任何 .NET Framework。

發票 OCR 機器學習(逐步教學):圖 1 - C# 項目

步驟 2:透過 NuGet 套件管理器安裝 IronOCR

要在您的專案中使用 IronOCR,您需要安裝 IronOCR NuGet 套件。 以下是如何做到這一點:

1.開啟 NuGet 套件管理員控制台。 在 Visual Studio 中,您可以在"工具">"NuGet 套件管理器">"套件管理器控制台"下找到它。

[發票 OCR 機器學習(逐步教學):圖 2 - 軟體包管理器控制台](/static-assets/ocr/blog/invoice-ocr-machine-learning-tutorial/invoice-ocr-machine-learning-tutorial-2.webp)
  1. 執行以下命令安裝 IronOCR 軟體包:

    Install-Package IronOcr

    發票 OCR 機器學習(逐步教學):圖 3 - IronOCR 安裝

  2. 等待軟體包安裝完成。 完成後,您就可以在專案中使用 IronOCR 了。

步驟 3:在 C# 中實作 OCR

現在,讓我們編寫 C# 程式碼,使用 IronOCR 對發票執行 OCR 操作。 我們將使用以下發票樣本作為範例。

發票OCR機器學習(逐步教學):圖4 - 發票範本範例

以下範例程式碼將以發票圖像作為輸入,並從發票中提取數據,例如發票號碼、採購訂單等。

// Define the path to the invoice image
string invoicePath = @"D:\Invoices\SampleInvoice.png";

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Add the invoice image to the OCR input
    input.AddImage(invoicePath);

    // Perform OCR on the input image and store result
    OcrResult result = ocr.Read(input);

    // Output the extracted text from the image to the console
    Console.WriteLine(result.Text);
}
// Define the path to the invoice image
string invoicePath = @"D:\Invoices\SampleInvoice.png";

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Add the invoice image to the OCR input
    input.AddImage(invoicePath);

    // Perform OCR on the input image and store result
    OcrResult result = ocr.Read(input);

    // Output the extracted text from the image to the console
    Console.WriteLine(result.Text);
}
' Define the path to the invoice image
Dim invoicePath As String = "D:\Invoices\SampleInvoice.png"

' Create an instance of IronTesseract for OCR processing
Dim ocr As New IronTesseract()

' Use 'using' to ensure proper disposal of OcrInput resources
Using input As New OcrInput()
	' Add the invoice image to the OCR input
	input.AddImage(invoicePath)

	' Perform OCR on the input image and store result
	Dim result As OcrResult = ocr.Read(input)

	' Output the extracted text from the image to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

上面的程式碼是一個簡潔的 C# 範例,它使用 IronOCR 對單一發票圖像 (SampleInvoice.png) 執行 OCR,然後將提取的發票資料列印到控制台。 請務必將invoicePath變數替換為您的特定發票影像檔案的路徑。

發票OCR機器學習(逐步教學):圖5 - 發票OCR輸出

讓我們一次匯入多張發票,並提取其中的資料。 以下是我們用作輸入的發票目錄。

發票OCR機器學習(逐步教學):圖6 - 發票目錄

以下範例程式碼將一次性從多張發票中提取文字。

// Get all PNG files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Invoices\", "*.png");

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Loop through each file and add it to the OCR input
    foreach (string file in fileArray)
    {
        input.AddImage(file);
    }

    // Perform OCR on all the added images and store the result
    OcrResult result = ocr.Read(input);

    // Output the extracted text from all images to the console
    Console.WriteLine(result.Text);
}
// Get all PNG files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Invoices\", "*.png");

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Loop through each file and add it to the OCR input
    foreach (string file in fileArray)
    {
        input.AddImage(file);
    }

    // Perform OCR on all the added images and store the result
    OcrResult result = ocr.Read(input);

    // Output the extracted text from all images to the console
    Console.WriteLine(result.Text);
}
' Get all PNG files from the specified directory
Dim fileArray() As String = Directory.GetFiles("D:\Invoices\", "*.png")

' Create an instance of IronTesseract for OCR processing
Dim ocr As New IronTesseract()

' Use 'using' to ensure proper disposal of OcrInput resources
Using input As New OcrInput()
	' Loop through each file and add it to the OCR input
	For Each file As String In fileArray
		input.AddImage(file)
	Next file

	' Perform OCR on all the added images and store the result
	Dim result As OcrResult = ocr.Read(input)

	' Output the extracted text from all images to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

上面的程式碼將從資料夾中獲取所有 PNG 圖像,提取數據,然後將資料夾中所有發票的提取資料列印到控制台。

發票OCR機器學習(逐步教學):圖7 - 提取的數據

將提取的資料儲存為可搜尋的 PDF 發票

以下程式碼將讀取資料夾中的所有影像,執行資料提取,並將它們儲存為單一可搜尋的 PDF 發票。

// Get all PNG files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Invoices\", "*.png");

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Loop through each file and add it to the OCR input
    foreach (string file in fileArray)
    {
        input.AddImage(file);
    }

    // Perform OCR on all the added images and store the result
    OcrResult result = ocr.Read(input);

    // Save the result as a searchable PDF
    result.SaveAsSearchablePdf(@"D:\Invoices\Searchable.pdf");
}
// Get all PNG files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Invoices\", "*.png");

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Loop through each file and add it to the OCR input
    foreach (string file in fileArray)
    {
        input.AddImage(file);
    }

    // Perform OCR on all the added images and store the result
    OcrResult result = ocr.Read(input);

    // Save the result as a searchable PDF
    result.SaveAsSearchablePdf(@"D:\Invoices\Searchable.pdf");
}
' Get all PNG files from the specified directory
Dim fileArray() As String = Directory.GetFiles("D:\Invoices\", "*.png")

' Create an instance of IronTesseract for OCR processing
Dim ocr As New IronTesseract()

' Use 'using' to ensure proper disposal of OcrInput resources
Using input As New OcrInput()
	' Loop through each file and add it to the OCR input
	For Each file As String In fileArray
		input.AddImage(file)
	Next file

	' Perform OCR on all the added images and store the result
	Dim result As OcrResult = ocr.Read(input)

	' Save the result as a searchable PDF
	result.SaveAsSearchablePdf("D:\Invoices\Searchable.pdf")
End Using
$vbLabelText   $csharpLabel

所有範例中的程式碼幾乎都類似; 我們只是做了一些細微的改動,以示範不同的使用場景。 輸出的PDF檔案如下所示:

發票OCR機器學習(逐步教學):圖8 - PDF輸出

透過這種方式,IronPDF 提供了一種最簡單的自動化發票處理和文件處理的方法。

從PDF發票中提取發票數據

要使用 IronOCR 從 PDF 發票中提取數據,您可以按照與前面程式碼範例類似的方法進行操作。 IronOCR能夠處理基於圖像和基於文字的PDF文件。 以下是從PDF發票中提取資料的簡要範例:

// Get all PDF files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Invoices\", "*.pdf");

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Loop through each file and add it to the OCR input
    foreach (string file in fileArray)
    {
        input.AddPdf(file);
    }

    // Perform OCR on all the added PDFs and store the result
    OcrResult result = ocr.Read(input);

    // Output the extracted text from all PDFs to the console
    Console.WriteLine(result.Text);
}
// Get all PDF files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Invoices\", "*.pdf");

// Create an instance of IronTesseract for OCR processing
IronTesseract ocr = new IronTesseract();

// Use 'using' to ensure proper disposal of OcrInput resources
using (OcrInput input = new OcrInput())
{
    // Loop through each file and add it to the OCR input
    foreach (string file in fileArray)
    {
        input.AddPdf(file);
    }

    // Perform OCR on all the added PDFs and store the result
    OcrResult result = ocr.Read(input);

    // Output the extracted text from all PDFs to the console
    Console.WriteLine(result.Text);
}
' Get all PDF files from the specified directory
Dim fileArray() As String = Directory.GetFiles("D:\Invoices\", "*.pdf")

' Create an instance of IronTesseract for OCR processing
Dim ocr As New IronTesseract()

' Use 'using' to ensure proper disposal of OcrInput resources
Using input As New OcrInput()
	' Loop through each file and add it to the OCR input
	For Each file As String In fileArray
		input.AddPdf(file)
	Next file

	' Perform OCR on all the added PDFs and store the result
	Dim result As OcrResult = ocr.Read(input)

	' Output the extracted text from all PDFs to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

上述程式碼使用 IronOCR 有效率地批次處理位於目錄 (@"D:\Invoices\") 中的多個 PDF 發票。 它檢索文件路徑,將每個 PDF 新增為 OCR 處理,合併提取的文本,並將結果列印到控制台。 這種方法簡化了處理大量發票的組織的發票資料提取流程,提高了效率並減少了人工工作量。

發票OCR機器學習(逐步教學):圖9 - 提取輸出

結論

總而言之,機器學習與先進的 OCR 技術(如 IronOCR)的融合正在重塑發票的處理方式。 本文將帶您了解 IronOCR 的使用流程,並展現了其顯著優勢。 透過採用 IronOCR,企業可以提高準確率,節省時間和金錢,並輕鬆處理各種格式和語言的發票。 取消人工資料輸入不僅可以提高效率,還可以降低財務交易中出現代價高昂的錯誤的可能性。 IronOCR 簡化並改善了發票處理工作流程,對於希望在當今競爭激烈的環境中提升財務營運能力的企業來說,這是一個明智的選擇。 此外,IronOCR 還提供了一系列強大的功能,包括支援 125 多種語言、可自訂的資料擷取以及與基於圖像和基於文字的 PDF 的兼容性。

IronOCR 的功能集令人印象深刻,但同樣值得注意的是, IronOCR 的定價模式旨在滿足各種業務需求,為小型企業和大型公司提供免費試用,並提供靈活的選擇。 無論您是處理少量發票還是管理大量財務文件,IronOCR 都是一個可靠且經濟高效的解決方案。

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。