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

相关推荐
arron889911 小时前
YOLOv8n-pose 模型使用
人工智能·深度学习·yolo
飞翔的佩奇17 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
Coovally AI模型快速验证20 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
yzx99101321 小时前
小程序开发APP
开发语言·人工智能·python·yolo
飞翔的佩奇21 小时前
【完整源码+数据集+部署教程】二维码与查找模式检测系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·二维码与查找模式检测
yzx9910132 天前
Yolov模型的演变
人工智能·算法·yolo
lxmyzzs2 天前
【图像算法 - 16】庖丁解牛:基于YOLO12与OpenCV的车辆部件级实例分割实战(附完整代码)
人工智能·深度学习·opencv·算法·yolo·计算机视觉·实例分割
Coovally AI模型快速验证3 天前
SOD-YOLO:基于YOLO的无人机图像小目标检测增强方法
人工智能·yolo·目标检测·机器学习·计算机视觉·目标跟踪·无人机
飞翔的佩奇3 天前
【完整源码+数据集+部署教程】食品分类与实例分割系统源码和数据集:改进yolo11-AggregatedAttention
python·yolo·计算机视觉·数据集·yolo11·食品分类与实例分割
Virgil1393 天前
用PaddleDetection套件训练自己的数据集,PP-YOLO-SOD训练全流程
yolo