C# OpenVino Yolov8 Seg 分割

效果

项目

代码

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;

namespace OpenVino_Yolov8_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        String startupPath;
        string classer_path;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        String model_path;
        Core core;
        Mat image;

        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 = "";
            image = new Mat(image_path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            // 配置图片数据
            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;

            // 读取推理结果
            det_result_array = core.read_infer_result<float>("output0", 8400 * 116);
            proto_result_array = core.read_infer_result<float>("output1", 32 * 160 * 160);

            SegmentationResult result_pro= new SegmentationResult(classer_path, factors);
            Mat result_image = result_pro.draw_result(result_pro.process_result(det_result_array, proto_result_array), image.Clone());

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\yolov8n-seg.onnx";
            core = new Core(model_path, "CPU");
            classer_path = "yolov8-detect-lable.txt";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            core.delet();
        }
    }
}

完整Demo下载

相关推荐
struggle202539 分钟前
DeepSpeed 是一个深度学习优化库,使分布式训练和推理变得简单、高效和有效
人工智能·深度学习
猎嘤一号1 小时前
使用 PyTorch 和 TensorBoard 实时可视化模型训练
人工智能·pytorch·python
从零开始学习人工智能2 小时前
多模型协同:基于 SAM 分割 + YOLO 检测 + ResNet 分类的工业开关状态实时监控方案
人工智能·yolo·分类
s153352 小时前
12-OPENCV ROCKX项目 人脸拍照
人工智能·opencv·计算机视觉
alasnot3 小时前
BERT情感分类
人工智能·深度学习·bert
只有左边一个小酒窝3 小时前
(九)现代循环神经网络(RNN):从注意力增强到神经架构搜索的深度学习演进
人工智能·rnn·深度学习
UQI-LIUWJ4 小时前
论文略读:REEF: Representation Encoding Fingerprints for Large Language Models
人工智能·语言模型·自然语言处理
强盛小灵通专卖员4 小时前
基于YOLOv12的电力高空作业安全检测:为电力作业“保驾护航”,告别安全隐患!
人工智能·深度学习·安全·yolo·核心期刊·计算机期刊
万米商云4 小时前
AI推荐系统演进史:从协同过滤到图神经网络与强化学习的融合
人工智能·深度学习·神经网络