C# PaddleDetection 证件分类 驾驶证、行驶证、身份证分类

本文主要是介绍C# PaddleDetection 证件分类 驾驶证、行驶证、身份证分类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

项目

效果

代码


项目

VS2022+.net4.8+OpenCvSharp4+Sdcb.PaddleDetection

效果

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using Sdcb.PaddleDetection;
using Sdcb.PaddleInference;
using System;
using System.Drawing;
using System.Windows.Forms;
using YamlDotNet;

namespace PaddleDetection_证件分类
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";

        double fontScale = 2D;
        int thickness = 4;
        LineTypes lineType = LineTypes.Link4;

        PaddleConfig paddleConfig;
        PaddleDetector d;
        String startupPath;
        float confidence = 0.75f;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pic_jsz1.Image = null;
            pic_jsz2.Image = null;
            pic_sfz.Image = null;
            pic_xsz1.Image = null;
            pic_xsz2.Image = null;

            img = ofd.FileName;
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);
        }

        /// <summary>
        /// 剪裁图片
        /// </summary>
        /// <param name="src">原图片</param>
        /// <param name="left">左坐标</param>
        /// <param name="top">顶部坐标</param>
        /// <param name="right">右坐标</param>
        /// <param name="bottom">底部坐标</param>
        /// <returns>剪裁后的图片</returns>
        public Image CutImage(Image src, int left, int top, int right, int bottom)
        {
            Bitmap srcBitmap = new Bitmap(src);
            int width = right - left;
            int height = bottom - top;
            Bitmap destBitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(destBitmap))
            {
                g.Clear(Color.Transparent);
                //设置画布的描绘质量         
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);
            }
            return destBitmap;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Mat src = Cv2.ImRead(img);
            DetectionResult[] r = d.Run(src);

            for (int i = 0; i < r.Length; i++)
            {
                if (r[i].Confidence > confidence)
                {
                    Image temp = CutImage(bmp, r[i].Rect.X, r[i].Rect.Y, r[i].Rect.X + r[i].Rect.Width, r[i].Rect.Y + r[i].Rect.Height);
                    if (r[i].LabelName == "sfz")
                    {
                        pic_sfz.Image = temp;
                    }
                    else if (r[i].LabelName == "xsz1")
                    {
                        pic_xsz1.Image = temp;
                    }
                    else if (r[i].LabelName == "xsz2")
                    {
                        pic_xsz2.Image = temp;
                    }
                    else if (r[i].LabelName == "jsz1")
                    {
                        pic_jsz1.Image = temp;
                    }
                    else if (r[i].LabelName == "jsz2")
                    {
                        pic_jsz2.Image = temp;
                    }

                    Scalar scalar = Scalar.RandomColor();
                    Cv2.Rectangle(src, r[i].Rect, scalar, 1, LineTypes.Link8, 0);
                    Cv2.PutText(src, r[i].LabelName, new OpenCvSharp.Point(r[i].Rect.X + r[i].Rect.Width / 2, r[i].Rect.Y + r[i].Rect.Height / 2), HersheyFonts.HersheyComplex, fontScale, scalar, thickness, lineType, false);
                }
            }

            //Cv2.ImShow("src", src);
            pictureBox1.Image = BitmapConverter.ToBitmap(src);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            paddleConfig = PaddleConfig.FromModelDir(startupPath + "\\models\\");
            string configYmlPath = startupPath + "\\models\\infer_cfg.yml";
            d = new PaddleDetector(paddleConfig, configYmlPath);
        }
    }
}

using OpenCvSharp;
using OpenCvSharp.Extensions;
using Sdcb.PaddleDetection;
using Sdcb.PaddleInference;
using System;
using System.Drawing;
using System.Windows.Forms;
using YamlDotNet;namespace PaddleDetection_证件分类
{public partial class Form1 : Form{public Form1(){InitializeComponent();}Bitmap bmp;string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string img = "";double fontScale = 2D;int thickness = 4;LineTypes lineType = LineTypes.Link4;PaddleConfig paddleConfig;PaddleDetector d;String startupPath;float confidence = 0.75f;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pic_jsz1.Image = null;pic_jsz2.Image = null;pic_sfz.Image = null;pic_xsz1.Image = null;pic_xsz2.Image = null;img = ofd.FileName;bmp = new Bitmap(img);pictureBox1.Image = new Bitmap(img);}/// <summary>/// 剪裁图片/// </summary>/// <param name="src">原图片</param>/// <param name="left">左坐标</param>/// <param name="top">顶部坐标</param>/// <param name="right">右坐标</param>/// <param name="bottom">底部坐标</param>/// <returns>剪裁后的图片</returns>public Image CutImage(Image src, int left, int top, int right, int bottom){Bitmap srcBitmap = new Bitmap(src);int width = right - left;int height = bottom - top;Bitmap destBitmap = new Bitmap(width, height);using (Graphics g = Graphics.FromImage(destBitmap)){g.Clear(Color.Transparent);//设置画布的描绘质量         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;g.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);}return destBitmap;}private void button2_Click(object sender, EventArgs e){Mat src = Cv2.ImRead(img);DetectionResult[] r = d.Run(src);for (int i = 0; i < r.Length; i++){if (r[i].Confidence > confidence){Image temp = CutImage(bmp, r[i].Rect.X, r[i].Rect.Y, r[i].Rect.X + r[i].Rect.Width, r[i].Rect.Y + r[i].Rect.Height);if (r[i].LabelName == "sfz"){pic_sfz.Image = temp;}else if (r[i].LabelName == "xsz1"){pic_xsz1.Image = temp;}else if (r[i].LabelName == "xsz2"){pic_xsz2.Image = temp;}else if (r[i].LabelName == "jsz1"){pic_jsz1.Image = temp;}else if (r[i].LabelName == "jsz2"){pic_jsz2.Image = temp;}Scalar scalar = Scalar.RandomColor();Cv2.Rectangle(src, r[i].Rect, scalar, 1, LineTypes.Link8, 0);Cv2.PutText(src, r[i].LabelName, new OpenCvSharp.Point(r[i].Rect.X + r[i].Rect.Width / 2, r[i].Rect.Y + r[i].Rect.Height / 2), HersheyFonts.HersheyComplex, fontScale, scalar, thickness, lineType, false);}}//Cv2.ImShow("src", src);pictureBox1.Image = BitmapConverter.ToBitmap(src);}private void Form1_Load(object sender, EventArgs e){startupPath = System.Windows.Forms.Application.StartupPath;paddleConfig = PaddleConfig.FromModelDir(startupPath + "\\models\\");string configYmlPath = startupPath + "\\models\\infer_cfg.yml";d = new PaddleDetector(paddleConfig, configYmlPath);}}
}

这篇关于C# PaddleDetection 证件分类 驾驶证、行驶证、身份证分类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/587119

相关文章

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

C#中的 StreamReader/StreamWriter 使用示例详解

《C#中的StreamReader/StreamWriter使用示例详解》在C#开发中,StreamReader和StreamWriter是处理文本文件的核心类,属于System.IO命名空间,本... 目录前言一、什么是 StreamReader 和 StreamWriter?1. 定义2. 特点3. 用

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.