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# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间