How to Read License Plates using IronOCR
When managing a large volume of vehicle images, manually reading license plates is time-consuming and prone to human error. Automating this process with a tool like IronOCR provides a more efficient, accurate solution. IronOCR's ReadLicensePlate
method can programmatically extract license plate numbers from images, saving considerable time while improving data accuracy.
In this guide, we’ll demonstrate how to use IronOCR for license plate recognition, walking through examples and customizable configurations that make the process seamless. By leveraging these methods, developers can automate license plate reading, making tasks like parking management, toll collection, or security surveillance more efficient.
How to Read License Plates using IronOCR
- Download the C# library for reading license plates
- Import the license plate image for processing
- Ensure the document contains only the license plate image, without headers or footers
- Use the
ReadLicensePlate
method to extract data from the image - Access the OcrLicensePlateResult property to view and manipulate the extracted license data
Start using IronOCR in your project today with a free trial.
To use this function, you must also install the IronOcr.Extension.AdvancedScan
package.
Read License Plate Example
To read a license plate in IronOCR, we have to apply the following steps:
- We utilize the
ReadLicensePlate
method, which takes anOcrInput
as a parameter for the input. This method is more optimized for license plates precisely than the library's standardRead
counterpart. - Optionally, we can configure IronOCR to whitelist specific characters only that can exist in a license plate, to speed up the license plate number processing.
Please note
- The method currently only works for English, Chinese, Japanese, Korean, and Latin alphabet scripts.
- Using advanced scan on .NET Framework requires the project to run on x64 architecture.
License Plate
Code
:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-read-license-plate.cs
using IronOcr;
using System;
// Initialize the IronTesseract OCR object
var ocr = new IronTesseract();
// Whitelist characters that are typically found in license plates
ocr.Configuration.WhiteListCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
// Use a using statement to ensure the OcrInput object is disposed of correctly
using (var inputLicensePlate = new OcrInput())
{
// Load the license plate image
inputLicensePlate.AddImage("plate.jpeg");
// Perform OCR to read the license plate. This uses the Read method from the IronTesseract library.
OcrResult result = ocr.Read(inputLicensePlate);
// Prepare the output string containing the OCR result and the confidence level
string output = $"{result.Text}\nResult Confidence: {result.Confidence*100}%";
// Print the result to the console
Console.WriteLine(output);
}
Imports Microsoft.VisualBasic
Imports IronOcr
Imports System
' Initialize the IronTesseract OCR object
Private ocr = New IronTesseract()
' Whitelist characters that are typically found in license plates
ocr.Configuration.WhiteListCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
' Use a using statement to ensure the OcrInput object is disposed of correctly
Using inputLicensePlate = New OcrInput()
' Load the license plate image
inputLicensePlate.AddImage("plate.jpeg")
' Perform OCR to read the license plate. This uses the Read method from the IronTesseract library.
Dim result As OcrResult = ocr.Read(inputLicensePlate)
' Prepare the output string containing the OCR result and the confidence level
Dim output As String = $"{result.Text}" & vbLf & "Result Confidence: {result.Confidence*100}%"
' Print the result to the console
Console.WriteLine(output)
End Using
Output
The code demonstrates how to import an image as an OcrInput
and use it with the ReadLicensePlate
method to extract the text from the license plate. The output shows the extracted text that matches the license plate shown in the input image, along with a confidence level indicating the accuracy of the OCR.
Text: The extracted text from OCR Input.
Confidence: A "double" property that indicates the statistical accuracy confidence of an average of every character, with one being the highest and 0 being the lowest.
License Plate From a Car Image
The method also works well with images containing a car with a license plate. The code is the exact same as the one above, with the input image changed. You can also extract the pixel coordinates of the area where the license plate is situated in the image.
Example Input
:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-read-from-car.cs
// Import necessary namespaces
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Linq; // Ensure the LINQ namespace is imported for FirstOrDefault
// Initialize the IronTesseract OCR engine
var ocr = new IronTesseract();
// Create an instance of OcrInput to hold our input image
using var inputLicensePlate = new OcrInput();
// Load the image containing the car license plate into the OcrInput instance
inputLicensePlate.AddImage("car_license.jpg");
// Perform OCR to get license plate information
OcrResult result = ocr.Read(inputLicensePlate);
// Check if the OCR result is valid and contains text
if (result.Success)
{
// Retrieve the text recognized as the license plate
string licensePlateText = result.Text;
// Assuming result.Text contains the text and result.Regions stores regions of detected text
var firstRegion = result.Regions.FirstOrDefault();
if (firstRegion != null)
{
// Retrieve license plate coordinates from the recognized region
RectangleF rectangle = firstRegion.Rectangle;
// Format the output string to include the license plate number and its coordinates
string output = $"License Plate Number:\n{licensePlateText}\n\n"
+ $"License Plate Area:\n"
+ $"Starting X: {rectangle.X}\n"
+ $"Starting Y: {rectangle.Y}\n"
+ $"Width: {rectangle.Width}\n"
+ $"Height: {rectangle.Height}";
// Print the formatted output to the console
Console.WriteLine(output);
}
else
{
// If no text regions are detected, inform the user
Console.WriteLine("No regions detected in the image.");
}
}
else
{
// If OCR fails, inform the user of failure to recognize text
Console.WriteLine("OCR failed to recognize text in the image.");
}
' Import necessary namespaces
Imports Microsoft.VisualBasic
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Linq ' Ensure the LINQ namespace is imported for FirstOrDefault
' Initialize the IronTesseract OCR engine
Private ocr = New IronTesseract()
' Create an instance of OcrInput to hold our input image
Private inputLicensePlate = New OcrInput()
' Load the image containing the car license plate into the OcrInput instance
inputLicensePlate.AddImage("car_license.jpg")
' Perform OCR to get license plate information
Dim result As OcrResult = ocr.Read(inputLicensePlate)
' Check if the OCR result is valid and contains text
If result.Success Then
' Retrieve the text recognized as the license plate
Dim licensePlateText As String = result.Text
' Assuming result.Text contains the text and result.Regions stores regions of detected text
Dim firstRegion = result.Regions.FirstOrDefault()
If firstRegion IsNot Nothing Then
' Retrieve license plate coordinates from the recognized region
Dim rectangle As RectangleF = firstRegion.Rectangle
' Format the output string to include the license plate number and its coordinates
Dim output As String = $"License Plate Number:" & vbLf & "{licensePlateText}" & vbLf & vbLf & $"License Plate Area:" & vbLf & $"Starting X: {rectangle.X}" & vbLf & $"Starting Y: {rectangle.Y}" & vbLf & $"Width: {rectangle.Width}" & vbLf & $"Height: {rectangle.Height}"
' Print the formatted output to the console
Console.WriteLine(output)
Else
' If no text regions are detected, inform the user
Console.WriteLine("No regions detected in the image.")
End If
Else
' If OCR fails, inform the user of failure to recognize text
Console.WriteLine("OCR failed to recognize text in the image.")
End If
Output
The example shows how the ReadLicensePlate
method can be applied to an image of a car. The method will also return the rectangle coordinates of where the license plate is situated in the image.
This method is optimized to find single license plates only and is capable of searching for them in stock images.
Frequently Asked Questions
What is IronOCR?
IronOCR is a C# library that allows developers to perform optical character recognition, including extracting text from images of license plates.
How does the ReadLicensePlate method work?
The ReadLicensePlate method in IronOCR takes an OcrInput parameter containing the image of the license plate. It extracts the license plate number and provides a confidence level indicating the accuracy of the recognition.
What are the benefits of using IronOCR for reading license plates?
Using IronOCR for license plate reading automates the process, improving efficiency and accuracy compared to manual methods. This is particularly useful in applications like parking management, toll collection, and security surveillance.
Which languages does the ReadLicensePlate method support?
The ReadLicensePlate method currently supports English, Chinese, Japanese, Korean, and Latin alphabet scripts.
What additional package is required to use the ReadLicensePlate method?
To use the ReadLicensePlate method, you must install the IronOcr.Extensions.AdvancedScan package.
Can IronOCR read license plates from images of cars?
Yes, IronOCR can read license plates from images of cars and even extract the coordinates of the license plate area within the image.
How can I enhance the performance of license plate recognition with IronOCR?
You can enhance performance by configuring IronOCR to whitelist specific characters that are commonly found in license plates, which speeds up the processing.
What is the confidence level provided by the ReadLicensePlate method?
The confidence level is a 'double' property indicating the statistical accuracy of the OCR process, with a value from 0 to 1, where 1 is the highest confidence.