如何使用IronOCR識別車牌
在管理大量車輛圖像時,手動讀取車牌既耗時又容易發生人為錯誤。 使用像 IronOCR 這樣的工具來自動化此流程,能提供一個更高效且準確的解決方案。 IronOCR 的 ReadLicensePlate
方法可以程式化地從圖像中提取車牌號碼,節省大量時間的同時提高數據準確性。
在本指南中,我們將演示如何使用IronOCR進行車牌識別,通過示例和可自定義的配置使過程更加順暢。 通過利用這些方法,開發人員可以自動化車牌識別,使停車管理、收費管理或安全監控等任務更加高效。
如何使用IronOCR識別車牌
- 下載用於讀取車牌的 C# 程式庫
- 導入車牌影像進行處理
- 確保文件僅包含車牌圖像,沒有頁首或頁尾。
- 使用
讀取車牌號碼
從影像中提取資料的方法 - 訪問OcrLicensePlateResult查看和操作提取的授權數據的屬性
立即在您的專案中使用IronOCR,並享受免費試用。
若要使用此功能,您還需要安裝IronOcr.Extension.AdvancedScan套件。
閱讀車牌範例
要在 IronOCR 中讀取車牌,我們必須執行以下步驟:
- 我們使用
ReadLicensePlate
方法,該方法將OcrInput
作為輸入的參數。 此方法比該庫的標準Read
方法更精確地針對車牌進行優化。 -
可選:我們可以配置IronOCR僅允許特定字符(這些字符可以存在於車牌中)以加速車牌號碼處理。
[{我(
- 該方法目前僅適用於英語、中文、日語、韓語和拉丁字母。
-
使用高級掃描功能在 .NET Framework 上運行需要項目在 x64 架構上運行。
)}]
車牌
代碼
:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-read-license-plate.cs
using IronOcr;
using System;
var ocr = new IronTesseract();
ocr.Configuration.WhiteListCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
using var inputLicensePlate = new OcrInput();
inputLicensePlate.LoadImage("plate.jpeg");
// Read license plate
OcrLicensePlateResult result = ocr.ReadLicensePlate(inputLicensePlate);
// Retrieve license plate number and confidence value
string output = $"{result.Text}\nResult Confidence: {result.Confidence}";
Console.WriteLine(output);
Imports Microsoft.VisualBasic
Imports IronOcr
Imports System
Private ocr = New IronTesseract()
ocr.Configuration.WhiteListCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
Dim inputLicensePlate = New OcrInput()
inputLicensePlate.LoadImage("plate.jpeg")
' Read license plate
Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(inputLicensePlate)
' Retrieve license plate number and confidence value
Dim output As String = $"{result.Text}" & vbLf & "Result Confidence: {result.Confidence}"
Console.WriteLine(output)
輸出
請注意,我們必須首先將圖像導入為 OcrInput
,以正確傳遞參數給 ReadLicensePlate
方法。
正如您從控制台輸出中所看到的,提取的文本與輸入圖像中顯示的車牌號和州一致。
從 OCR 輸入中提取的文本。
信心:一個「雙精度浮點」屬性,表示每個字元平均的統計準確性信心水平,其中1表示最高,0表示最低。
從車輛圖像識別車牌
該方法對包含車牌的單張圖像也能很好地運作。 程式碼與上面的一模一樣,只是更換了輸入圖片。 您還可以提取影像中車牌所在區域的像素座標。
範例輸入:
:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-read-from-car.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
var ocr = new IronTesseract();
using var inputLicensePlate = new OcrInput();
inputLicensePlate.LoadImage("car_license.jpg");
// Read license plate
OcrLicensePlateResult result = ocr.ReadLicensePlate(inputLicensePlate);
// Retrieve license plate coordinates
RectangleF rectangle = result.Licenseplate;
// Write license plate value and coordinates in a string
string output = $"License Plate Number:\n{result.Text}\n\n"
+ $"License Plate Area_\n"
+ $"Starting X: {rectangle.X}\n"
+ $"Starting Y: {rectangle.Y}\n"
+ $"Width: {rectangle.Width}\n"
+ $"Height: {rectangle.Height}";
Console.WriteLine(output);
Imports Microsoft.VisualBasic
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Private ocr = New IronTesseract()
Private inputLicensePlate = New OcrInput()
inputLicensePlate.LoadImage("car_license.jpg")
' Read license plate
Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(inputLicensePlate)
' Retrieve license plate coordinates
Dim rectangle As RectangleF = result.Licenseplate
' Write license plate value and coordinates in a string
Dim output As String = $"License Plate Number:" & vbLf & "{result.Text}" & vbLf & vbLf & $"License Plate Area_" & vbLf & $"Starting X: {rectangle.X}" & vbLf & $"Starting Y: {rectangle.Y}" & vbLf & $"Width: {rectangle.Width}" & vbLf & $"Height: {rectangle.Height}"
Console.WriteLine(output)
輸出
正如您所見,輸出與上方顯示的車牌相符,並且圖片中車牌的精確矩形區域也被正確提取。
此方法經過優化,僅能找到單一的車牌,並且能夠在庫存圖像中搜尋它們。