如何運用IronOCR提升 C# OCR辨識置信度以增強圖片轉文字準確率
IronOCR 的讀取置信度表示 OCR辨識系統對識別文字準確性的確定程度,取值範圍為 0 到 100,分數越高表示圖片轉文字可靠性越高——可透過任何 OcrResult 物件上的 Confidence 屬性存取它。
OCR(光學字元辨識)的讀取置信度是指 OCR 系統對影像或文件中辨識出的文字的準確性所賦予的確定性或可靠性等級。 它是衡量 OCR 系統對識別文本正確性的信心程度的指標。 在處理掃描文件、照片或任何文字品質可能有所不同的影像時,此指標尤其重要。
置信度分數越高,表示辨識結果的準確度越有把握;而信賴度分數越低,表示辨識結果的可靠性可能較低。 了解這些置信度等級有助於開發人員在應用程式中實現適當的驗證邏輯和錯誤處理。
快速入門:一行程式碼即可掌握 OCR 讀取技巧
使用 IronTesseract 的 Read 方法和圖像檔案路徑,然後存取返回的 OcrResult 的 Confidence 屬性,以查看IronOCR對其文本識別的確定性如何。 這是一種簡單可靠的評估 OCR 輸出準確性的方法。
最簡工作流程(5個步驟)
- 下載 C# 庫以存取讀取置信度
- 準備目標圖像和 PDF 文檔
- Access the **`Confidence`** property of the OCR result
- 檢索頁面、段落、行、單字和字元的置信度
- Check the **`Choices`** property for alternative word choices
如何提升C#的閱讀理解能力?
對輸入影像執行 OCR 後,文字的置信度會儲存在Confidence屬性中。 使用"using"語句可以在使用後自動釋放物件。 分別使用 OcrImageInput 和 OcrPdfInput 類別新增影像和 PDF 等文件。 Read 方法將傳回一個OcrResult對象,允許存取Confidence屬性。
:path=/static-assets/ocr/content-code-examples/how-to/tesseract-result-confidence-get-confidence.cs
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.tiff");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Get confidence level
double confidence = ocrResult.Confidence;
Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.tiff")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Get confidence level
Private confidence As Double = ocrResult.Confidence
傳回的置信度值範圍為 0 到 100,其中:
- 90-100 :極佳的置信度 - 文字高度可靠
- 80-89 :置信度良好-文字整體準確,僅有少量不確定性
- 70-79 :中等置信度 - 文字可能包含一些錯誤 -低於 70 :低置信度 - 文字應進行審核或重新處理
如何在不同層面建立自信?
您不僅可以獲得整個文件的置信度,還可以存取每一頁、段落、行、單字和字元的置信度。 此外,您還可以獲得區塊的置信度,該區塊表示一個或多個緊密相鄰的段落的集合。
:path=/static-assets/ocr/content-code-examples/how-to/tesseract-result-confidence-confidence-level.cs
// Get page confidence level
double pageConfidence = ocrResult.Pages[0].Confidence;
// Get paragraph confidence level
double paragraphConfidence = ocrResult.Paragraphs[0].Confidence;
// Get line confidence level
double lineConfidence = ocrResult.Lines[0].Confidence;
// Get word confidence level
double wordConfidence = ocrResult.Words[0].Confidence;
// Get character confidence level
double characterConfidence = ocrResult.Characters[0].Confidence;
// Get block confidence level
double blockConfidence = ocrResult.Blocks[0].Confidence;
' Get page confidence level
Dim pageConfidence As Double = ocrResult.Pages(0).Confidence
' Get paragraph confidence level
Dim paragraphConfidence As Double = ocrResult.Paragraphs(0).Confidence
' Get line confidence level
Dim lineConfidence As Double = ocrResult.Lines(0).Confidence
' Get word confidence level
Dim wordConfidence As Double = ocrResult.Words(0).Confidence
' Get character confidence level
Dim characterConfidence As Double = ocrResult.Characters(0).Confidence
' Get block confidence level
Dim blockConfidence As Double = ocrResult.Blocks(0).Confidence
實際範例:按置信度篩選
處理品質參差不齊的文件(例如低品質掃描件)時,可以使用置信度評分來篩選結果:
using IronOcr;
using System.Linq;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Configure for better accuracy
ocrTesseract.Configuration.ReadBarCodes = false;
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;
// Add image
using var imageInput = new OcrImageInput("invoice.png");
// Apply filters to improve quality
imageInput.Deskew();
imageInput.DeNoise();
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Filter words with confidence above 85%
var highConfidenceWords = ocrResult.Words
.Where(word => word.Confidence >= 85)
.Select(word => word.Text)
.ToList();
// Process only high-confidence text
string reliableText = string.Join(" ", highConfidenceWords);
Console.WriteLine($"High confidence text: {reliableText}");
// Flag low-confidence words for manual review
var lowConfidenceWords = ocrResult.Words
.Where(word => word.Confidence < 85)
.Select(word => new { word.Text, word.Confidence })
.ToList();
foreach (var word in lowConfidenceWords)
{
Console.WriteLine($"Review needed: '{word.Text}' (Confidence: {word.Confidence:F2}%)");
}
using IronOcr;
using System.Linq;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Configure for better accuracy
ocrTesseract.Configuration.ReadBarCodes = false;
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;
// Add image
using var imageInput = new OcrImageInput("invoice.png");
// Apply filters to improve quality
imageInput.Deskew();
imageInput.DeNoise();
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Filter words with confidence above 85%
var highConfidenceWords = ocrResult.Words
.Where(word => word.Confidence >= 85)
.Select(word => word.Text)
.ToList();
// Process only high-confidence text
string reliableText = string.Join(" ", highConfidenceWords);
Console.WriteLine($"High confidence text: {reliableText}");
// Flag low-confidence words for manual review
var lowConfidenceWords = ocrResult.Words
.Where(word => word.Confidence < 85)
.Select(word => new { word.Text, word.Confidence })
.ToList();
foreach (var word in lowConfidenceWords)
{
Console.WriteLine($"Review needed: '{word.Text}' (Confidence: {word.Confidence:F2}%)");
}
Imports IronOcr
Imports System.Linq
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Configure for better accuracy
ocrTesseract.Configuration.ReadBarCodes = False
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd
' Add image
Using imageInput As New OcrImageInput("invoice.png")
' Apply filters to improve quality
imageInput.Deskew()
imageInput.DeNoise()
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Filter words with confidence above 85%
Dim highConfidenceWords = ocrResult.Words _
.Where(Function(word) word.Confidence >= 85) _
.Select(Function(word) word.Text) _
.ToList()
' Process only high-confidence text
Dim reliableText As String = String.Join(" ", highConfidenceWords)
Console.WriteLine($"High confidence text: {reliableText}")
' Flag low-confidence words for manual review
Dim lowConfidenceWords = ocrResult.Words _
.Where(Function(word) word.Confidence < 85) _
.Select(Function(word) New With {Key .Text = word.Text, Key .Confidence = word.Confidence}) _
.ToList()
For Each word In lowConfidenceWords
Console.WriteLine($"Review needed: '{word.Text}' (Confidence: {word.Confidence:F2}%)")
Next
End Using
OCR中的字元選項有哪些?
除了置信水準之外,還有另一個有趣的屬性叫做選擇。 選項中包含備選詞語清單及其統計相關性。 此資訊允許使用者存取其他可能的角色。 當使用多種語言或特殊字體時,此功能尤其有用。
:path=/static-assets/ocr/content-code-examples/how-to/tesseract-result-confidence-get-choices.cs
using IronOcr;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("Potter.tiff");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Get choices
Choice[] choices = ocrResult.Characters[0].Choices;
Imports IronOcr
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("Potter.tiff")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Get choices
Private choices() As Choice = ocrResult.Characters(0).Choices
不同的角色選擇有何幫助?
不同的角色選擇會帶來以下幾個好處:
1.歧義消除:當字元"O"和"0",或"l"和"1"混淆時
2.字體變體:風格化或裝飾字體的不同詮釋
3.品質問題:處理降級文字時可能出現多種問題
4.語言語境:基於語言規則的不同解釋
角色選擇
以下是一個全面的範例,示範如何透過角色選擇來提高準確率:
using IronOcr;
using System;
using System.Linq;
using static IronOcr.OcrResult;
// Configure IronTesseract for detailed results
IronTesseract ocrTesseract = new IronTesseract();
// Process image with potential ambiguities
using var imageInput = new OcrImageInput("ambiguous_text.png");
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Analyze character choices for each word
foreach (var word in ocrResult.Words)
{
Console.WriteLine($"\nWord: '{word.Text}' (Confidence: {word.Confidence:F2}%)");
// Check each character in the word
foreach (var character in word.Characters)
{
if (character.Choices != null && character.Choices.Length > 1)
{
Console.WriteLine($" Character '{character.Text}' has alternatives:");
// Display all choices sorted by confidence
foreach (var choice in character.Choices.OrderByDescending(c => c.Confidence))
{
Console.WriteLine($" - '{choice.Text}': {choice.Confidence:F2}%");
}
}
}
}
using IronOcr;
using System;
using System.Linq;
using static IronOcr.OcrResult;
// Configure IronTesseract for detailed results
IronTesseract ocrTesseract = new IronTesseract();
// Process image with potential ambiguities
using var imageInput = new OcrImageInput("ambiguous_text.png");
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Analyze character choices for each word
foreach (var word in ocrResult.Words)
{
Console.WriteLine($"\nWord: '{word.Text}' (Confidence: {word.Confidence:F2}%)");
// Check each character in the word
foreach (var character in word.Characters)
{
if (character.Choices != null && character.Choices.Length > 1)
{
Console.WriteLine($" Character '{character.Text}' has alternatives:");
// Display all choices sorted by confidence
foreach (var choice in character.Choices.OrderByDescending(c => c.Confidence))
{
Console.WriteLine($" - '{choice.Text}': {choice.Confidence:F2}%");
}
}
}
}
Imports IronOcr
Imports System
Imports System.Linq
Imports IronOcr.OcrResult
' Configure IronTesseract for detailed results
Dim ocrTesseract As New IronTesseract()
' Process image with potential ambiguities
Using imageInput As New OcrImageInput("ambiguous_text.png")
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Analyze character choices for each word
For Each word In ocrResult.Words
Console.WriteLine(vbCrLf & $"Word: '{word.Text}' (Confidence: {word.Confidence:F2}%)")
' Check each character in the word
For Each character In word.Characters
If character.Choices IsNot Nothing AndAlso character.Choices.Length > 1 Then
Console.WriteLine($" Character '{character.Text}' has alternatives:")
' Display all choices sorted by confidence
For Each choice In character.Choices.OrderByDescending(Function(c) c.Confidence)
Console.WriteLine($" - '{choice.Text}': {choice.Confidence:F2}%")
Next
End If
Next
Next
End Using
進階自信策略
在處理護照、車牌或MICR支票等特殊文件時,置信度評分對於驗證至關重要:
using IronOcr;
public class DocumentValidator
{
private readonly IronTesseract ocr = new IronTesseract();
public bool ValidatePassportNumber(string imagePath, double minConfidence = 95.0)
{
using var input = new OcrImageInput(imagePath);
// Configure for passport reading
ocr.Configuration.ReadBarCodes = true;
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine;
// Apply preprocessing
input.Deskew();
input.Scale(200); // Upscale for better accuracy
var result = ocr.Read(input);
// Find passport number pattern
var passportLine = result.Lines
.Where(line => line.Text.Contains("P<") || IsPassportNumberFormat(line.Text))
.FirstOrDefault();
if (passportLine != null)
{
Console.WriteLine($"Passport line found: {passportLine.Text}");
Console.WriteLine($"Confidence: {passportLine.Confidence:F2}%");
// Only accept if confidence meets threshold
return passportLine.Confidence >= minConfidence;
}
return false;
}
private bool IsPassportNumberFormat(string text)
{
// Simple passport number validation
return System.Text.RegularExpressions.Regex.IsMatch(text, @"^[A-Z]\d{7,9}$");
}
}
using IronOcr;
public class DocumentValidator
{
private readonly IronTesseract ocr = new IronTesseract();
public bool ValidatePassportNumber(string imagePath, double minConfidence = 95.0)
{
using var input = new OcrImageInput(imagePath);
// Configure for passport reading
ocr.Configuration.ReadBarCodes = true;
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine;
// Apply preprocessing
input.Deskew();
input.Scale(200); // Upscale for better accuracy
var result = ocr.Read(input);
// Find passport number pattern
var passportLine = result.Lines
.Where(line => line.Text.Contains("P<") || IsPassportNumberFormat(line.Text))
.FirstOrDefault();
if (passportLine != null)
{
Console.WriteLine($"Passport line found: {passportLine.Text}");
Console.WriteLine($"Confidence: {passportLine.Confidence:F2}%");
// Only accept if confidence meets threshold
return passportLine.Confidence >= minConfidence;
}
return false;
}
private bool IsPassportNumberFormat(string text)
{
// Simple passport number validation
return System.Text.RegularExpressions.Regex.IsMatch(text, @"^[A-Z]\d{7,9}$");
}
}
Imports IronOcr
Public Class DocumentValidator
Private ReadOnly ocr As New IronTesseract()
Public Function ValidatePassportNumber(imagePath As String, Optional minConfidence As Double = 95.0) As Boolean
Using input As New OcrImageInput(imagePath)
' Configure for passport reading
ocr.Configuration.ReadBarCodes = True
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine
' Apply preprocessing
input.Deskew()
input.Scale(200) ' Upscale for better accuracy
Dim result = ocr.Read(input)
' Find passport number pattern
Dim passportLine = result.Lines _
.Where(Function(line) line.Text.Contains("P<") OrElse IsPassportNumberFormat(line.Text)) _
.FirstOrDefault()
If passportLine IsNot Nothing Then
Console.WriteLine($"Passport line found: {passportLine.Text}")
Console.WriteLine($"Confidence: {passportLine.Confidence:F2}%")
' Only accept if confidence meets threshold
Return passportLine.Confidence >= minConfidence
End If
Return False
End Using
End Function
Private Function IsPassportNumberFormat(text As String) As Boolean
' Simple passport number validation
Return System.Text.RegularExpressions.Regex.IsMatch(text, "^[A-Z]\d{7,9}$")
End Function
End Class
優化以獲得更強的信心
為了獲得更高的置信度,可以考慮使用影像濾波器和預處理技術:
using IronOcr;
// Create an optimized OCR workflow
IronTesseract ocr = new IronTesseract();
using var input = new OcrImageInput("low_quality_scan.jpg");
// Apply multiple filters to improve confidence
input.Deskew(); // Correct rotation
input.DeNoise(); // Remove noise
input.Sharpen(); // Enhance edges
input.Dilate(); // Thicken text
input.Scale(150); // Upscale for clarity
// Configure for accuracy over speed
ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
ocr.Configuration.EngineMode = TesseractEngineMode.TesseractOnly;
var result = ocr.Read(input);
Console.WriteLine($"Document confidence: {result.Confidence:F2}%");
// Generate confidence report
var confidenceReport = result.Pages
.Select((page, index) => new
{
PageNumber = index + 1,
Confidence = page.Confidence,
WordCount = page.Words.Length,
LowConfidenceWords = page.Words.Count(w => w.Confidence < 80)
});
foreach (var page in confidenceReport)
{
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence:F2}% confidence");
Console.WriteLine($" Total words: {page.WordCount}");
Console.WriteLine($" Low confidence words: {page.LowConfidenceWords}");
}
using IronOcr;
// Create an optimized OCR workflow
IronTesseract ocr = new IronTesseract();
using var input = new OcrImageInput("low_quality_scan.jpg");
// Apply multiple filters to improve confidence
input.Deskew(); // Correct rotation
input.DeNoise(); // Remove noise
input.Sharpen(); // Enhance edges
input.Dilate(); // Thicken text
input.Scale(150); // Upscale for clarity
// Configure for accuracy over speed
ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
ocr.Configuration.EngineMode = TesseractEngineMode.TesseractOnly;
var result = ocr.Read(input);
Console.WriteLine($"Document confidence: {result.Confidence:F2}%");
// Generate confidence report
var confidenceReport = result.Pages
.Select((page, index) => new
{
PageNumber = index + 1,
Confidence = page.Confidence,
WordCount = page.Words.Length,
LowConfidenceWords = page.Words.Count(w => w.Confidence < 80)
});
foreach (var page in confidenceReport)
{
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence:F2}% confidence");
Console.WriteLine($" Total words: {page.WordCount}");
Console.WriteLine($" Low confidence words: {page.LowConfidenceWords}");
}
Imports IronOcr
' Create an optimized OCR workflow
Dim ocr As New IronTesseract()
Using input As New OcrImageInput("low_quality_scan.jpg")
' Apply multiple filters to improve confidence
input.Deskew() ' Correct rotation
input.DeNoise() ' Remove noise
input.Sharpen() ' Enhance edges
input.Dilate() ' Thicken text
input.Scale(150) ' Upscale for clarity
' Configure for accuracy over speed
ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5
ocr.Configuration.EngineMode = TesseractEngineMode.TesseractOnly
Dim result = ocr.Read(input)
Console.WriteLine($"Document confidence: {result.Confidence:F2}%")
' Generate confidence report
Dim confidenceReport = result.Pages _
.Select(Function(page, index) New With {
.PageNumber = index + 1,
.Confidence = page.Confidence,
.WordCount = page.Words.Length,
.LowConfidenceWords = page.Words.Count(Function(w) w.Confidence < 80)
})
For Each page In confidenceReport
Console.WriteLine($"Page {page.PageNumber}: {page.Confidence:F2}% confidence")
Console.WriteLine($" Total words: {page.WordCount}")
Console.WriteLine($" Low confidence words: {page.LowConfidenceWords}")
Next
End Using
概括
理解和利用 OCR 置信度評分對於建立強大的文件處理應用程式至關重要。 透過利用 IronOCR 的置信度屬性和字元選擇功能,開發人員可以在其 OCR 工作流程中實現智慧驗證、錯誤處理和品質保證機制。 無論您是處理螢幕截圖、表格還是專業文檔,置信度評分都能提供確保準確提取文字所需的指標。
常見問題解答
什麼是 OCR 信心,為什麼它很重要?
OCR 置信度是一個從 0 到 100 的度量,表示 OCR 系統對文字辨識準確性的肯定程度。IronOCR 透過任何 OcrResult 物件上的 Confidence 屬性提供此度量,協助開發人員評估辨識文字的可靠性,尤其是在處理掃描文件、照片或文字品質不一的影像時。
如何在 C# 中快速檢查 OCR 的信心?
使用 IronOCR,您只需要一行程式碼就可以得到 OCR 的信心值: double confidence = new IronOcr.IronTesseract().Read("input.png").Confidence; 這會回傳一個 0-100 之間的信心分數,表示 IronOCR 對於文字辨識的肯定程度。
不同的置信度分數範圍代表什麼意思?
IronOCR 信心分數表示:90-100 (優) 表示文字高度可靠;80-89 (良) 表示文字大致準確,但有輕微的不確定性;70-79 (中) 表示文字可能包含一些錯誤;低於 70 (低) 表示文字應該重新檢閱或處理。
如何存取不同文字元素的置信度?
IronOCR 可讓您擷取多重粒度的置信度 - 頁面、段落、行、字詞和個別字元。執行 OCR 之後,您可以透過 OcrResult 物件結構存取各層級的置信度屬性。
我可以得到有信心分數的替代詞建議嗎?
是的,IronOCR 提供了一個「選擇」(Choices)屬性,可提供其他的字詞選擇以及它們的置信度分數。當 OCR 引擎識別出同一文字的多種可能解釋時,此功能會有所幫助,讓您可以實作智慧型驗證邏輯。
如何在我的應用程式中實作信心驗證?
使用 IronOCR 的 Read 方法後,檢查 OcrResult 的 Confidence 屬性。根據置信度臨界值實施條件邏輯 - 例如,自動接受 90 分以上的結果,標記 70-90 分之間的結果以供審查,並重新處理或手動驗證 70 分以下的結果。

