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

相关推荐
大学生毕业题目18 小时前
毕业项目推荐:99-基于yolov8/yolov5/yolo11的肾结石检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·肾结石检测
老兵发新帖19 小时前
基于Label Studio的视频标注与YOLO模型训练全流程指南
python·yolo·音视频
音沐mu.21 小时前
【47】飞机数据集(有v5/v8模型)/YOLO飞机检测
yolo·目标检测·数据集·飞机数据集·飞机检测
听风吹雨yu1 天前
YoloV11的pt模型转rknn模型适用于RK3588等系列
linux·python·yolo·开源·rknn
Byron Loong1 天前
【机器视觉】YOLO中 P,R,F1曲线的含义
yolo·目标跟踪
weixin_456904271 天前
基于Yolov11,Paddle,Zxing进行目标检测文本条码识别的环境配置记录
yolo·目标检测·paddle
智驱力人工智能1 天前
矿场轨道异物AI监测系统 构建矿山运输安全的智能感知防线 轨道异物检测 基于YOLO的轨道异物识别算法 地铁隧道轨道异物实时预警技术
人工智能·opencv·算法·安全·yolo·边缘计算
AI街潜水的八角1 天前
基于深度学习神经网络YOLOv5目标检测的安全帽识别系统
深度学习·神经网络·yolo
mahtengdbb11 天前
Yolov8结合CAA-HSFPN网络实现汽车漆面缺陷检测与分类的完整实战指南
yolo·分类·数据挖掘
peixiuhui1 天前
EdgeGateway 快速开始手册-常见问题
mqtt·网关·yolo·modbus·边缘计算网关·edgegateway·开源网关程序