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#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

C#如何在Excel文档中获取分页信息

《C#如何在Excel文档中获取分页信息》在日常工作中,我们经常需要处理大量的Excel数据,本文将深入探讨如何利用Spire.XLSfor.NET,高效准确地获取Excel文档中的分页信息,包括水平... 目录理解Excel中的分页机制借助 Spire.XLS for .NET 获取分页信息为什么选择 S

C#高效实现在Word文档中自动化创建图表的可视化方案

《C#高效实现在Word文档中自动化创建图表的可视化方案》本文将深入探讨如何利用C#,结合一款功能强大的第三方库,实现在Word文档中自动化创建图表,为你的数据呈现和报告生成提供一套实用且高效的解决方... 目录Word文档图表自动化:为什么选择C#?从零开始:C#实现Word文档图表的基本步骤深度优化:C

在C#中分离饼图的某个区域的操作指南

《在C#中分离饼图的某个区域的操作指南》在处理Excel饼图时,我们可能需要将饼图的各个部分分离出来,以使它们更加醒目,Spire.XLS提供了Series.DataFormat.Percent属性,... 目录引言如何设置饼图各分片之间分离宽度的代码示例:从整个饼图中分离单个分片的代码示例:引言在处理

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#实现将Excel工作表拆分为多个窗格

《C#实现将Excel工作表拆分为多个窗格》在日常工作中,我们经常需要处理包含大量数据的Excel文件,本文将深入探讨如何在C#中利用强大的Spire.XLSfor.NET自动化实现Excel工作表的... 目录为什么需要拆分 Excel 窗格借助 Spire.XLS for .NET 实现冻结窗格(Fro