C# Onnx Yolov8 Fire Detect 火焰识别,火灾检测

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

目录

效果

​模型信息

项目

​代码

下载 


效果

模型信息

Model Properties
-------------------------
author:Ultralytics
task:detect
license:AGPL-3.0 https://ultralytics.com/license
version:8.0.172
stride:32
batch:1
imgsz:[640, 640]
names:{0: 'Fire'}
---------------------------------------------------------------

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

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

项目

代码

/// <summary>
/// 结果绘制
/// </summary>
/// <param name="result">识别结果</param>
/// <param name="image">绘制图片</param>
/// <returns></returns>
public Mat draw_result(Result result, Mat image)
{
    // 将识别结果绘制到图片上
    for (int i = 0; i < result.length; i++)
    {
        //Console.WriteLine(result.rects[i]);
        Cv2.Rectangle(image, result.rects[i], new Scalar(0, 0, 255), 2, LineTypes.Link8);
        
        Cv2.Rectangle(image, new Point(result.rects[i].TopLeft.X-1, result.rects[i].TopLeft.Y - 20),
            new Point(result.rects[i].BottomRight.X, result.rects[i].TopLeft.Y), new Scalar(0, 0, 255), -1);
        
        Cv2.PutText(image, result.classes[i] + "-" + result.scores[i].ToString("0.00"),
            new Point(result.rects[i].X, result.rects[i].Y - 4),
            HersheyFonts.HersheySimplex, 0.6, new Scalar(0, 0, 0), 1);
    }
    return image;
}

using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.ML.OnnxRuntime;
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 OpenCvSharp;
using static System.Net.Mime.MediaTypeNames;

namespace Onnx_Yolov8_Fire_Detect
{
    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_image;

        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        List<NamedOnnxValue> input_ontainer;
        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        Tensor<float> result_tensors;
        Result result;

        StringBuilder sb=new StringBuilder();
        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\fire.onnx";
            classer_path = startupPath + "\\lable.txt";
            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            // 设置为CPU上运行
            options.AppendExecutionProvider_CPU(0);
            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
            // 输入Tensor
            input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();
        }

        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;
            }

            // 配置图片数据
            image = new Mat(image_path);
            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 * 1];
            float[] factors = new float[2];
            factors[0] = factors[1] = (float)(max_image_length / 640.0);

            // 将图片转为RGB通道
            Mat image_rgb = new Mat();
            Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
            Mat resize_image = new Mat();
            Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));

            // 输入Tensor
            for (int y = 0; y < resize_image.Height; y++)
            {
                for (int x = 0; x < resize_image.Width; x++)
                {
                    input_tensor[0, 0, y, x] = resize_image.At<Vec3b>(y, x)[0] / 255f;
                    input_tensor[0, 1, y, x] = resize_image.At<Vec3b>(y, x)[1] / 255f;
                    input_tensor[0, 2, y, x] = resize_image.At<Vec3b>(y, x)[2] / 255f;
                }
            }

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_ontainer.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_ontainer);

            dt2 = DateTime.Now;

            // 将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            // 读取第一个节点输出并转为Tensor数据
            result_tensors = results_onnxvalue[0].AsTensor<float>();

            result_array = result_tensors.ToArray();

            resize_image.Dispose();
            image_rgb.Dispose();

            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 = "无信息";
            }
        }
    }
}

下载 

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

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

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

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

