C# OpenVino Yolov8 Detect 目标检测

效果

项目

代码

复制代码
using OpenCvSharp;
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 static System.Net.Mime.MediaTypeNames;

namespace OpenVino_Yolov8_Detect
{
    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;

        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 Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\yolov8n.onnx";
            classer_path = startupPath + "\\det_lable.txt";
            core = new Core(model_path, "CPU");
        }

        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[8400 * 84];
            float[] factors = new float[2];
            factors = new float[2];
            factors[0] = factors[1] = (float)(max_image_length / 640.0);

            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", 8400 * 84);
            
            DetectionResult result_pro = new DetectionResult(classer_path, factors);
            Mat result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

            textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            core.delet();
        }
    }
}

完整Demo下载

相关推荐
灰灰勇闯IT13 小时前
DVPP 视频预处理:YOLO 视频检测的瓶颈与解法
yolo·音视频
前网易架构师-高司机18 小时前
带标注的螺丝、螺栓、垫圈缺陷识别数据集,包含缺陷里包含生锈和划痕,1291张图,支持yolo,coco json,voc xml,文末有模型训练代码。
yolo·数据集·缺陷·螺栓·螺丝·垫圈·
动物园猫18 小时前
桥梁损伤目标检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·目标检测
忆~遂愿19 小时前
从文字应答到具象共情:Agent 交互的底层革新
人工智能·深度学习·目标检测·microsoft·机器学习·ar·交互
cjzcjl19 小时前
Android端侧部署人脸识别工程性能优化笔记——NV21数据变速变换
yolo·yuv
小白|1 天前
elec-ops-inspection:电力行业算子应用场景实战
目标检测
koharu1231 天前
PointRCNN 精解:从原始点云到三维框的两阶段检测
人工智能·深度学习·目标检测·3d·三维点云
张道宁1 天前
从零开始训练YOLO手机检测模型:完整实战教程
python·yolo
星越华夏1 天前
计算机视觉:YOLOv12安装环境
人工智能·yolo·计算机视觉
羊羊小栈1 天前
AI赋能电力巡检:智能故障预警系统
人工智能·yolo·目标检测·毕业设计·大作业