• C# OpenVino Yolov8 Seg 分割


    目录

    效果

    模型信息

    项目

    代码

    下载 

    exe程序说明


    效果

     

    模型信息

    Model Properties
    -------------------------
    date:2023-09-07T17:11:46.798385
    description:Ultralytics YOLOv8n-seg model trained on coco.yaml
    author:Ultralytics
    task:segment
    license:AGPL-3.0 https://ultralytics.com/license
    version:8.0.172
    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, 116, 8400]
    name:output1
    tensor:Float[1, 32, 160, 160]
    ---------------------------------------------------------------

    项目

    代码

    // 图片缩放
    Mat image = new Mat(image_path);
    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[] det_result_array = new float[8400 * 116];
    float[] proto_result_array = new float[32 * 160 * 160];
    float[] factors = new float[4];
    factors[0] = factors[1] = (float)(max_image_length / 640.0);
    factors[2] = image.Rows;
    factors[3] = image.Cols;

    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;

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

    下载 

    完整Demo下载

    exe程序下载

    exe程序说明

    1、运行路径中不能包含中文,否则模型加载不成功,程序无法运行

    2、如果提示“无法加载 DLL“OpenVinoSharpExtern.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”,请使用depends22_x64等工具查找缺失的依赖库并添加。

    也可下载源码自己本地编译,下载地址:https://download.csdn.net/download/lw112190/88806702

    实在自己搞不定的,可是使用Sdcb.OpenVINO,地址:https://lw112190.blog.csdn.net/article/details/136038410

  • 相关阅读:
    【数据结构】堆(C语言)
    自己动手从零写桌面操作系统GrapeOS系列教程——6.电脑启动过程介绍
    Docker 容器化(初学者的分享)
    [树形dp]Hanging Hearts Codeforces1740E
    ViewPager2 滑动图片浏览
    【Python百日进阶-Web开发-音频】Day702 - librosa安装及模块一览表
    biopython----bio.PDB
    程序员在工作之余如何保障收入?兼职才是硬道理!
    心知天气api接口怎么用?
    通过工作组在DAO中展开更有效的治理
  • 原文地址:https://blog.csdn.net/lw112190/article/details/132753726