C# OpenCvSharp Yolov8 Cls 图像分类

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
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;

namespace OpenCvSharp_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;
        Mat image;

        ClasResult result_pro;
        Mat result_mat;
        Mat result_image;
        Mat result_mat_to_float;

        Net opencv_net;
        Mat BN_image;

        float[] result_array;

        int max_image_length;
        Mat max_image;
        Rect roi;

        KeyValuePair<string, float> result_cls;
        StringBuilder sb = new StringBuilder();

        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";

            result_pro = new ClasResult(classer_path);

            //初始化网络类,读取本地模型
            opencv_net = CvDnn.ReadNetFromOnnx(model_path);

            result_array = new float[1000];
        }

        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);
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            //缩放图片
            max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
            max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            roi = new Rect(0, 0, image.Cols, image.Rows);
            image.CopyTo(new Mat(max_image, roi));

            //数据归一化处理
            BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            dt1 = DateTime.Now;
            //模型推理,读取推理结果
            result_mat = opencv_net.Forward();
            dt2 = DateTime.Now;

            //将推理结果转为float数据类型
            result_mat_to_float = new Mat(1, 1000, MatType.CV_32F, result_mat.Data);

            //将数据读取到数组中
            result_mat_to_float.GetArray<float>(out result_array);

            result_cls = result_pro.process_result(result_array);

            result_image = result_pro.draw_result(result_cls, image.Clone());

            if (!result_image.Empty())
            {
                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                sb.Clear();
                sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
                sb.AppendLine("------------------------------");

                sb.AppendLine(string.Format("{0}:{1}"
                    , result_cls.Key
                    , result_cls.Value.ToString("0.00")));

                textBox1.Text = sb.ToString();
            }
            else
            {
                textBox1.Text = "无信息";
            }

        }
    }
}

Demo下载

相关推荐
王哈哈^_^9 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
深度学习lover15 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
Eric.Lee202119 小时前
yolo v5 开源项目
人工智能·yolo·目标检测·计算机视觉
极智视界1 天前
无人机场景数据集大全「包含数据标注+划分脚本+训练脚本」 (持续原地更新)
算法·yolo·目标检测·数据集标注·分割算法·算法训练·无人机场景数据集
深度学习lover1 天前
<项目代码>YOLOv8 夜间车辆识别<目标检测>
人工智能·yolo·目标检测·计算机视觉·表情识别·夜间车辆识别
小哥谈2 天前
源码解析篇 | YOLO11:计算机视觉领域的新突破 !对比YOLOv8如何 ?
人工智能·深度学习·神经网络·yolo·目标检测·机器学习·计算机视觉
挂科边缘2 天前
基于YOLOv8 Web的安全帽佩戴识别检测系统的研究和设计,数据集+训练结果+Web源码
前端·人工智能·python·yolo·目标检测·计算机视觉
小张贼嚣张2 天前
yolov8涨点系列之HiLo注意力机制引入
深度学习·yolo·机器学习
CV-King2 天前
yolov11-cpp-opencv-dnn推理onnx模型
人工智能·opencv·yolo·计算机视觉·dnn
辛勤的程序猿2 天前
YOLO即插即用---PConv
深度学习·yolo·计算机视觉