• C# OpenVino Yolov8 Detect 目标检测


    目录

    效果​​​​​​​

    模型信息

    项目 

    代码

    下载 


    效果

    模型信息

    Model Properties
    -------------------------
    date:2023-09-05T13:17:15.396588
    description:Ultralytics YOLOv8n model trained on coco.yaml
    author:Ultralytics
    task:detect
    license:AGPL-3.0 https://ultralytics.com/license
    version:8.0.170
    stride:32
    batch:1
    imgsz:[640, 640]
    names:{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
    ---------------------------------------------------------------

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

    Outputs
    -------------------------
    name:output0
    tensor:Float[1, 84, 8400]
    ---------------------------------------------------------------

    项目 

    VS2022

    .net framework 4.8

    OpenCvSharp 4.8

    openvino_2023.0.1.11005

    代码

    // 图片缩放
    int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    Rect roi = new Rect(0, 0, image.Cols, image.Rows);
    image.CopyTo(new Mat(max_image, roi));

    float[] result_array = new float[8400 * 84];
    float[] factors = new float[2];
    factors = new float[2];
    factors[0] = factors[1] = (float)(max_image_length / 640.0);

    byte[] image_data = max_image.ImEncode(".bmp");
    //存储byte的长度
    ulong image_size = Convert.ToUInt64(image_data.Length);
    // 加载推理图片数据
    core.load_input_data("images", image_data, image_size, 1);
    // 模型推理
    dt1 = DateTime.Now;
    core.infer();
    dt2 = DateTime.Now;
    // 读取推理结果
    result_array = core.read_infer_result("output0", 8400 * 84);

    DetectionResult result_pro = new DetectionResult(classer_path, factors);
    Mat result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());

    pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

    textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

    1. using OpenCvSharp;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.ComponentModel;
    5. using System.Data;
    6. using System.Drawing;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Windows.Forms;
    10. using static System.Net.Mime.MediaTypeNames;
    11. namespace OpenVino_Yolov8_Detect
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. string image_path = "";
    21. String startupPath;
    22. DateTime dt1 = DateTime.Now;
    23. DateTime dt2 = DateTime.Now;
    24. String model_path;
    25. string classer_path;
    26. StringBuilder sb = new StringBuilder();
    27. Core core;
    28. Mat image;
    29. private void button1_Click(object sender, EventArgs e)
    30. {
    31. OpenFileDialog ofd = new OpenFileDialog();
    32. ofd.Filter = fileFilter;
    33. if (ofd.ShowDialog() != DialogResult.OK) return;
    34. pictureBox1.Image = null;
    35. image_path = ofd.FileName;
    36. pictureBox1.Image = new Bitmap(image_path);
    37. textBox1.Text = "";
    38. image = new Mat(image_path);
    39. }
    40. private void Form1_Load(object sender, EventArgs e)
    41. {
    42. startupPath = System.Windows.Forms.Application.StartupPath;
    43. model_path = startupPath + "\\yolov8n.onnx";
    44. classer_path = startupPath + "\\det_lable.txt";
    45. core = new Core(model_path, "CPU");
    46. }
    47. private void button2_Click(object sender, EventArgs e)
    48. {
    49. if (image_path == "")
    50. {
    51. return;
    52. }
    53. // 配置图片数据
    54. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    55. Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    56. Rect roi = new Rect(0, 0, image.Cols, image.Rows);
    57. image.CopyTo(new Mat(max_image, roi));
    58. float[] result_array = new float[8400 * 84];
    59. float[] factors = new float[2];
    60. factors = new float[2];
    61. factors[0] = factors[1] = (float)(max_image_length / 640.0);
    62. byte[] image_data = max_image.ImEncode(".bmp");
    63. //存储byte的长度
    64. ulong image_size = Convert.ToUInt64(image_data.Length);
    65. // 加载推理图片数据
    66. core.load_input_data("images", image_data, image_size, 1);
    67. // 模型推理
    68. dt1 = DateTime.Now;
    69. core.infer();
    70. dt2 = DateTime.Now;
    71. // 读取推理结果
    72. result_array = core.read_infer_result<float>("output0", 8400 * 84);
    73. DetectionResult result_pro = new DetectionResult(classer_path, factors);
    74. Mat result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());
    75. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    76. textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    77. }
    78. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    79. {
    80. core.delet();
    81. }
    82. }
    83. }

    下载 

    完整Demo下载

  • 相关阅读:
    HttpClient
    ROS之rviz文件的加载和保存
    聊聊单点登录(SSO)中的CAS认证
    我是用了OpenCVForUnity中的webCamTextureToMat的Example,后续要用霍夫圆,就使用OpenCvSharp,两个插件里的Mat无法转换
    VRRP 虚拟路由器冗余协议的解析和配置
    【MATLAB源码-第72期】基于matlab的OFDM-IM索引调制系统在高斯,瑞利,莱斯信道误码率对比,对比传统OFDM系统。
    【JDBC笔记】数据库连接池与c3p0连接池创建连接
    【CSS】你还记得组合选择器怎么用吗?
    MySQL锁(乐观锁、悲观锁、多粒度锁)
    redis中stream数据结构使用详解——redis最适合做消息队列的数据结构
  • 原文地址:https://blog.csdn.net/lw112190/article/details/132734714