目錄
效果
模型信息
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# PaddleOCR 車牌識別參考?https://lw112190.blog.csdn.net/article/details/131010997