C# OnnxRuntime yolo11 pest detection

news/2024/10/22 11:06:15/

 

目录

介绍

效果

模型信息

项目

代码

下载


介绍

官网地址:GitHub - ultralytics/ultralytics: Ultralytics YOLO11 🚀

YoloV11(You Only Look Once V11)是一种计算机视觉算法,用于目标检测。它是由 Joseph Redmon 提出的卷积神经网(CNN)模型,是Yolo系列算法中的最新版本。

YoloV11相对于前几个版本来说,主要有以下几点改进:1)更轻量级;2)更加准确;3)更快的推理速度。

  • YoloV11相对于其他目标检测算法具有更轻量级的优势。这是因为它的核心架构检测网络使用了更多的轻量级卷积和池化层,从而将其大小缩小到仅有20MB大小。这使得它在处理较小的图像上具有显著的优势,也使得它更易于部署到基于移动设备和嵌入式设备的应用中。
  • YoloV11在准确性方面也有了显著的提升。这是因为它引入了更多的改进和优化来提高检测性能。例如,它使用了更多的卷积层来提取更丰富的特征信息,同时还使用了更多的跳跃连接来让高层特征信息能够被直接传递到低层特征信息中,从而在物体大小和位置检测中提高了准确性。
  • YoloV11具有更高的推理速度。这是因为它采用了一些优秀的加速技术,如SPPnet和SAM,以提高网络的能力和速度。此外,通过与其他检测算法比较,我们可以看到,YoloV11在GPU测试中的运行速度相对较快,可以在实时检测中实现准确性和速度的平衡。

YoloV11是一种新型的轻量级、高精度、高速度的目标检测算法,具有广泛的应用前景,尤其是在基于嵌入式和移动设备的应用中具有巨大的潜力。

效果

模型信息

Model Properties
-------------------------
date:2024-10-22T08:46:38.254437
author:Ultralytics
version:8.3.5
task:detect
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[736, 736]
names:{0: 'rice leaf roller', 1: 'rice leaf caterpillar', 2: 'paddy stem maggot', 3: 'asiatic rice borer', 4: 'yellow rice borer', 5: 'rice gall midge', 6: 'Rice Stemfly', 7: 'brown plant hopper', 8: 'white backed plant hopper', 9: 'small brown plant hopper', 10: 'rice water weevil', 11: 'rice leafhopper', 12: 'grain spreader thrips', 13: 'rice shell pest', 14: 'grub', 15: 'mole cricket', 16: 'wireworm', 17: 'white margined moth', 18: 'black cutworm', 19: 'large cutworm', 20: 'yellow cutworm', 21: 'red spider', 22: 'corn borer', 23: 'army worm', 24: 'aphids', 25: 'Potosiabre vitarsis', 26: 'peach borer', 27: 'english grain aphid', 28: 'green bug', 29: 'bird cherry-oataphid', 30: 'wheat blossom midge', 31: 'penthaleus major', 32: 'longlegged spider mite', 33: 'wheat phloeothrips', 34: 'wheat sawfly', 35: 'cerodonta denticornis', 36: 'beet fly', 37: 'flea beetle', 38: 'cabbage army worm', 39: 'beet army worm', 40: 'Beet spot flies', 41: 'meadow moth', 42: 'beet weevil', 43: 'sericaorient alismots chulsky', 44: 'alfalfa weevil', 45: 'flax budworm', 46: 'alfalfa plant bug', 47: 'tarnished plant bug', 48: 'Locustoidea', 49: 'lytta polita', 50: 'legume blister beetle', 51: 'blister beetle', 52: 'therioaphis maculata Buckton', 53: 'odontothrips loti', 54: 'Thrips', 55: 'alfalfa seed chalcid', 56: 'Pieris canidia', 57: 'Apolygus lucorum', 58: 'Limacodidae', 59: 'Viteus vitifoliae', 60: 'Colomerus vitis', 61: 'Brevipoalpus lewisi McGregor', 62: 'oides decempunctata', 63: 'Polyphagotars onemus latus', 64: 'Pseudococcus comstocki Kuwana', 65: 'parathrene regalis', 66: 'Ampelophaga', 67: 'Lycorma delicatula', 68: 'Xylotrechus', 69: 'Cicadella viridis', 70: 'Miridae', 71: 'Trialeurodes vaporariorum', 72: 'Erythroneura apicalis', 73: 'Papilio xuthus', 74: 'Panonchus citri McGregor', 75: 'Phyllocoptes oleiverus ashmead', 76: 'Icerya purchasi Maskell', 77: 'Unaspis yanonensis', 78: 'Ceroplastes rubens', 79: 'Chrysomphalus aonidum', 80: 'Parlatoria zizyphus Lucus', 81: 'Nipaecoccus vastalor', 82: 'Aleurocanthus spiniferus', 83: 'Tetradacus c Bactrocera minax ', 84: 'Dacus dorsalis(Hendel)', 85: 'Bactrocera tsuneonis', 86: 'Prodenia litura', 87: 'Adristyrannus', 88: 'Phyllocnistis citrella Stainton', 89: 'Toxoptera citricidus', 90: 'Toxoptera aurantii', 91: 'Aphis citricola Vander Goot', 92: 'Scirtothrips dorsalis Hood', 93: 'Dasineura sp', 94: 'Lawana imitata Melichar', 95: 'Salurnis marginella Guerr', 96: 'Deporaus marginatus Pascoe', 97: 'Chlumetia transversa', 98: 'Mango flat beak leafhopper', 99: 'Rhytidodera bowrinii white', 100: 'Sternochetus frigidus', 101: 'Cicadellidae'}
---------------------------------------------------------------

