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

相关推荐
2501_9413331013 小时前
数字识别与检测_YOLOv3_C3k2改进模型解析
人工智能·yolo·目标跟踪
xsc-xyc17 小时前
RuntimeError: Dataset ‘/data.yaml‘ error ❌ ‘_lz
人工智能·深度学习·yolo·计算机视觉·视觉检测
张3蜂17 小时前
我希望做的是识别身份证正反面,我需要标注多少张图片?
yolo
AAD5558889919 小时前
YOLOv8-MAN-Faster电容器缺陷检测:七类组件识别与分类系统
yolo·分类·数据挖掘
AI浩20 小时前
YOLO-IOD:面向实时增量目标检测
yolo·目标检测·目标跟踪
wfeqhfxz258878221 小时前
YOLOv8-BiFPN鸟巢目标检测与识别实战教程
yolo·目标检测·目标跟踪
Katecat9966321 小时前
基于YOLOv8和MAFPN的骆驼目标检测系统实现
人工智能·yolo·目标检测
ZCXZ12385296a1 天前
YOLOv8_HSPAN_机器人视觉系统中的球体目标检测与追踪_1
yolo·目标检测·机器人
BestSongC1 天前
基于 YOLO11 的智能行人摔倒检测系统
人工智能·深度学习·yolo·目标检测
2501_941333101 天前
【工业视觉检测】基于YOLOv8的皮带输送机关键部件检测与识别系统完整实现
人工智能·yolo·视觉检测