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

相关推荐
极智视界36 分钟前
无人机场景 - 目标检测数据集 - 环岛路况车辆检测数据集下载
yolo·目标检测·数据集·voc·coco·算法训练·无人机场景环岛路况车辆检测
Liue6123123142 分钟前
基于YOLOv5的柑橘类叶片识别任务实现与优化
yolo
duyinbi75171 小时前
基于YOLOv10n的人脸口罩检测模型_结合FocalModulation改进技术_提升口罩检测精度与效率_1
yolo
Liue612312312 小时前
YOLOv8-CSMHSA 狮子检测与识别模型训练与评估_1
yolo
棒棒的皮皮2 小时前
【深度学习】YOLO学习资源之官方文档&Darknet文档
深度学习·学习·yolo·计算机视觉
irisMoon062 小时前
yolo v5识别人物情绪
yolo
Coovally AI模型快速验证3 小时前
计算机视觉的 2026:从“堆算力”竞赛,到“省算力”智慧
人工智能·深度学习·算法·yolo·计算机视觉·无人机
JicasdC123asd3 小时前
基于YOLOv10n-MambaOut的行李箱检测与识别系统_深度学习_Python_源码_详解
python·深度学习·yolo
Katecat9966312 小时前
YOLOv8-Seg改进系列真空喷嘴质量检测与分类任务实现
yolo·分类·数据挖掘
JOBkiller12314 小时前
基于YOLOv8-Seg-RepHGNetV2的银耳缺陷检测与分类实现
yolo·分类·数据挖掘