C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色)

2023-11-23 16:28

本文主要是介绍C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

效果

模型信息

mot_ppyoloe_s_36e_ppvehicle.onnx 

vehicle_attribute_model.onnx

项目

代码

下载


C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色)

效果

模型信息

mot_ppyoloe_s_36e_ppvehicle.onnx 

Inputs
-------------------------
name:image
tensor:Float[-1, 3, 640, 640]
name:scale_factor
tensor:Float[-1, 2]
---------------------------------------------------------------

Outputs
-------------------------
name:multiclass_nms3_0.tmp_0
tensor:Float[-1, 6]
name:multiclass_nms3_0.tmp_2
tensor:Int32[1]
---------------------------------------------------------------

vehicle_attribute_model.onnx

Inputs
-------------------------
name:x
tensor:Float[-1, 3, 192, 256]
---------------------------------------------------------------

Outputs
-------------------------
name:sigmoid_2.tmp_0
tensor:Float[-1, 19]
---------------------------------------------------------------

项目

VS2022

.net framework 4.8

OpenCvSharp 4.8

Microsoft.ML.OnnxRuntime 1.16.2

代码

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Text;

namespace Onnx_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

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

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        Mat image;

        PP_YOLOE pp_yoloe;
        VehicleAttr vehicleAttr;

        StringBuilder sb = new StringBuilder();

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

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new System.Drawing.Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pp_yoloe = new PP_YOLOE("model/mot_ppyoloe_s_36e_ppvehicle.onnx", 0.6f);

            vehicleAttr = new VehicleAttr("model/vehicle_attribute_model.onnx");

            image_path = "test_img/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);

        }

        private unsafe void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "检测中,请稍等……";
            pictureBox2.Image = null;
            sb.Clear();
            System.Windows.Forms.Application.DoEvents();

            image = new Mat(image_path);

            dt1 = DateTime.Now;
            List<BoxInfo> ltBoxInfo = pp_yoloe.Detect(image);
            dt2 = DateTime.Now;

            Mat result_image = image.Clone();
            //pp_yoloe.DrawPred(result_image, ltBoxInfo);

            sb.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            sb.AppendLine("------------------------------");

            for (int n = 0; n < ltBoxInfo.Count; n++)
            {

                Rect rect = new Rect();
                rect.X = (int)ltBoxInfo[n].xmin;
                rect.Y = (int)ltBoxInfo[n].ymin;
                rect.Width = (int)(ltBoxInfo[n].xmax - ltBoxInfo[n].xmin);
                rect.Height = (int)(ltBoxInfo[n].ymax - ltBoxInfo[n].ymin);
                Mat crop_img = new Mat(image, rect);

                string color_res_str = "color:";
                string type_res_str = "type:";

                vehicleAttr.Detect(crop_img, out color_res_str, out type_res_str);

                Cv2.Rectangle(result_image, new OpenCvSharp.Point(ltBoxInfo[n].xmin, ltBoxInfo[n].ymin), new OpenCvSharp.Point(ltBoxInfo[n].xmax, ltBoxInfo[n].ymax), new Scalar(0, 0, 255), 2);
                Cv2.PutText(result_image
                    , type_res_str + "," + color_res_str
                    , new OpenCvSharp.Point(ltBoxInfo[n].xmin, ltBoxInfo[n].ymin - 10)
                    , HersheyFonts.HersheySimplex
                    , 1
                    , new Scalar(0, 255, 0)
                    , 2);

                sb.AppendLine("vehicle:" + ltBoxInfo[n].score.ToString("0.00") + " " + type_res_str + "," + color_res_str);
            }


            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }

            pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream());
            textBox1.Text = sb.ToString();
        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
    }
}
 

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Text;namespace Onnx_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;Mat image;PP_YOLOE pp_yoloe;VehicleAttr vehicleAttr;StringBuilder sb = new StringBuilder();private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path = ofd.FileName;pictureBox1.Image = new System.Drawing.Bitmap(image_path);image = new Mat(image_path);}private void Form1_Load(object sender, EventArgs e){pp_yoloe = new PP_YOLOE("model/mot_ppyoloe_s_36e_ppvehicle.onnx", 0.6f);vehicleAttr = new VehicleAttr("model/vehicle_attribute_model.onnx");image_path = "test_img/1.jpg";pictureBox1.Image = new Bitmap(image_path);}private unsafe void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;sb.Clear();System.Windows.Forms.Application.DoEvents();image = new Mat(image_path);dt1 = DateTime.Now;List<BoxInfo> ltBoxInfo = pp_yoloe.Detect(image);dt2 = DateTime.Now;Mat result_image = image.Clone();//pp_yoloe.DrawPred(result_image, ltBoxInfo);sb.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");sb.AppendLine("------------------------------");for (int n = 0; n < ltBoxInfo.Count; n++){Rect rect = new Rect();rect.X = (int)ltBoxInfo[n].xmin;rect.Y = (int)ltBoxInfo[n].ymin;rect.Width = (int)(ltBoxInfo[n].xmax - ltBoxInfo[n].xmin);rect.Height = (int)(ltBoxInfo[n].ymax - ltBoxInfo[n].ymin);Mat crop_img = new Mat(image, rect);string color_res_str = "color:";string type_res_str = "type:";vehicleAttr.Detect(crop_img, out color_res_str, out type_res_str);Cv2.Rectangle(result_image, new OpenCvSharp.Point(ltBoxInfo[n].xmin, ltBoxInfo[n].ymin), new OpenCvSharp.Point(ltBoxInfo[n].xmax, ltBoxInfo[n].ymax), new Scalar(0, 0, 255), 2);Cv2.PutText(result_image, type_res_str + "," + color_res_str, new OpenCvSharp.Point(ltBoxInfo[n].xmin, ltBoxInfo[n].ymin - 10), HersheyFonts.HersheySimplex, 1, new Scalar(0, 255, 0), 2);sb.AppendLine("vehicle:" + ltBoxInfo[n].score.ToString("0.00") + " " + type_res_str + "," + color_res_str);}if (pictureBox2.Image != null){pictureBox2.Image.Dispose();}pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream());textBox1.Text = sb.ToString();}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载

源码下载

这篇关于C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

使用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. 查询数据三、事务处

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3