How to Get Read Confidence

Read confidence in OCR (Optical Character Recognition) refers to the level of certainty or reliability that the OCR system assigns to the accuracy of the text it has recognized in an image or document. It is a measure of how confident the OCR system is that the recognized text is correct.

A high confidence score indicates a high degree of certainty that the recognition is accurate, while a low confidence score suggests that the recognition may be less reliable.

Get Started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer



Get Read Confidence Example

After performing OCR on the input image, the confidence level of the text is stored in the Confidence property. Utilize the 'using' statement to automatically dispose of objects after use. Add documents such as images and PDFs with the OcrImageInput and OcrPdfInput classes, respectively. The Read method will return an OcrResult object that allows access to the Confidence property.

:path=/static-assets/ocr/content-code-examples/how-to/tesseract-result-confidence-get-confidence.cs
using IronOcr;

// This code performs Optical Character Recognition (OCR) using the IronOcr library.
// It reads text from an image file and retrieves the recognized text's confidence level.

// Instantiate IronTesseract, the OCR engine
var ocrTesseract = new IronTesseract();

// Load an image from a file to use as input for OCR
// Using 'using var' ensures proper disposal of resources, as OcrInput implements IDisposable
using var imageInput = new OcrInput("sample.tiff");

// Perform OCR on the image input
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve the confidence level of the OCR process
double confidence = ocrResult.Confidence;

// Output the result and confidence to console for verification and debugging purposes
Console.WriteLine("OCR Result: " + ocrResult.Text);
Console.WriteLine("Confidence Level: " + confidence);
Imports IronOcr

' This code performs Optical Character Recognition (OCR) using the IronOcr library.
' It reads text from an image file and retrieves the recognized text's confidence level.

' Instantiate IronTesseract, the OCR engine
Private ocrTesseract = New IronTesseract()

' Load an image from a file to use as input for OCR
' Using 'using var' ensures proper disposal of resources, as OcrInput implements IDisposable
Private imageInput = New OcrInput("sample.tiff")

' Perform OCR on the image input
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Retrieve the confidence level of the OCR process
Private confidence As Double = ocrResult.Confidence

' Output the result and confidence to console for verification and debugging purposes
Console.WriteLine("OCR Result: " & ocrResult.Text)
Console.WriteLine("Confidence Level: " & confidence)
$vbLabelText   $csharpLabel

Get Read Confidences at Different Levels

Not only can you retrieve the confidence level of the entire document, but you can also access the confidence levels of each page, paragraph, line, word, and character. Furthermore, you can obtain the confidence of a block, which represents a collection of one or more paragraphs located closely together.

:path=/static-assets/ocr/content-code-examples/how-to/tesseract-result-confidence-confidence-level.cs
// This C# code snippet extracts confidence levels from various elements of an OCR result object.
// It checks if each collection within the OCR result is non-empty before trying to access its elements,
// thus preventing potential runtime exceptions like IndexOutOfRangeException.
// Each section is guarded by a conditional check that ensures safety when accessing elements.

if (ocrResult.Pages != null && ocrResult.Pages.Count > 0)
{
    // Get page confidence level from the first page
    double pageConfidence = ocrResult.Pages[0].Confidence;
    // Additional processing with pageConfidence can be done here
}

if (ocrResult.Paragraphs != null && ocrResult.Paragraphs.Count > 0)
{
    // Get paragraph confidence level from the first paragraph
    double paragraphConfidence = ocrResult.Paragraphs[0].Confidence;
    // Additional processing with paragraphConfidence can be done here
}

if (ocrResult.Lines != null && ocrResult.Lines.Count > 0)
{
    // Get line confidence level from the first line
    double lineConfidence = ocrResult.Lines[0].Confidence;
    // Additional processing with lineConfidence can be done here
}

if (ocrResult.Words != null && ocrResult.Words.Count > 0)
{
    // Get word confidence level from the first word
    double wordConfidence = ocrResult.Words[0].Confidence;
    // Additional processing with wordConfidence can be done here
}

if (ocrResult.Characters != null && ocrResult.Characters.Count > 0)
{
    // Get character confidence level from the first character
    double characterConfidence = ocrResult.Characters[0].Confidence;
    // Additional processing with characterConfidence can be done here
}

if (ocrResult.Blocks != null && ocrResult.Blocks.Count > 0)
{
    // Get block confidence level from the first block
    double blockConfidence = ocrResult.Blocks[0].Confidence;
    // Additional processing with blockConfidence can be done here
}
' This C# code snippet extracts confidence levels from various elements of an OCR result object.
' It checks if each collection within the OCR result is non-empty before trying to access its elements,
' thus preventing potential runtime exceptions like IndexOutOfRangeException.
' Each section is guarded by a conditional check that ensures safety when accessing elements.

If ocrResult.Pages IsNot Nothing AndAlso ocrResult.Pages.Count > 0 Then
	' Get page confidence level from the first page
	Dim pageConfidence As Double = ocrResult.Pages(0).Confidence
	' Additional processing with pageConfidence can be done here
End If

If ocrResult.Paragraphs IsNot Nothing AndAlso ocrResult.Paragraphs.Count > 0 Then
	' Get paragraph confidence level from the first paragraph
	Dim paragraphConfidence As Double = ocrResult.Paragraphs(0).Confidence
	' Additional processing with paragraphConfidence can be done here
End If

