跳過到頁腳內容
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 APIs、ASP.NET MVC、ASP.NET Web Forms或任何.NET框架。

發票OCR機器學習(循序漸進),圖1 - C#項目

步驟2:通過NuGet Package Manager安裝IronOCR

要在項目中使用IronOCR,您需要安裝IronOCR NuGet包。 操作方法如下:

  1. 打開NuGet包管理控制台。 在Visual Studio中,您可以在“工具”>“NuGet包管理器”>“包管理控制台”下找到這個。

發票OCR機器學習(循序漸進),圖2 - 包管理控制台

  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的兼容性。

While IronOCR's feature set is impressive, it's also noteworthy that IronOCR's pricing model is designed to accommodate a wide range of business needs, offering flexible options with a free trial for both small enterprises and larger corporations. 無論是處理少量發票還是管理大量財務文件,IronOCR都是一個可靠且具有成本效益的解決方案。

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