当前位置: 首页 > news >正文

C# Onnx 用于边缘检测的轻量级密集卷积神经网络LDC

效果

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Onnx_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;string model_path;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;int inpHeight = 360;int inpWidth = 640;int outHeight = 360;int outWidth = 640;Mat image;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;float[] result_array;StringBuilder sb = new StringBuilder();private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);}private void Form1_Load(object sender, EventArgs e){startupPath = Application.StartupPath + "\\model\\";model_path = startupPath + "LDC_640x360.onnx";// 创建输出会话options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行// 创建推理模型类,读取本地模型文件onnx_session = new InferenceSession(model_path, options);// 输入Tensorinput_tensor = new DenseTensor<float>(new[] { 1, 3, inpHeight, inpWidth });// 创建输入容器input_ontainer = new List<NamedOnnxValue>();}private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;Application.DoEvents();//图片image = new Mat(image_path);Mat resize_image = new Mat();Cv2.Resize(image, resize_image, new OpenCvSharp.Size(inpWidth, inpHeight));// 输入Tensorfor (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];input_tensor[0, 1, y, x] = resize_image.At<Vec3b>(y, x)[1];input_tensor[0, 2, y, x] = resize_image.At<Vec3b>(y, x)[2];}}//将 input_tensor 放入一个输入参数的容器,并指定名称input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input_image", input_tensor));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_ontainer);dt2 = DateTime.Now;Mat average_image = Mat.Zeros(image.Rows, image.Cols, MatType.CV_32FC1);Mat fuse_image = new Mat(image.Rows, image.Cols, MatType.CV_8UC1);results_onnxvalue = result_infer.ToArray();for (int i = 0; i < results_onnxvalue.Length; i++){result_tensors = results_onnxvalue[i].AsTensor<float>();result_array = result_tensors.ToArray();Mat result = new Mat(outHeight, outWidth, MatType.CV_32FC1, result_array);Mat TmpExp = new Mat();Cv2.Exp(-result, TmpExp);Mat mask = 1.0 / (1.0 + TmpExp);double min_value, max_value;Cv2.MinMaxLoc(mask, out min_value, out max_value);mask = (mask - min_value) * 255.0 / (max_value - min_value + 1e-12);mask.ConvertTo(mask, MatType.CV_8UC1);Cv2.BitwiseNot(mask, mask);Cv2.Resize(mask, mask, new OpenCvSharp.Size(image.Cols, image.Rows));Cv2.Accumulate(mask,average_image, mask);  //将所有图像叠加fuse_image = mask;}average_image = average_image / (float)results_onnxvalue.Length; //求出平均图像average_image.ConvertTo(average_image, MatType.CV_8UC1);result_image = average_image.Clone();if (!result_image.Empty()){//Cv2.ImShow("LDC-average_image", average_image);//Cv2.ImShow("LDC-fuse_image", fuse_image);pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());sb.Clear();sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");textBox1.Text = sb.ToString();}else{textBox1.Text = "无信息";}}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载

可执行程序exe包0积分下载

源码下载

相关文章:

  • Kafka - 监控工具 Kafka Eagle:实时洞察Kafka集群的利器
  • P2107 小Z的AK计划
  • 如何读懂深度学习python项目,以`Multi-label learning from single positive label`为例
  • Fourier分析导论——第2章——Fourier级数的基本属性(E.M. Stein R. Shakarchi)
  • 一篇博客读懂顺序表 —— Sequence-List
  • FIFO 位宽转换
  • 力扣740. 删除并获得点数(动态规划)
  • Debian或Ubuntu静态交叉编译arm和aarch64
  • miniconda快速安装
  • 我的云栖大会之旅:见证云计算创新的15年
  • 使用springboot对Elasticsearch 进行索引的增、删、改、查
  • 企业网络带宽使用情况检查技巧
  • Vite+Vue3项目全局引入scss文件
  • 【蓝桥杯选拔赛真题44】python小蓝晨跑 青少年组蓝桥杯python 选拔赛STEMA比赛真题解析
  • 从用户角度出发,如何优化大数据可视化体验|北京蓝蓝UI设计公司
  • 【mysql】环境安装、服务启动、密码设置
  • CentOS7 安装JDK
  • golang 发送GET和POST示例
  • k8s 面向应用开发者的基础命令
  • Laravel5.4 Queues队列学习
  • node和express搭建代理服务器(源码)
  • Python学习之路13-记分
  • React的组件模式
  • 复习Javascript专题(四):js中的深浅拷贝
  • 互联网大裁员:Java程序员失工作,焉知不能进ali?
  • 用Python写一份独特的元宵节祝福
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (4)事件处理——(7)简单事件(Simple events)
  • (6)添加vue-cookie
  • (poj1.2.1)1970(筛选法模拟)
  • (搬运以学习)flask 上下文的实现
  • (第61天)多租户架构(CDB/PDB)
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • *** 2003
  • ./configure,make,make install的作用
  • .net framework profiles /.net framework 配置
  • .NET 反射 Reflect
  • .NET开发者必备的11款免费工具
  • .Net转Java自学之路—基础巩固篇十三(集合)
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • [ vulhub漏洞复现篇 ] GhostScript 沙箱绕过(任意命令执行)漏洞CVE-2019-6116
  • [4.9福建四校联考]
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [Android] 240204批量生成联系人,短信,通话记录的APK
  • [BZOJ1010] [HNOI2008] 玩具装箱toy (斜率优化)
  • [Firefly-Linux] RK3568修改控制台DEBUG为普通串口UART
  • [GPT]Andrej Karpathy微软Build大会GPT演讲(上)--GPT如何训练
  • [hdu 4552] 怪盗基德的挑战书
  • [hive]中的字段的数据类型有哪些