C# OpenVino Yolov8 Seg 分割

news/2024/7/10 23:42:18 标签: openvino, YOLO, 人工智能, C#目标分割

效果

 

项目

代码

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


http://www.niftyadmin.cn/n/5012489.html

相关文章

Codeforces Round 895 (Div. 3) C. Non-coprime Split

找到数字 x x x的因子 k k k&#xff0c;构造出 k , x − k k,x-k k,x−k即可。 因为 x C 1 ∗ k x C_1 * k xC1​∗k , x − k ( C 1 − 1 ) ∗ k x - k (C_1 - 1) * k x−k(C1​−1)∗k&#xff0c;保证其最小公因数不为 1 1 1如果没有因子&#xff0c;即这个数字是质数&…

YOLOV7改进-对小目标有效的BiFormer注意力机制

如果要在sppcspc后面加一个注意力模块BiFormer 1、 普通加 1、models-common.py搜这个模块 2、直接加 3、全部复制下来&#xff0c;models新建文件 4、common导入进来 5、填上一层的输出通道数 2、模块里加 难点&#xff1a;在配置文件找到对应的这一块 yolov7x中 1、…

包管理工具--》发布一个自己的npm包

包管理工具系列文章目录 一、包管理工具--》npm的配置及使用&#xff08;一&#xff09; 二、包管理工具--》npm的配置及使用&#xff08;二&#xff09; 三、包管理工具--》发布一个自己的npm包 四、包管理工具--》yarn的配置及使用 五、包管理工具--》其他包管理器之cnpm…

十二、集合(5)

本章概要 for-in 和迭代器 适配器方法惯用法 本章小结 简单集合分类 for-in和迭代器 到目前为止&#xff0c;for-in 语法主要用于数组&#xff0c;但它也适用于任何 Collection 对象。实际上在使用 ArrayList 时&#xff0c;已经看到了一些使用它的示例&#xff0c;下面是它…

企业架构LNMP学习笔记22

防盗链原理和实现。 域名A的资源文件&#xff0c;经常被域名B直接调用访问。 而用户经常访问域名B&#xff0c;看到的资源&#xff08;图片等&#xff09;以为是域名B的&#xff0c;实际则是域名A的。 但是域名A没有获得任何收益&#xff0c;却要给域名B来源的访问消耗服务器…

《Effective STL》读书笔记(二):vector和string

vector 和 string 优先于动态分配数组 当使用new动态分配内存时&#xff0c;我们需要关注以下内容 必须保证动态分配的内存会被delete&#xff0c;否则会造成资源泄露必须确保使用了正确的delete形式。如果分配了单个对象&#xff0c;则必须使用delete&#xff1b;如果分配了…

『力扣每日一题08』验证回文串

一、题目 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后&#xff0c;短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。 字母和数字都属于字母数字字符。 给你一个字符串 s&#xff0c;如果它是 回文串 &#xff0c;返回 true &#xff1b;否…

Mybatis日期检索格式报错

问题复现 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String ##…