Read Photo
This code example demonstrates how to use the IronTesseract OCR engine to extract text and analyze specific regions from a photo.
// Import necessary namespaces
using IronOcr;
using System;
class Program
{
static void Main(string[] args)
{
// Create an instance of the IronTesseract OCR engine
var Ocr = new IronTesseract();
// Initialize an OcrInput object
var Input = new OcrInput();
// Load the image frame from a TIFF file. The '0' indicates the first frame.
Input.AddImage("ocr.tiff", 0);
// Use the OCR engine to read the photo
var Result = Ocr.Read(Input);
// Identifying the first region of the recognized text
if (Result.Regions.Count > 0)
{
// Store the frame number of the region
int frameNumber = Result.Regions[0].FrameNumber;
// Retrieve the text found in the first region
string textInRegion = Result.Regions[0].Text;
// Store the coordinates of the text region (bounding box)
var region = Result.Regions[0].Region;
// Format the output into a readable string
string output = string.Format(
"Text in First Region: {0}\nCoordinates: X={1}, Y={2}, Width={3}, Height={4}\nConfidence: {5}%\nFull Text: {6}",
textInRegion,
region.X, region.Y, region.Width, region.Height,
Result.Confidence,
Result.Text
);
// Print the output to the console
Console.WriteLine(output);
}
else
{
Console.WriteLine("No text regions found in the image.");
}
}
}
// Import necessary namespaces
using IronOcr;
using System;
class Program
{
static void Main(string[] args)
{
// Create an instance of the IronTesseract OCR engine
var Ocr = new IronTesseract();
// Initialize an OcrInput object
var Input = new OcrInput();
// Load the image frame from a TIFF file. The '0' indicates the first frame.
Input.AddImage("ocr.tiff", 0);
// Use the OCR engine to read the photo
var Result = Ocr.Read(Input);
// Identifying the first region of the recognized text
if (Result.Regions.Count > 0)
{
// Store the frame number of the region
int frameNumber = Result.Regions[0].FrameNumber;
// Retrieve the text found in the first region
string textInRegion = Result.Regions[0].Text;
// Store the coordinates of the text region (bounding box)
var region = Result.Regions[0].Region;
// Format the output into a readable string
string output = string.Format(
"Text in First Region: {0}\nCoordinates: X={1}, Y={2}, Width={3}, Height={4}\nConfidence: {5}%\nFull Text: {6}",
textInRegion,
region.X, region.Y, region.Width, region.Height,
Result.Confidence,
Result.Text
);
// Print the output to the console
Console.WriteLine(output);
}
else
{
Console.WriteLine("No text regions found in the image.");
}
}
}
' Import necessary namespaces
Imports Microsoft.VisualBasic
Imports IronOcr
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create an instance of the IronTesseract OCR engine
Dim Ocr = New IronTesseract()
' Initialize an OcrInput object
Dim Input = New OcrInput()
' Load the image frame from a TIFF file. The '0' indicates the first frame.
Input.AddImage("ocr.tiff", 0)
' Use the OCR engine to read the photo
Dim Result = Ocr.Read(Input)
' Identifying the first region of the recognized text
If Result.Regions.Count > 0 Then
' Store the frame number of the region
Dim frameNumber As Integer = Result.Regions(0).FrameNumber
' Retrieve the text found in the first region
Dim textInRegion As String = Result.Regions(0).Text
' Store the coordinates of the text region (bounding box)
Dim region = Result.Regions(0).Region
' Format the output into a readable string
Dim output As String = String.Format("Text in First Region: {0}" & vbLf & "Coordinates: X={1}, Y={2}, Width={3}, Height={4}" & vbLf & "Confidence: {5}%" & vbLf & "Full Text: {6}", textInRegion, region.X, region.Y, region.Width, region.Height, Result.Confidence, Result.Text)
' Print the output to the console
Console.WriteLine(output)
Else
Console.WriteLine("No text regions found in the image.")
End If
End Sub
End Class
Explanation
Initialization: The IronTesseract OCR engine and
OcrInput
object are instantiated to facilitate OCR processing.Image Loading: An image frame ("ocr.tiff") is loaded into the
OcrInput
object using theAddImage
method, with0
specifying that the first frame of the image is processed.OCR Processing: The OCR engine processes the image using the
Read
method of the IronTesseract instance, returning anOcrResult
object, which contains OCR results including recognized text and regions.Region Analysis: The code checks if there are any recognized text regions. If so, it:
- Retrieves the frame number of the first text region.
- Extracts the text found in the first region.
- Obtains bounding box coordinates of the text region for further analysis (X, Y, Width, Height).
Output: The code formats a detailed output string with the text from the first recognized region, the region's bounding box details, and the overall OCR result, including a confidence score and full text.
- Console Output: The formatted string is printed to the console, offering a clear overview of the first recognized text region and the full OCR result.
This approach allows for detailed analysis of text regions within an image and is particularly useful for extracting information from documents or structured photos.