Inputs
-------------------------
name:images
tensor:Float[1, 3, 736, 736]
---------------------------------------------------------------

Outputs
-------------------------
name:output0
tensor:Float[1, 106, 11109]
---------------------------------------------------------------

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Onnx_Demo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string model_path;string classer_path;public string[] class_names;public int class_num;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;int input_height;int input_width;float ratio_height;float ratio_width;InferenceSession onnx_session;int box_num;float conf_threshold;float nms_threshold;/// <summary>/// 选择图片/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);textBox1.Text = "";pictureBox2.Image = null;}/// <summary>/// 推理/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}button2.Enabled = false;pictureBox2.Image = null;textBox1.Text = "";Application.DoEvents();Mat image = new Mat(image_path);//图片缩放int height = image.Rows;int width = image.Cols;Mat temp_image = image.Clone();if (height > input_height || width > input_width){float scale = Math.Min((float)input_height / height, (float)input_width / width);OpenCvSharp.Size new_size = new OpenCvSharp.Size((int)(width * scale), (int)(height * scale));Cv2.Resize(image, temp_image, new_size);}ratio_height = (float)height / temp_image.Rows;ratio_width = (float)width / temp_image.Cols;Mat input_img = new Mat();Cv2.CopyMakeBorder(temp_image, input_img, 0, input_height - temp_image.Rows, 0, input_width - temp_image.Cols, BorderTypes.Constant, 0);//Cv2.ImShow("input_img", input_img);//输入TensorTensor<float> input_tensor = new DenseTensor<float>(new[] { 1, 3, 736, 736 });for (int y = 0; y < input_img.Height; y++){for (int x = 0; x < input_img.Width; x++){input_tensor[0, 0, y, x] = input_img.At<Vec3b>(y, x)[0] / 255f;input_tensor[0, 1, y, x] = input_img.At<Vec3b>(y, x)[1] / 255f;input_tensor[0, 2, y, x] = input_img.At<Vec3b>(y, x)[2] / 255f;}}List<NamedOnnxValue> input_container = new List<NamedOnnxValue>{NamedOnnxValue.CreateFromTensor("images", input_tensor)};//推理dt1 = DateTime.Now;var ort_outputs = onnx_session.Run(input_container).ToArray();dt2 = DateTime.Now;float[] data = Transpose(ort_outputs[0].AsTensor<float>().ToArray(), 4 + class_num, box_num);float[] confidenceInfo = new float[class_num];float[] rectData = new float[4];List<DetectionResult> detResults = new List<DetectionResult>();for (int i = 0; i < box_num; i++){Array.Copy(data, i * (class_num + 4), rectData, 0, 4);Array.Copy(data, i * (class_num + 4) + 4, confidenceInfo, 0, class_num);float score = confidenceInfo.Max(); // 获取最大值int maxIndex = Array.IndexOf(confidenceInfo, score); // 获取最大值的位置int _centerX = (int)(rectData[0] * ratio_width);int _centerY = (int)(rectData[1] * ratio_height);int _width = (int)(rectData[2] * ratio_width);int _height = (int)(rectData[3] * ratio_height);detResults.Add(new DetectionResult(maxIndex,class_names[maxIndex],new Rect(_centerX - _width / 2, _centerY - _height / 2, _width, _height),score));}//NMSCvDnn.NMSBoxes(detResults.Select(x => x.Rect), detResults.Select(x => x.Confidence), conf_threshold, nms_threshold, out int[] indices);detResults = detResults.Where((x, index) => indices.Contains(index)).ToList();//绘制结果Mat result_image = image.Clone();foreach (DetectionResult r in detResults){Cv2.PutText(result_image, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);Cv2.Rectangle(result_image, r.Rect, Scalar.Red, thickness: 2);}pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";button2.Enabled = true;}/// <summary>///窗体加载/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Form1_Load(object sender, EventArgs e){model_path = "model/yolo11-pest-detection.onnx";//创建输出会话,用于输出模型读取信息SessionOptions options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行// 创建推理模型类,读取模型文件onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径input_height = 736;input_width = 736;box_num = 11109;conf_threshold = 0.25f;nms_threshold = 0.5f;classer_path = "model/lable.txt";class_names = File.ReadAllLines(classer_path, Encoding.UTF8);class_num = class_names.Length;image_path = "test_img/1.jpg";pictureBox1.Image = new Bitmap(image_path);}/// <summary>/// 保存/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button3_Click(object sender, EventArgs e){if (pictureBox2.Image == null){return;}Bitmap output = new Bitmap(pictureBox2.Image);SaveFileDialog sdf = new SaveFileDialog();sdf.Title = "保存";sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";if (sdf.ShowDialog() == DialogResult.OK){switch (sdf.FilterIndex){case 1:{output.Save(sdf.FileName, ImageFormat.Jpeg);break;}case 2:{output.Save(sdf.FileName, ImageFormat.Png);break;}case 3:{output.Save(sdf.FileName, ImageFormat.Bmp);break;}case 4:{output.Save(sdf.FileName, ImageFormat.Emf);break;}case 5:{output.Save(sdf.FileName, ImageFormat.Exif);break;}case 6:{output.Save(sdf.FileName, ImageFormat.Gif);break;}case 7:{output.Save(sdf.FileName, ImageFormat.Icon);break;}case 8:{output.Save(sdf.FileName, ImageFormat.Tiff);break;}case 9:{output.Save(sdf.FileName, ImageFormat.Wmf);break;}}MessageBox.Show("保存成功,位置:" + sdf.FileName);}}private void pictureBox1_DoubleClick(object sender, EventArgs e){ShowNormalImg(pictureBox1.Image);}private void pictureBox2_DoubleClick(object sender, EventArgs e){ShowNormalImg(pictureBox2.Image);}public void ShowNormalImg(Image img){if (img == null) return;frmShow frm = new frmShow();frm.Width = Screen.PrimaryScreen.Bounds.Width;frm.Height = Screen.PrimaryScreen.Bounds.Height;if (frm.Width > img.Width){frm.Width = img.Width;}if (frm.Height > img.Height){frm.Height = img.Height;}bool b = frm.richTextBox1.ReadOnly;Clipboard.SetDataObject(img, true);frm.richTextBox1.ReadOnly = false;frm.richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));frm.richTextBox1.ReadOnly = b;frm.ShowDialog();}public unsafe float[] Transpose(float[] tensorData, int rows, int cols){float[] transposedTensorData = new float[tensorData.Length];fixed (float* pTensorData = tensorData){fixed (float* pTransposedData = transposedTensorData){for (int i = 0; i < rows; i++){for (int j = 0; j < cols; j++){int index = i * cols + j;int transposedIndex = j * rows + i;pTransposedData[transposedIndex] = pTensorData[index];}}}}return transposedTensorData;}}public class DetectionResult{public DetectionResult(int ClassId, string Class, Rect Rect, float Confidence){this.ClassId = ClassId;this.Confidence = Confidence;this.Rect = Rect;this.Class = Class;}public string Class { get; set; }public int ClassId { get; set; }public float Confidence { get; set; }public Rect Rect { get; set; }}}