If ocrResult.Lines IsNot Nothing AndAlso ocrResult.Lines.Count > 0 Then
	' Get line confidence level from the first line
	Dim lineConfidence As Double = ocrResult.Lines(0).Confidence
	' Additional processing with lineConfidence can be done here
End If

If ocrResult.Words IsNot Nothing AndAlso ocrResult.Words.Count > 0 Then
	' Get word confidence level from the first word
	Dim wordConfidence As Double = ocrResult.Words(0).Confidence
	' Additional processing with wordConfidence can be done here
End If

If ocrResult.Characters IsNot Nothing AndAlso ocrResult.Characters.Count > 0 Then
	' Get character confidence level from the first character
	Dim characterConfidence As Double = ocrResult.Characters(0).Confidence
	' Additional processing with characterConfidence can be done here
End If

If ocrResult.Blocks IsNot Nothing AndAlso ocrResult.Blocks.Count > 0 Then
	' Get block confidence level from the first block
	Dim blockConfidence As Double = ocrResult.Blocks(0).Confidence
	' Additional processing with blockConfidence can be done here
End If
$vbLabelText   $csharpLabel

Get Character Choices

Apart from the confidence level, there is another interesting property called Choices. Choices contain a list of alternative word choices and their statistical relevance. This information allows the user to access other possible characters.

:path=/static-assets/ocr/content-code-examples/how-to/tesseract-result-confidence-get-choices.cs
using IronOcr; // Importing the IronOcr namespace to use OCR functionalities.
using static IronOcr.OcrResult; // Importing static members of the OcrResult class for ease of access.

try
{
    // Instantiate IronTesseract
    var ocrTesseract = new IronTesseract();

    // Add image using a using statement, ensuring the resource is properly disposed
    using var imageInput = new OcrInput("Potter.tiff");

    // Perform OCR and get the result
    var ocrResult = ocrTesseract.Read(imageInput);

    // Ensure there are characters in the resulting OCR to avoid runtime errors
    if (ocrResult.Text.Length > 0)
    {
        // Get choices from the first character of the result
        var choices = ocrResult.Characters[0].Choices;

        // Output choice information for debugging purposes
        foreach (var choice in choices)
        {
            Console.WriteLine($"Text: {choice.Text}, Confidence: {choice.Confidence}");
        }
    }
    else
    {
        Console.WriteLine("No characters were detected in the image.");
    }
}
catch (Exception ex)
{
    // Catch any errors, such as file not found or OCR failure, and print them
    Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports IronOcr ' Importing the IronOcr namespace to use OCR functionalities.
Imports IronOcr.OcrResult ' Importing static members of the OcrResult class for ease of access.

Try
	' Instantiate IronTesseract
	Dim ocrTesseract = New IronTesseract()

	' Add image using a using statement, ensuring the resource is properly disposed
	Dim imageInput = New OcrInput("Potter.tiff")

	' Perform OCR and get the result
	Dim ocrResult = ocrTesseract.Read(imageInput)

	' Ensure there are characters in the resulting OCR to avoid runtime errors
	If ocrResult.Text.Length > 0 Then
		' Get choices from the first character of the result
		Dim choices = ocrResult.Characters(0).Choices

		' Output choice information for debugging purposes
		For Each choice In choices
			Console.WriteLine($"Text: {choice.Text}, Confidence: {choice.Confidence}")
		Next choice
	Else
		Console.WriteLine("No characters were detected in the image.")
	End If
Catch ex As Exception
	' Catch any errors, such as file not found or OCR failure, and print them
	Console.WriteLine($"An error occurred: {ex.Message}")
End Try
$vbLabelText   $csharpLabel

Retrieved Information

Choices

Frequently Asked Questions

What is read confidence in OCR?

Read confidence in OCR refers to the level of certainty or reliability that the OCR system assigns to the accuracy of the text it has recognized in an image or document.

How can I get started with IronOCR to assess read confidence?

To get started with IronOCR, you need to download the library from NuGet, prepare the targeted image and PDF document, and access the Confidence property of the OCR result.

How do I access the confidence level of OCR-recognized text using IronOCR?

After performing OCR with IronOCR, the confidence level of the text is stored in the Confidence property of the OcrResult object. This can be accessed using the Read method.

Can I retrieve confidence levels for different text elements?

Yes, with IronOCR, you can retrieve the confidence levels of pages, paragraphs, lines, words, and characters individually.

What are character choices in IronOCR?

Character choices provide a list of alternative word choices and their statistical relevance, allowing access to other possible characters recognized by the OCR.

How does the Confidence property in IronOCR work?

The Confidence property indicates the accuracy level of the recognized text. It provides a numerical score representing the certainty of the OCR system regarding the recognition.

Is it possible to access confidence levels at the block level in IronOCR?

Yes, IronOCR allows you to access the confidence level of a block, which represents a collection of one or more paragraphs located closely together.

What is the purpose of the Choices property in IronOCR?

The Choices property is used to provide alternative word choices and their confidence scores, offering users additional insights into possible text interpretations.

How do I implement OCR with confidence levels using C#?

Implement OCR with confidence levels in C# by using IronOCR's library. Set the OCR engine mode, prepare the input, and access the Confidence property through the OcrResult object.

What steps are involved in assessing read confidence with IronOCR?

The steps include downloading the IronOCR library, preparing documents, using the Read method to obtain an OcrResult, and accessing the Confidence property for accuracy assessment.

Chaknith related to Retrieved Information
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.