相关文章

  • 【yolov5&yolov7&yolov8火焰和烟雾检测】

    YOLOv5训练好的火焰检测模型,并包含2000张标注好的火焰和烟雾数据集,标签格式为xml和txt两种,类别名为fire, 有QT界面 采用pytrch框架,代码是python的 火灾检测数据集-1 YOLOv3火焰识别训练模型: https://download.csdn.net/download/zhiqingAI/85438269 yolov5火焰识别训练模型+数据集: https

    2024年04月28日
    浏览(32)
  • YOLOv8/YOLOv7/YOLOv5-火灾检测、烟雾检测系统-界面+视频实时检测+数据集(算法-毕业设计)

    本项目通过yolov8/yolov7/yolov5训练自己的数据集,并开发可视化界面,实现了一个火灾烟雾实时检测系统,操作视频和效果展示如下: 【yolov8/yolov7/yolov5火灾烟雾检测系统-界面+视频实时检测+数据集(原创算法-毕业设计)】 https://www.bilibili.com/video/BV1FG41127H3/?share_source=copy_webvd_sou

    2024年02月04日
    浏览(42)
  • YOLOv8优化:独家创新(SC_C_Detect)检测头结构创新,实现涨点 | 检测头新颖创新系列

      💡💡💡 本文独家改进: 独家创新(SC_C_Detect)检测头结构创新,适合科研创新度十足,强烈推荐 SC_C_Detect |   亲测在多个数据集能够实现大幅涨点 💡💡💡Yolov8魔术师,独家首发创新(原创),适用于Yolov5、Yolov7、Yolov8等各个Yolo系列,专栏文章提供每一步步骤和源码

    2024年02月07日
    浏览(35)
  • YOLOv8优化:独家创新(Partial_C_Detect)检测头结构创新,实现涨点 | 检测头新颖创新系列

     💡💡💡 本文独家改进: 独家创新(Partial_C_Detect)检测头结构创新,适合科研创新度十足,强烈推荐 Partial_C_Detect  |   亲测在多个数据集能够实现大幅涨点 💡💡💡Yolov8魔术师,独家首发创新(原创),适用于Yolov5、Yolov7、Yolov8等各个Yolo系列,专栏文章提供每一步步骤

    2024年02月05日
    浏览(36)
  • Windows10+Python+Yolov8+ONNX图片缺陷识别,并在原图中标记缺陷,有onnx模型则无需配置,无需训练。

    目录 一、训练自己数据集的YOLOv8模型  1.博主电脑配置 2.深度学习GPU环境配置  3.yolov8深度学习环境准备 4.准备数据集 二、Python+Onnx模型进行图像缺陷检测,并在原图中标注 1、模型转换 2、查看模型结构 3、修改输入图片的尺寸 4、 图像数据归一化 5、模型推理 6、推理结果筛

    2024年02月12日
    浏览(32)
  • YOLOv8 人体姿态估计(关键点检测) python推理 && ONNX RUNTIME C++部署

    目录   1、下载权重 ​编辑2、python 推理 3、转ONNX格式 4、ONNX RUNTIME C++ 部署 utils.h utils.cpp detect.h detect.cpp main.cpp CmakeList.txt 我这里之前在做实例分割的时候,项目已经下载到本地,环境也安装好了,只需要下载pose的权重就可以 输出:   用netron查看一下:  如上图所是,YOLO

    2024年02月07日
    浏览(35)
  • 用于增强现实的实时可穿带目标检测:基于YOLOv8进行ONNX转换和部署

    点击蓝字 关注我们 关注并星标 从此不迷路 计算机视觉研究院 公众号ID | 计算机视觉研究院 学习群 | 扫码在主页获取加入方式 计算机视觉研究院专栏 Column of Computer Vision Institute 今天给大家介绍了一种在增强现实(AR)环境中使用机器学习(ML)进行实时目标检测的软件体

    2024年02月04日
    浏览(37)
  • 基于BP神经网络的火焰识别,基于BP神经网络的火灾识别

    背影 BP神经网络的原理 BP神经网络的定义 BP神经网络的基本结构 BP神经网络的神经元 BP神经网络的激活函数, BP神经网络的传递函数 代码链接:基于BP神经网络的火焰识别,基于BP神经网络的火灾识别资源-CSDN文库 https://download.csdn.net/download/abc991835105/88215777 神经网络参数 基于

    2024年02月11日
    浏览(29)
  • C# OpenCvSharp Yolov8 Pose 姿态识别

    目录 效果 项目 模型信息 代码 下载  VS2022 .net framework 4.8 OpenCvSharp 4.8 Microsoft.ML.OnnxRuntime 1.16.2 Model Properties ------------------------- date:2023-09-07T17:11:43.091306 description:Ultralytics YOLOv8n-pose model trained on /usr/src/app/ultralytics/datasets/coco-pose.yaml author:Ultralytics kpt_shape:[17, 3] task:pose l

    2024年02月07日
    浏览(28)
  • 防护服穿戴检测识别算法 yolov8

    防护服穿戴检测识别系统基于yolov8网络模型图片数据识别训练,算法模型自动完成对现场人员是否按照要求穿戴行为实时分析。YOLOv8 算法的核心特性和改动可以归结为如下:提供了一个全新的 SOTA 模型,包括 P5 640 和 P6 1280 分辨率的目标检测网络和基于 YOLACT 的实例分割模型

    2024年02月03日
    浏览(55)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包