• C# OpenCvSharp 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]
    ---------------------------------------------------------------

    项目

    代码

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

    factors[0] = factors[1] = (float)(max_image_length / 640.0);

    //数据归一化处理
    BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);

    //配置图片输入数据
    opencv_net.SetInput(BN_image);

    dt1 = DateTime.Now;
    //模型推理,读取推理结果
    result_mat = opencv_net.Forward();
    dt2 = DateTime.Now;

    //将推理结果转为float数据类型
    result_mat_to_float = new Mat(8400, 84, MatType.CV_32F, result_mat.Data);

    //将数据读取到数组中
    result_mat_to_float.GetArray(out result_array);

    1. using OpenCvSharp;
    2. using OpenCvSharp.Dnn;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Drawing;
    8. using System.Linq;
    9. using System.Text;
    10. using System.Windows.Forms;
    11. namespace OpenCvSharp_Yolov8_Demo
    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. string classer_path;
    23. DateTime dt1 = DateTime.Now;
    24. DateTime dt2 = DateTime.Now;
    25. string model_path;
    26. Mat image;
    27. DetectionResult result_pro;
    28. Mat result_mat;
    29. Mat result_image;
    30. Mat result_mat_to_float;
    31. Net opencv_net;
    32. Mat BN_image;
    33. float[] result_array;
    34. float[] factors;
    35. int max_image_length;
    36. Mat max_image;
    37. Rect roi;
    38. Result result;
    39. StringBuilder sb = new StringBuilder();
    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 + "\\yolov8-detect-lable.txt";
    45. //初始化网络类,读取本地模型
    46. opencv_net = CvDnn.ReadNetFromOnnx(model_path);
    47. result_array = new float[8400 * 84];
    48. factors = new float[2];
    49. }
    50. private void button1_Click(object sender, EventArgs e)
    51. {
    52. OpenFileDialog ofd = new OpenFileDialog();
    53. ofd.Filter = fileFilter;
    54. if (ofd.ShowDialog() != DialogResult.OK) return;
    55. pictureBox1.Image = null;
    56. image_path = ofd.FileName;
    57. pictureBox1.Image = new Bitmap(image_path);
    58. textBox1.Text = "";
    59. image = new Mat(image_path);
    60. pictureBox2.Image = null;
    61. }
    62. private void button2_Click(object sender, EventArgs e)
    63. {
    64. if (image_path == "")
    65. {
    66. return;
    67. }
    68. //缩放图片
    69. max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    70. max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    71. roi = new Rect(0, 0, image.Cols, image.Rows);
    72. image.CopyTo(new Mat(max_image, roi));
    73. factors[0] = factors[1] = (float)(max_image_length / 640.0);
    74. //数据归一化处理
    75. BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);
    76. //配置图片输入数据
    77. opencv_net.SetInput(BN_image);
    78. dt1 = DateTime.Now;
    79. //模型推理,读取推理结果
    80. result_mat = opencv_net.Forward();
    81. dt2 = DateTime.Now;
    82. //将推理结果转为float数据类型
    83. result_mat_to_float = new Mat(8400, 84, MatType.CV_32F, result_mat.Data);
    84. //将数据读取到数组中
    85. result_mat_to_float.GetArray<float>(out result_array);
    86. result_pro = new DetectionResult(classer_path, factors);
    87. result = result_pro.process_result(result_array);
    88. result_image = result_pro.draw_result(result, image.Clone());
    89. if (!result_image.Empty())
    90. {
    91. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    92. sb.Clear();
    93. sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
    94. sb.AppendLine("------------------------------");
    95. for (int i = 0; i < result.length; i++)
    96. {
    97. sb.AppendLine(string.Format("{0}:{1},({2},{3},{4},{5})"
    98. , result.classes[i]
    99. , result.scores[i].ToString("0.00")
    100. , result.rects[i].TopLeft.X
    101. , result.rects[i].TopLeft.Y
    102. , result.rects[i].BottomRight.X
    103. , result.rects[i].BottomRight.Y
    104. ));
    105. }
    106. textBox1.Text = sb.ToString();
    107. }
    108. else
    109. {
    110. textBox1.Text = "无信息";
    111. }
    112. }
    113. }
    114. }

    下载 

    Demo下载

  • 相关阅读:
    【Java校招面试】实战面经(十)
    《微服务架构设计模式》第二章
    微服务从代码到k8s部署应有尽有系列(三、鉴权)
    spring-boot入门之如何利用idea创建一个spring-boot项目
    Java面试经验,Java实习生应届生面试笔试题整理
    万字总结:分布式系统的38个知识点
    JavaOOP-类、对象、方法、变量作用域及JavaDoc注释
    2022年下半年(软考高级)信息系统项目管理师报名条件
    Python-tracemalloc-跟踪内存分配
    3BHE022291R0101 PCD230A 专注于制造卓越人工智能
  • 原文地址:https://blog.csdn.net/lw112190/article/details/132962855