C#中部署PaddleSeg的ONNX模型

今日工作总遇到需要C#中布置分割模型的需求,在此记录一下。

首先先看待部署模型的格式

以下是代码,输出是将得到的结果按二维数组输出

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
//using SixLabors.Fonts;
//using System.Drawing;

namespace Microsoft.ML.OnnxRuntime.Sample
{
    class Program
    {
        public static void Main(string[] args)
        {
            onnxtest(args);

            static int[,] onnxtest(string[] args)
            {
                // Read paths
                string modelFilePath = @"D:\ZihaoLi\ONNXDeploy\output.onnx";
                string imageFilePath = @"D:\ZihaoLi\ONNXDeploy\Camera0.bmp";



                // Read image
                using Image<Rgb24> image = SixLabors.ImageSharp.Image.Load<Rgb24>(imageFilePath);

                // Resize image

                image.Mutate(x => x.Resize(512, 512));

                // Preprocess image
                Tensor<float> input = new DenseTensor<float>(new[] { 1, 3, image.Height, image.Width });

                for (int y = 0; y < image.Height; y++)
                {
                    image.ProcessPixelRows(im =>
                    {
                        var pixelSpan = im.GetRowSpan(y);
                        for (int x = 0; x < image.Width; x++)
                        {
                            input[0, 0, y, x] = ((pixelSpan[x].R) / 255f - 0.5f) / 0.5f;

                            input[0, 1, y, x] = ((pixelSpan[x].G) / 255f - 0.5f) / 0.5f;

                            input[0, 2, y, x] = ((pixelSpan[x].B) / 255f - 0.5f) / 0.5f;

                        }
                    });

                }
                // Setup inputs and outputs
                var inputs = new List<NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("x", input)
            };

                // Run inference
                using var session = new InferenceSession(modelFilePath);
                using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);
                var Test = results.ToList()[0].AsTensor<long>().ToArray<long>();

                int[,] res = new int[512, 512]; //RES为结果数组
                int tmp = 0;
                foreach (int i in Test)
                {
                    res[tmp / 512, tmp % 512] = i;
                    tmp++;
                }

                for(int i = 0; i < 512; i++)
                {
                    for(int j = 0; j < 512; j++)
                    {
                        if(res[i,j] == 1)
                        {
                            Console.WriteLine((i, j));
                        }
                    }
                }

                return (res);
           
            }


        }
    }
}

 因为第一次用C#部署,对于如何对图片进行处理,包括结果图mask与原图的叠加还不是很熟悉,如果有懂这方面的朋友欢迎指教~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值