C# OpenVino Yolov8 Cls 分类

效果

项目

代码

复制代码
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;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        String model_path;
        string classer_path;
        StringBuilder sb = new StringBuilder();
        Core core;
        Mat image;
        ClasResult result_pro;
        KeyValuePair<string, float> result_cls;
        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;
            }

            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[] result_array = new float[1000];

            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;

            // 读取推理结果
            result_array = core.read_infer_result<float>("output0", 1000);

            result_cls = result_pro.process_result(result_array);
            Mat result_image = result_pro.draw_result(result_cls, 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-cls.onnx";
            classer_path = startupPath + "\\yolov8-cls-lable.txt";
            core = new Core(model_path, "CPU");
            result_pro = new ClasResult(classer_path);
        }

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

Demo下载

相关推荐
jay神6 小时前
一文讲清楚YOLOv26模型
人工智能·深度学习·yolo·机器学习·计算机视觉
qq_25294131686 小时前
无人机目标检测数据集(UAV Detection Dataset)| 无人机检测 反无人机 低空安防5001期
人工智能·yolo·目标检测·计算机视觉·视觉检测·无人机·反无人机
YOLO数据集集合10 小时前
蚊子视觉验证数据集 | 蚊虫识别 疟疾防控 病媒生物 细粒度分类 YOLO格式 深度学习数据集
深度学习·yolo·分类·数据集·蚊子分类·蚊虫分类·蚊虫识别
java1234_小锋11 小时前
YOLO26 计算机视觉 - YOLO26图片与视频推理 & 摄像头与实时检测
人工智能·yolo·计算机视觉·机器视觉·yolo26
YOLO数据集集合1 天前
蚊子目标检测数据集 | 蚊虫检测 病媒生物 智能硬件 轻量化模型 目标检测 YOLO格式 深度学习数据集9015期
深度学习·yolo·目标检测·计算机视觉·分类·智能硬件·蚊子识别
YOLO数据集集合1 天前
煤炭质量目标检测数据集 、| 煤炭检测 工业视觉 质量分级 异物检测 煤炭异物8005期
人工智能·yolo·目标检测·计算机视觉·分类·煤炭检测数据集·异物数据集
qq_25294131681 天前
窗户清洁检测数据集 | 窗户清洁检测 建筑运维 二分类任务 YOLO格式 深度学习数据集9019期
运维·人工智能·深度学习·yolo·目标检测·分类·窗户清晰度
YOLO数据集集合1 天前
尼帕果实目标检测数据集 | 尼帕果实检测 农业视觉 热带作物 目标检测 YOLO格式9013期
人工智能·yolo·目标检测·机器学习·计算机视觉·农业果实
qq_25294131681 天前
航拍垃圾堆检测数据集 | 航拍垃圾检测 固废管理 无人机巡检 环保监测9025期
yolo·目标检测·计算机视觉·分类·无人机·垃圾检测·垃圾
qq_25294131681 天前
建筑物缺陷目标检测数据集 | 建筑裂缝检测 房屋缺陷 结构损伤 目标检测9022期
人工智能·yolo·目标检测·计算机视觉·视觉检测·建筑裂缝·建筑外立面