下载

源码下载


http://www.ppmy.cn/news/1541045.html

相关文章

大模型之三十二-语音合成TTS(coqui) 之二 fine-tune

在 大模型之三十-语音合成TTS(coqui)[shichaog CSDN]中提到了xttsv2的fine-tune。 数据情况&#xff1a; 我是从bilibili up主小Lin说提取了一些视频&#xff0c;然后进行了重新的fine-tune。 训练结果 如下图所示&#xff0c;上面波形幅度较大的是xttsv2原始模型的结果&am…

Synchronized是非公平锁吗?那么如何体现?锁能降级吗?

1. synchronized 是非公平锁吗&#xff1f; 是的&#xff0c;synchronized 是非公平锁。 什么是非公平锁&#xff1f; 非公平锁意味着线程获取锁的顺序并不一定按照先后到达的顺序&#xff0c;也就是说&#xff0c;后来的线程有可能插队&#xff0c;优先获取锁。这种机制可以…

python的Django框架入门

Django是一个开源的Python Web开发框架&#xff0c;遵循MTV&#xff08;Model-Template-View&#xff0c;模型-模板-视图&#xff09;架构&#xff0c;是一个全功能的Web框架&#xff0c;Django的模块化设计允许开发者通过添加自定义插件、使用第三方库等方式来增强其功能。 一…

