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

相关推荐
青风975 小时前
YOLO-World:实时开放词汇对象检测(YOLO-World: Real-Time Open-Vocabulary Object Detection)
人工智能·yolo·目标检测
三天不学习7 小时前
YOLO + .NET 10 快速入门:从零搭建实时目标检测应用
yolo·目标检测·.net
深度知识积累AI7 小时前
RK3588 部署 YOLO26 目标检测:从训练到 NPU 推理
yolo
C_c..7 小时前
#YOLOv11 目标检测训练结果怎么看?一文看懂 Precision、Recall、mAP 指标
人工智能·yolo·目标检测·机器学习·计算机视觉·目标跟踪
笑脸惹桃花7 小时前
目标检测:YOLOv12环境配置,超详细,适合0基础纯小白
深度学习·yolo·目标检测·目标跟踪·yolov12
动物园猫1 天前
睡岗检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
筱筱木2 天前
从标注优化到全流程落地:Python+YOLOv8 打造 YOLO模型训练 一体化平台
yolo
AI街潜水的八角2 天前
基于YOLO26电池顶盖焊接缺陷检测系统1:电池顶盖焊接缺陷检测数据集说明(含下载链接)
人工智能·深度学习·yolo·目标跟踪
Ricky05532 天前
搭载实时 FPGA 处理系统的航天器上用于海上监视的超分辨率YOLO目标检测技术(意大利2026年研究)
yolo·目标检测·fpga开发
YOLO数据集集合2 天前
智慧农业|农田作物杂草识别数据集|航拍巡检|YOLO实例分割|深度学习训练集|智能除草视觉数据集
人工智能·深度学习·yolo·目标检测·无人机