C# OpenCvSharp Yolov8 Detect 目标检测

这篇具有很好参考价值的文章主要介绍了C# OpenCvSharp Yolov8 Detect 目标检测。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

效果

模型信息

项目

代码

下载 


效果

yolo 8 c#,C#人工智能实践,C# 目标检测,c#,计算机视觉,人工智能,目标检测,机器学习,YOLO

模型信息

Model Properties
-------------------------
date:2023-09-05T13:17:15.396588
description:Ultralytics YOLOv8n model trained on coco.yaml
author:Ultralytics
task:detect
license:AGPL-3.0 https://ultralytics.com/license
version:8.0.170
stride:32
batch:1
imgsz:[640, 640]
names:{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
---------------------------------------------------------------

Inputs
-------------------------
name:images
tensor:Float[1, 3, 640, 640]
---------------------------------------------------------------

Outputs
-------------------------
name:output0
tensor:Float[1, 84, 8400]
---------------------------------------------------------------

项目

yolo 8 c#,C#人工智能实践,C# 目标检测,c#,计算机视觉,人工智能,目标检测,机器学习,YOLO

代码

//缩放图片
max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
roi = new Rect(0, 0, image.Cols, image.Rows);
image.CopyTo(new Mat(max_image, roi));

factors[0] = factors[1] = (float)(max_image_length / 640.0);

//数据归一化处理
BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);

//配置图片输入数据
opencv_net.SetInput(BN_image);

dt1 = DateTime.Now;
//模型推理,读取推理结果
result_mat = opencv_net.Forward();
dt2 = DateTime.Now;

//将推理结果转为float数据类型
result_mat_to_float = new Mat(8400, 84, MatType.CV_32F, result_mat.Data);

//将数据读取到数组中
result_mat_to_float.GetArray<float>(out result_array);

using OpenCvSharp;
using OpenCvSharp.Dnn;
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;

namespace OpenCvSharp_Yolov8_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        string classer_path;

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;

        DetectionResult result_pro;
        Mat result_mat;
        Mat result_image;
        Mat result_mat_to_float;

        Net opencv_net;
        Mat BN_image;

        float[] result_array;
        float[] factors;

        int max_image_length;
        Mat max_image;
        Rect roi;

        Result result;
        StringBuilder sb = new StringBuilder();

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\yolov8n.onnx";
            classer_path = startupPath + "\\yolov8-detect-lable.txt";

            //初始化网络类,读取本地模型
            opencv_net = CvDnn.ReadNetFromOnnx(model_path);

            result_array = new float[8400 * 84];
            factors = new float[2];
        }

        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);
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            //缩放图片
            max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
            max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            roi = new Rect(0, 0, image.Cols, image.Rows);
            image.CopyTo(new Mat(max_image, roi));

            factors[0] = factors[1] = (float)(max_image_length / 640.0);

            //数据归一化处理
            BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            dt1 = DateTime.Now;
            //模型推理,读取推理结果
            result_mat = opencv_net.Forward();
            dt2 = DateTime.Now;

            //将推理结果转为float数据类型
            result_mat_to_float = new Mat(8400, 84, MatType.CV_32F, result_mat.Data);

            //将数据读取到数组中
            result_mat_to_float.GetArray<float>(out result_array);

            result_pro = new DetectionResult(classer_path, factors);

            result = result_pro.process_result(result_array);

            result_image = result_pro.draw_result(result, image.Clone());

            if (!result_image.Empty())
            {
                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                sb.Clear();
                sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
                sb.AppendLine("------------------------------");
                for (int i = 0; i < result.length; i++)
                {
                    sb.AppendLine(string.Format("{0}:{1},({2},{3},{4},{5})"
                        , result.classes[i]
                        , result.scores[i].ToString("0.00")
                        , result.rects[i].TopLeft.X
                        , result.rects[i].TopLeft.Y
                        , result.rects[i].BottomRight.X
                        , result.rects[i].BottomRight.Y
                        ));
                }
                textBox1.Text = sb.ToString();
            }
            else
            {
                textBox1.Text = "无信息";
            }

        }
    }
}

下载 

Demo下载文章来源地址https://www.toymoban.com/news/detail-786690.html

到了这里,关于C# OpenCvSharp Yolov8 Detect 目标检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包