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下载

相关推荐
量子位8 分钟前
LIama 4 发布重夺开源第一!DeepSeek 同等代码能力但参数减一半,一张 H100 就能跑,还有两万亿参数超大杯
人工智能·deepseek
量子位12 分钟前
米哈游蔡浩宇新作 iPhone 实机演示:10 分钟就被 AI 小美撩到脸红,她的命运由我拯救
人工智能·aigc
蚝油菜花22 分钟前
清华联合DeepSeek推出奖励模型新标杆!DeepSeek-GRM:让AI学会自我批评,推理性能越跑越强
人工智能·开源
Yan-英杰41 分钟前
【百日精通JAVA | SQL篇 | 第四篇】约束
java·服务器·开发语言·数据库·人工智能·sql·mysql
掘金安东尼1 小时前
记录 Chatgpt 辅助离线配置环境,我就像是个小丑🤡🤡🤡
人工智能·程序员·llm
羡江0071 小时前
机器学习(概述)
人工智能·深度学习·机器学习
Listennnn1 小时前
EM算法到底是什么东东
人工智能·算法·机器学习
Cl_rown去掉l变成C1 小时前
第P10周:Pytorch实现车牌识别
人工智能·pytorch·python
Coovally AI模型快速验证1 小时前
GPT-4o从语义分割到深度图生成,大模型狂潮下的计算机视觉:技术进步≠替代危机
人工智能·gpt·神经网络·目标检测·计算机视觉·目标跟踪
机器鱼2 小时前
C++计算机视觉实战:100个实际案例分析
人工智能·计算机视觉