C# paddlerocrsharp識別身份證號的實現(xiàn)示例
paddle-ocrsharp倉庫地址:https://gitee.com/raoyutian/paddle-ocrsharp
項目搭建
新建控制臺項目
安裝paddleocrsharp

下載訓(xùn)練好的模型

解壓放到對應(yīng)的文件夾中,都修改為如果較新則復(fù)制


編寫代碼OCRHelper.cs
using PaddleOCRSharp;
namespace OCRTest02;
public class OCRHelper
{
/// <summary>
/// 初始化OCR引擎
/// </summary>
/// <returns></returns>
public static PaddleOCREngine initOcrEngine(string exePath)
{
//OCR參數(shù)
OCRParameter oCRParameter = new OCRParameter();
oCRParameter.cpu_math_library_num_threads = 6;//預(yù)測并發(fā)線程數(shù),CPU預(yù)測時的線程數(shù),在機器核數(shù)充足的情況下,該值越大,預(yù)測速度越快;默認10
oCRParameter.enable_mkldnn = true;//web部署該值建議設(shè)置為0,否則出錯,內(nèi)存如果使用很大,建議該值也設(shè)置為0.
oCRParameter.cls = true; //是否執(zhí)行文字方向分類;默認false
oCRParameter.use_angle_cls = true;//是否開啟方向檢測,用于檢測識別180旋轉(zhuǎn)
oCRParameter.det_db_score_mode = false;//是否使用多段線,即文字區(qū)域是用多段線還是用矩形,
oCRParameter.det_db_unclip_ratio = 1.6f;
//OCR配置
OCRModelConfig config = new OCRModelConfig();
string modelPathroot = Path.Join(exePath,"ch_PP-OCRv4");
config.det_infer = modelPathroot + @"\ch_PP-OCRv4_det_infer";
config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer";
config.rec_infer = modelPathroot + @"\ch_PP-OCRv4_rec_infer";
config.keys = modelPathroot + @"\ppocr_keys.txt";
//初始化OCR引擎
return new PaddleOCREngine(config, oCRParameter);
}
}
Program.cs
using OCRTest02;
var exePath = AppDomain.CurrentDomain.BaseDirectory;
var engine = OCRHelper.initOcrEngine(exePath);
Console.WriteLine("初始化成功");
var filePath = "F:\\Desktop\\韋小寶.png";
var imagebyte = File.ReadAllBytes(filePath);
var ocrRes = engine.DetectText(imagebyte);
foreach (var textBlock in ocrRes.TextBlocks)
{
Console.Write(string.Join(";",textBlock.BoxPoints));
Console.Write("\t"+textBlock.Text);
Console.Write("\t"+textBlock.Score+"\n");
}

識別結(jié)果的精度還是比較高的但是目前數(shù)據(jù)比較亂,需要找到身份證號和姓名,下面代碼可以識別橫著、豎著的身份證信息并且提取出來,斜這的就不太行了,需要做圖像校正,可以嘗試一下https://github.com/shakex/card-rectification
using System.Text.RegularExpressions;
using OCRTest02;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Processing;
var exePath = AppDomain.CurrentDomain.BaseDirectory;
var engine = OCRHelper.initOcrEngine(exePath);
Console.WriteLine("初始化成功");
var filePath = "F:\\Desktop\\韋小寶.jpg";
var imagebyte = File.ReadAllBytes(filePath);
var ocrRes = engine.DetectText(imagebyte);
string name = "";
string id = "";
var sfzImg = Image.Load(filePath);
foreach (var textBlock in ocrRes.TextBlocks)
{
//Console.WriteLine(textBlock.Text);
#region 繪制包圍矩形
List<PointF> points = new List<PointF>();
foreach (var point in textBlock.BoxPoints)
{
points.Add(new PointF(point.X,point.Y));
}
sfzImg.Mutate(x=>x.DrawPolygon(new SolidPen(Color.HotPink,2.0f),points.ToArray()));
#endregion
if (string.IsNullOrWhiteSpace(textBlock.Text))
{
continue;
}
var tmpText = textBlock.Text.Trim();
if (tmpText.Contains("姓名"))
{
tmpText = tmpText.Replace("姓名", "");
name = tmpText;
}
var idMatch = new Regex(@"^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$");
if (idMatch.IsMatch(tmpText))
{
id = tmpText;
}
}
Console.WriteLine($"姓名:{name},身份證號:{id}");
var now = DateTime.Now;
var resultName = now.ToString("yyyy_MM_dd_HH_mm_ss");
sfzImg.Save($"{resultName}.jpg");
Console.WriteLine("保存成功");
到此這篇關(guān)于C# paddlerocrsharp識別身份證號的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)C# paddlerocrsharp識別身份證號內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 如何使用OpcUaHelper讀寫OPC服務(wù)器
這篇文章給大家介紹C# 如何使用OpcUaHelper讀寫OPC服務(wù)器,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友參考下吧2023-12-12
C#使用StringBuilder實現(xiàn)高效處理字符串
這篇文章主要為大家詳細介紹了C#如何使用StringBuilder實現(xiàn)高效處理字符串,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下2024-01-01
winform創(chuàng)建不規(guī)則窗體的方法
這篇文章主要介紹了winform創(chuàng)建不規(guī)則窗體的方法,涉及C#窗體創(chuàng)建的相關(guān)參數(shù)設(shè)置技巧,非常具有實用價值,需要的朋友可以參考下2015-09-09
C# XML字符串包含特殊字符的處理轉(zhuǎn)換方法小結(jié)
今天用C#輸出XML文件時,發(fā)現(xiàn)報錯,經(jīng)過反復(fù)檢查調(diào)試,發(fā)現(xiàn)是因為某處內(nèi)容含有某些特殊字符,這些特殊字符是在XML里不被允許的2020-07-07