后端接收参数的几种常用注解

目录 一、RequestParam 二、RequestBody 三、PathVariable 四、RequestHeader 五、RequestAttribute 六、RequestPart 七、Valid 一、RequestParam 1.作用 用于将请求中的 查询参数 或 表单参数 绑定到方法的参数上。支持 GET 和 POST 请求。 2.使用方法 GetMappin…

第十四届单片机嵌入式蓝桥杯

一、CubeMx配置 &#xff08;1&#xff09;LED配置 &#xff08;1&#xff09;LED灯里面用到了SN74HC573ADWR锁存器&#xff0c;这个锁存器有一个LE引脚,这个是我们芯片的锁存引脚&#xff08;使能引脚&#xff09;&#xff0c;由PD2这个端口来控制的 &#xff08;2&#xff…

多模态技术的协同表现:从文本生成、语音合成到口型同步综合测评

本文是针对多模态对话系统核心技术栈的使用效果和网络测评整理。 测评内容基于用户体验&#xff0c;侧重于从使用者角度出发&#xff0c;讨论实际操作中的体验感受&#xff0c;如技术的易用性、输出效果如文本的连贯性、语音的自然度、口型同步的准确性等。不涉及具体算法架构…

[C#][winform]基于yolov5的驾驶员抽烟打电话安全带检测系统C#源码+onnx模型+评估指标曲线+精美GUI界面

【重要说明】 该系统以opencvsharp作图像处理,onnxruntime做推理引擎&#xff0c;使用CPU进行推理&#xff0c;适合有显卡或者没有显卡windows x64系统均可&#xff0c;不支持macOS和Linux系统&#xff0c;不支持x86的windows操作系统。由于采用CPU推理&#xff0c;要比GPU慢。…

【ubuntu18.04】ubuntu18.04安装vmware-tools之后立即适应窗口为灰色,窗口不会自适应

问题描述 ubuntu18.04安装vmware-tools 点击查看&#xff0c;看到立即适应窗口为灰色 解决方案 关闭虚拟机 编辑此虚拟机 设置 注意 取消勾选拉伸模式 查看可以看到所有选项可选择 注意&#xff1a; 不要全屏的时候查看&#xff0c;全屏看到的立即适应窗口就会显示…