C# paddlerocrsharp识别身份证号

https://gitee.com/raoyutian/paddle-ocrsharp

项目搭建

新建控制台项目

安装paddleocrsharp

下载训练好的模型

解压放到对应的文件夹中,都修改为如果较新则复制

编写代码OCRHelper.cs

cs 复制代码
using PaddleOCRSharp;

namespace OCRTest02;

public class OCRHelper
{
    /// <summary>
    /// 初始化OCR引擎
    /// </summary>
    /// <returns></returns>
    public static PaddleOCREngine initOcrEngine(string exePath)
    {
        //OCR参数
        OCRParameter oCRParameter = new OCRParameter();
        oCRParameter.cpu_math_library_num_threads = 6;//预测并发线程数,CPU预测时的线程数,在机器核数充足的情况下,该值越大,预测速度越快;默认10
        oCRParameter.enable_mkldnn = true;//web部署该值建议设置为0,否则出错,内存如果使用很大,建议该值也设置为0.
        oCRParameter.cls = true; //是否执行文字方向分类;默认false
        oCRParameter.use_angle_cls = true;//是否开启方向检测,用于检测识别180旋转
        oCRParameter.det_db_score_mode = false;//是否使用多段线,即文字区域是用多段线还是用矩形,
        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

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");
}

识别结果的精度还是比较高的但是目前数据比较乱,需要找到身份证号和姓名,下面代码可以识别横着、竖着的身份证信息并且提取出来,斜这的就不太行了,需要做图像校正,可以尝试一下https://github.com/shakex/card-rectification

cs 复制代码
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("保存成功");
相关推荐
向宇it8 分钟前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
Mr_Xuhhh16 分钟前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
旺旺大力包20 分钟前
【JS笔记】JS 和 noodjs 的常见操作(十)
开发语言·javascript·node.js·ecmascript
背影疾风1 小时前
C++之路:类基础、构造析构、拷贝构造函数
linux·开发语言·c++
Ting-yu1 小时前
Java中Stream流的使用
java·开发语言·windows
一线码农1 小时前
MinHook 如何对 .NET 母体 CoreCLR 进行拦截
c#·.net·代码注入
【ql君】qlexcel2 小时前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制
Zevalin爱灰灰2 小时前
MATLAB GUI界面设计 第六章——常用库中的其它组件
开发语言·ui·matlab
冰糖猕猴桃2 小时前
【Python】进阶 - 数据结构与算法
开发语言·数据结构·python·算法·时间复杂度、空间复杂度·树、二叉树·堆、图
wt_cs2 小时前
银行回单ocr api集成解析-图像文字识别-文字识别技术
开发语言·python