進階閱讀的 OCR 設定
IronOCR 提供超越標準 OCR 的進階掃描讀取方法,例如 ReadLicensePlate 及 ReadPhoto。 這些方法由 IronOcr.Extensions.AdvancedScan 套件提供支援。 為了微調這些方法處理文字的方式,IronOCR 提供了 TesseractConfiguration 類別,讓開發人員能完全掌控字元白名單、黑名單、BarCode 偵測、資料表讀取等功能。
本文涵蓋 TesseractConfiguration 屬性,供進階閱讀參考,並提供實際案例,說明如何在真實情境中設定 OCR。
快速入門:將 OCR 輸出限制在字元白名單內
在呼叫 Read 之前,請先在 TesseractConfiguration 上設定 WhiteListCharacters。 任何未列入白名單的字元都會從結果中靜默刪除,無需後處理即可消除雜訊。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronOcr
PM > Install-Package IronOcr -
請複製並執行此程式碼片段。
var result = new IronTesseract() { Configuration = new TesseractConfiguration { WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- " } }.Read(new OcrInput("image.png")); Console.WriteLine(result.Text); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronOCR
如何設定 OCR 以進行進階閱讀
- 從 NuGet 安裝 IronOCR
- 安裝 IronOcr.Extensions.AdvancedScan 套件
- 設定
TesseractConfiguration屬性,例如白名單字元以及ReadBarCodes - 載入輸入圖片
OcrInput - 請使用進階方法讀取圖片,例如
ReadPhoto,ReadLicensePlate,或ReadPassport
TesseractConfiguration 屬性
TesseractConfiguration 類別提供以下屬性,用於自訂 OCR 行為。 這些設定透過 IronTesseract.Co/nfiguration 進行。
| 屬性 | 類型 | 描述 |
|---|---|---|
白名單字元 |
string | OCR 輸出僅會識別此字串中出現的字元。所有其他字元均不包含在內。 |
BlackListCharacters |
string | 此字串中的字元將被主動忽略並從 OCR 輸出中移除。 |
ReadBarCodes |
bool | 在 OCR 處理過程中,啟用或停用文件內的 BarCode 偵測功能。 |
ReadDataTables |
bool | 啟用或停用文件中透過 Tesseract 進行的表格結構偵測功能。 |
PageSegmentationMode |
TesseractPageSegmentationMode | 決定 Tesseract 如何分割輸入影像。選項包括 AutoOsd, Auto, SingleBlock, SingleLine, SingleWord、以及更多選項。 |
RenderSearchablePdf |
bool | 啟用此功能後,OCR 輸出可儲存為帶有隱形文字層的可搜尋 PDF 檔案。 |
RenderHocr |
bool | 啟用此功能後,OCR 輸出將包含 hOCR 資料,以便進行後續處理或匯出。 |
TesseractVariables |
Dictionary<string, object> | 提供直接存取 Tesseract 低階設定變數的功能,以實現精細控制。 |
TesseractVariables 字典更進一步,提供了數百個底層 Tesseract 引擎參數,以應對高階屬性不足的情況。
以下範例將逐一展示各屬性群組,首先從字元白名單開始說明。
為車牌號碼設定字元白名單
WhiteListCharacters 的常見應用情境是將 OCR 輸出限制為僅包含車牌上可能出現的字元:大寫字母、數字、連字號及空格。 透過指示引擎忽略預期字元集以外的內容,可消除雜訊並提升準確性。
輸入
以下車輛登記記錄包含大寫文字、小寫文字及特殊符號(#、|, *),以及標點符號。
BlackListCharacters 透過主動排除已知的干擾符號(如 , and*`。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading.cs
using IronOcr;
// Initialize the Tesseract OCR engine
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
// Whitelist only characters that appear on license plates
WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",
// Blacklist common noise characters
BlackListCharacters = "`~@#$%&*",
};
var ocrInput = new OcrInput();
// Load the input image
ocrInput.LoadImage("advanced-input.png");
// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);
// Print the filtered text result to the console
Console.WriteLine(results.Text);
Imports IronOcr
' Initialize the Tesseract OCR engine
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
' Whitelist only characters that appear on license plates
.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",
' Blacklist common noise characters
.BlackListCharacters = "`~@#$%&*"
}
Dim ocrInput As New OcrInput()
' Load the input image
ocrInput.LoadImage("advanced-input.png")
' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)
' Print the filtered text result to the console
Console.WriteLine(results.Text)
輸出
白名單過濾功能在結果中清晰可見:
- "Plate: ABC-1234"應譯為"P ABC-1234"。 小寫單字"late:"已省略,而車牌號碼則完全保留。
- "VIN: 1HGBH41JXMN109186" 應譯為 "VIN 1HGBH41JXMN109186"。 省略了冒號,但保留了大寫的 VIN 以及完整的數字。
- "所有者:john.doe@email.com"應譯為"O"。 已移除全小寫的電子郵件地址及所有標點符號。
- "地區:CA-90210 | "Zone #5"應譯為"R CA-90210 Z 5"。 縱橫線 (
|) and hash (#) 將被移除,而大寫字母與數字則予以保留。 - "費用:$125.00 + 稅*"應譯為"F 12500"。 美元符號、小數點、加號以及小寫的"tax"均已移除。
- "Ref: ~record_v2^final" 應譯為 "R 2"。 波浪線 (
~)、底線、插入符 (^) 以及所有小寫字元均已移除。
WhiteListCharacters 和 BlackListCharacters 的處理方式適用於任何文件類型,不僅限於車牌。 下一節將說明如何擴展讀取功能,以在同一次處理中偵測BARCODE與表格結構。
配置BarCode與資料表讀取
IronOCR 除了能識別文件中的文字外,還能偵測 BARCODE 及結構化表格。 這些功能是透過 TesseractConfiguration 進行控制的:
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
// Enable barcode detection within documents
ReadBarCodes = true,
// Enable table structure detection
ReadDataTables = true,
};
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
// Enable barcode detection within documents
ReadBarCodes = true,
// Enable table structure detection
ReadDataTables = true,
};
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.ReadBarCodes = True,
.ReadDataTables = True
}
- ReadBarCodes:當設定為
true時,IronOCR 除了文字外,還會掃描文件中的 BARCODE。 若預期不會出現 BARCODE,請設定為false以跳過 BARCODE 偵測並加快處理速度。 - ReadDataTables:當設定為
true時,Tesseract 會嘗試偵測並保留文件中的表格結構。 此功能適用於發票、報告及其他表格類文件。
這些選項可與 WhiteListCharacters 及 BlackListCharacters 結合使用,以精確控制從複雜文件中擷取的內容。
雖然篩選與偵測機制決定了擷取的內容,但版面配置的解析則是另一項考量。 下一節將說明如何根據文件類型選擇合適的 PageSegmentationMode。
控制頁面分割模式
PageSegmentationMode 會告訴 Tesseract 在進行辨識前該如何分割輸入的影像。 若針對特定版面配置選擇了錯誤的模式,將導致引擎誤讀或完全跳過文字。
| 模式 | 使用案例 |
|---|---|
AutoOsd |
具備方向與字體識別功能的自動版面分析 |
Auto |
不使用 OSD 的自動版面分析(預設) |
SingleColumn |
假設圖片為單欄文字 |
SingleBlock |
假設圖片為單一且格式統一的文字區塊 |
SingleLine |
假設圖片僅為單行文字 |
SparseText |
盡可能多地找出文字,且不限順序 |
對於僅含單一行文字的標籤或橫幅,SingleLine 可避免多區塊分析,從而同時提升速度與準確性。
輸入
single-line-label.png 是一張窄版運送標籤,上面僅有一行粗體 Courier 字體文字:SHIPPING LABEL: TRK-2024-XR9-001。
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};
using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};
using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine
}
Using input As New OcrInput()
input.LoadImage("single-line-label.png")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End Using
對於文字排版不規則的掃描頁面,SparseText 能比 Auto 恢復更多內容。
輸入
receipt-scan.png 是一張 Corner Market 的熱感收據,包含四項明細(咖啡、瑪芬、果汁、燕麥棒)、虛線分隔符、小計、稅金及總計。 此類版面配置下,固定區塊分割法會導致不同水平位置的內容遺漏。
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};
using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};
using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.PageSegmentationMode = TesseractPageSegmentationMode.SparseText
}
Using input As New OcrInput()
input.LoadImage("receipt-scan.png")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End Using
在根據文件類型調整版面分割後,下一步是控制輸出格式以利後續處理。
產生可搜尋的 PDF 檔案與 hOCR 輸出
RenderSearchablePdf 和 RenderHocr 用於控制 IronOCR 除了純文字結果外所產出的輸出格式。
RenderSearchablePdf 會在原始圖像上疊加一層不可見的文字層,生成一份 PDF 檔案,使用者可在其中搜尋和複製文字,同時掃描的圖像仍保持可見。 這是文件歸檔工作流程的標準輸出格式。
輸入
scanned-document.pdf 是一封由 IronOCR Solutions Ltd. 發出的單頁商業信函(日期為 2024 年 3 月 15 日,參考編號 DOC-2024-OCR-0315)。 翻譯結果將儲存為 searchable-output.pdf。
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
RenderSearchablePdf = true,
};
using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
RenderSearchablePdf = true,
};
using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
Imports IronTesseract
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.RenderSearchablePdf = True
}
Using input As New OcrInput()
input.LoadPdf("scanned-document.pdf")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable-output.pdf")
End Using
輸出
輸出結果為一份 PDF 檔案,其外觀與原始檔案完全相同,但內含一個隱藏的文字層。 開啟 searchable-output.pdf 並使用 Ctrl+F 確認內嵌文字可搜尋且可複製。
RenderHocr 會產生一份 hOCR 文件,這是一個 HTML 檔案,其中包含文字內容以及每個 WORD 的邊界框座標。 當下游工具需要精確的文字定位時,此功能便十分實用,例如內容遮蔽引擎或文件版面配置分析。
輸入
document-page.png 是一份標題為"2024 年第一季季度摘要"的文件頁面,內容包含兩段財務數據,涵蓋營收、營運成本及成長動能。 翻譯結果將儲存為 output.html。
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
RenderHocr = true,
};
using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
IronTesseract ocr = new IronTesseract();
ocr.Co/nfiguration = new TesseractConfiguration
{
RenderHocr = true,
};
using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
Imports IronTesseract
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.RenderHocr = True
}
Using input As New OcrInput()
input.LoadImage("document-page.png")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsHocrFile("output.html")
End Using
輸出
output.html 會將每個識別出的 WORD 與其邊界框座標進行編碼。 請在瀏覽器中開啟檔案以檢視 hOCR 結構,或將其傳遞給下游工具進行版面分析或遮蔽處理。
若您需要透過單次讀取呼叫同時取得三種輸出格式(純文字、可搜尋 PDF 及 hOCR),可同時啟用這兩項選項。
這些輸出標記的運作與閱讀語言無關,包括非拉丁文字系統。下一節將說明如何對日文文本套用字元過濾。
國際化文件的 Unicode 字元過濾
針對中文、日文或韓文的國際化文件,WhiteListCharacters 和 BlackListCharacters 屬性可支援 Unicode 字元。 這讓您可以將輸出限制在特定字元集,例如日語僅限平假名與片假名。
輸入
該文件包含一個標題(テスト)、一則混合平假名與片假名並帶有濁音標記變體(プ、で)的日文句子、一筆價格行(內含被列入黑名單的干擾符號 ★、 ■)及漢字(価格),以及一則備註行,其中包含另一個被列入黑名單的符號(§)、更多漢字(購入)、額外的有聲標記變體(プ, デ)以及基礎片假名(メモ, ール)。 白名單僅允許基礎平假名、基礎片假名、數字及常見日文標點符號通過; 這三個噪音符號已被明確列入黑名單。
平假名與片假名的 Unicode 字元範圍以字串常數形式傳遞至 WhiteListCharacters,雜訊符號則列於 BlackListCharacters。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-jp.cs
using IronOcr;
using System.IO;
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
// Whitelist only Hiragana, Katakana, numbers, and common Japanese punctuation
WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" +
"アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" +
"0123456789、。?!()¥ー",
// Blacklist common noise/symbols you want to ignore
BlackListCharacters = "★■§",
};
var ocrInput = new OcrInput();
// Load Japanese input image
ocrInput.LoadImage("jp.png");
// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);
// Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text);
// You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt");
Imports IronOcr
Imports System.IO
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" &
"アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" &
"0123456789、。?!()¥ー",
.BlackListCharacters = "★■§"
}
Dim ocrInput As New OcrInput()
' Load Japanese input image
ocrInput.LoadImage("jp.png")
' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)
' Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text)
' You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt")
輸出
完整的過濾後輸出內容已儲存為文字檔:jp-output.txt。
由於白名單僅包含基礎平假名與片假名字元,因此衍生出的帶聲標變體(如 プ (pu) 和 デ (de))將被省略。 此外,由於"価格"(價格)和"購入"(購買)等漢字不屬於白名單字元集,因此亦不包含在內。 無論白名單設定為何,系統都會主動移除 ■ 及 § 等黑名單符號。
接下來該去哪裡?
既然您已了解如何為進階閱讀情境設定 IronOCR,請進一步探索:
- 讀取特定文件類型,例如護照和車牌
- BarCode 與 QR 碼讀取作為獨立的 OCR 應用案例
- 從處理結果中匯出 hOCR 及可搜尋的 PDF 檔案
若用於正式生產環境,請記得取得授權以移除浮水印並使用完整功能。
常見問題
IronOCR 中的 TesseractConfiguration 是什麼?
IronOCR 中的 TesseractConfiguration 允許使用者自訂 OCR 設定,從而啟用進階讀取功能,例如字元白名單、BarCode 讀取及多語言支援。
如何在 IronOCR 中設定字元白名單?
在 IronOCR 中,您可以透過 TesseractConfiguration 設定字元白名單,藉此指定 OCR 引擎應識別的字元,此功能對於讀取車牌等任務特別有用。
IronOCR 能否讀取 BARCODE 和資料表?
是的,IronOCR 可透過調整 TesseractConfiguration 屬性中的特定設定,來讀取 BarCode 和資料表,以實現精確的 OCR 資料擷取。
IronOCR 是否支援中文、日文和韓文等國際語言?
IronOCR 透過其多語言 TesseractConfiguration 選項,支援包括中文、日文及韓文在內的國際語言。
在 IronOCR 中使用進階 OCR 設定有哪些好處?
透過 IronOCR 的進階 OCR 設定,可實現更精準且高效的文字辨識,並支援語言特定文字辨識及結構化資料擷取等專業任務。
是否可以針對特定的 OCR 任務來優化 IronOCR?
是的,IronOCR 可透過設定字元白名單、啟用 BARCODE 或表格辨識等功能,針對特定 OCR 任務進行優化,從而提升特定應用程式的執行效能。
如何在 IronOCR 中啟用多語言支援?
若要在 IronOCR 中啟用多語言支援,您可以調整 TesseractConfiguration 中的語言設定,讓 OCR 引擎能夠識別多種語言的文字。
何謂字元白名單?它們在 IronOCR 中如何使用?
IronOCR 中的字元白名單,是指 OCR 引擎被設定為可識別的特定字元清單,非常適合用於讀取數字或特定文字模式等針對性任務。
IronOCR 可用於讀取結構化資料格式嗎?
是的,IronOCR 可設定為讀取並處理結構化資料格式(例如 BARCODE 和表格),為各種資料擷取需求提供多功能的 OCR 功能。
IronOCR 提供哪些進階文字辨識設定選項?
IronOCR 提供字元白名單、多語言支援及 BARCODE 辨識等設定選項,以強化進階文字辨識功能,並針對特定需求進行客製化調整。

