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

相关推荐
MM_MS15 分钟前
Halcon图像采集助手、ROI操作和画图、ROI实现区域与轮廓之间的相互转换、区域的交集差集取反
图像处理·人工智能·数码相机·算法·目标检测·计算机视觉·视觉检测
uWvvJWldyYUZ39 分钟前
WCA 水循环算法优化 BP 神经网络(WCA - BPNN)回归预测在电厂运行数据中的应用
yolo
小Tomkk42 分钟前
PyTorch +YOLO + Label Studio + 图像识别 深度学习项目实战 (一)
人工智能·pytorch·yolo
奔跑吧邓邓子3 小时前
YOLOv8目标检测:从理论到实战的飞跃之旅
yolo·目标检测·目标跟踪·理论到实战
WJSKad12353 小时前
基于改进YOLO11的超市商品与电子设备多类别目标检测方法C3k2-ConvAttn
人工智能·目标检测·计算机视觉
Piar1231sdafa5 小时前
野猪目标检测与识别_基于YOLO11-Attention模型的改进实现
人工智能·目标检测·计算机视觉
2501_941333105 小时前
乒乓球比赛场景目标检测与行为分析研究
人工智能·目标检测·计算机视觉
岑梓铭5 小时前
YOLO深度学习(计算机视觉)一很有用!!(进一步加快训练速度的操作)
人工智能·深度学习·神经网络·yolo·计算机视觉
巫婆理发2226 小时前
目标检测+YOLO
人工智能·yolo·目标检测
培根芝士7 小时前
把PP-OCRv5_server模型转换为OpenVINO格式
人工智能·openvino