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

相关推荐
这张生成的图像能检测吗10 小时前
(论文速读)SCNN:用于交通场景理解的空间CNN
人工智能·深度学习·目标检测·计算机视觉·道路线检测
꧁执笔小白꧂1 天前
详细pyenv+venv+pip安装ultralytics
yolo·venv·pyenv·ultralytics
延卿1 天前
nnDetection:基于 PyTorch 的目标检测框架
人工智能·pytorch·目标检测
前网易架构师-高司机1 天前
带标注的细胞有丝分裂数据集数据集,识别率95.9%,304张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·细胞·显微镜·生物·有丝分裂·显微
动物园猫2 天前
PCB表面缺陷目标检测数据集:6类别、3,500张图像 | 目标检测
人工智能·目标检测·计算机视觉
前网易架构师-高司机2 天前
带标注的移动通信基站信号塔,可识别塔架和塔身数据集,识别率93.5%,1008张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·数据集·通信基站·信号塔·塔架·塔身
AI棒棒牛2 天前
第11讲 | 目标检测任务:边界框、IoU、类别与置信度
人工智能·yolo·目标检测·yolo26
深度研习笔记2 天前
OpenCV工业视觉实战06|ROI区域截取+局部精细化检测,锁定作业区域、屏蔽无效背景,大幅提升YOLO检测准确率
人工智能·opencv·yolo
FriendshipT2 天前
Ultralytics:解读CBLinear模块
人工智能·pytorch·python·深度学习·目标检测
ShallWeL2 天前
Orin 上用 Docker 跑通目标检测功能
人工智能·目标检测·docker·容器