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

相关推荐
goomind5 小时前
YOLOv8实战木材缺陷识别
人工智能·yolo·目标检测·缺陷检测·pyqt5·木材缺陷识别
吾门6 小时前
YOLO入门教程(三)——训练自己YOLO11实例分割模型并预测【含教程源码+一键分类数据集 + 故障排查】
yolo·分类·数据挖掘
向阳逐梦9 小时前
ROS机器视觉入门:从基础到人脸识别与目标检测
人工智能·目标检测·计算机视觉
Suyuoa12 小时前
附录2-pytorch yolov5目标检测
python·深度学习·yolo
OpenVINO 中文社区14 小时前
实战精选|如何使用 OpenVINO™ 在 ElectronJS 中创建桌面应用程序
人工智能·openvino
思通数科多模态大模型1 天前
10大核心应用场景,解锁AI检测系统的智能安全之道
人工智能·深度学习·安全·目标检测·计算机视觉·自然语言处理·数据挖掘
红色的山茶花1 天前
YOLOv8-ultralytics-8.2.103部分代码阅读笔记-block.py
笔记·深度学习·yolo
sp_fyf_20241 天前
【大语言模型】ACL2024论文-18 MINPROMPT:基于图的最小提示数据增强用于少样本问答
人工智能·深度学习·神经网络·目标检测·机器学习·语言模型·自然语言处理
思通数科AI全行业智能NLP系统1 天前
六大核心应用场景,解锁AI检测系统的智能安全之道
图像处理·人工智能·深度学习·安全·目标检测·计算机视觉·知识图谱
非自律懒癌患者1 天前
Transformer中的Self-Attention机制如何自然地适应于目标检测任务
人工智能·算法·目标检测