C# PaddleInference.PP-HumanSeg 人像分割 替换背景色

效果

项目

VS2022+.net4.8+OpenCvSharp4+Sdcb.PaddleInference

包含4个分割模型

modnet-hrnet_w18

modnet-mobilenetv2

ppmatting-hrnet_w18-human_512

ppmattingv2-stdc1-human_512

代码

using OpenCvSharp;
using Sdcb.PaddleInference;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using YamlDotNet.RepresentationModel;
 
namespace PaddleInference.PP_HumanSeg_人像分割_替换背景色
{
    public class PaddleSeger : IDisposable
    {
        private readonly PaddlePredictor _p;
        public PaddleSeger(PaddleConfig config, string deployYamlPath)
            : this(config.CreatePredictor(), deployYamlPath)
        {
        }
        public PaddleSeger(PaddlePredictor predictor, string deployYamlPath)
        {
            _p = predictor;
            var yaml = new YamlStream();
            using (FileStream ymlFile = File.OpenRead(deployYamlPath))
                yaml.Load(new StreamReader(ymlFile));
            YamlDocument doc = yaml.Documents[0];
        }
        public PaddleSeger(string modelDir) : this(PaddleConfig.FromModelFiles(
            Path.Combine(modelDir, "model.pdmodel"),
            Path.Combine(modelDir, "model.pdiparams")).Apply(PaddleDevice.Mkldnn()),
            Path.Combine(modelDir, "deploy.yaml"))
        {
        }
        public Mat Run(Mat src)
        {
            Mat dest;
            using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
            {
                Mat bgr = src.CvtColor(ColorConversionCodes.BGR2RGB);
                Mat normalized = Normalize(bgr);
                float[] data = ExtractMat(normalized);
                normalized.Dispose();
                bgr.Dispose();
                input.Shape = new int[] { 1, 3, src.Rows, src.Cols };
                input.SetData(data);
            }
            if (!_p.Run())
            {
                throw new Exception("PaddlePredictor(Seger) run failed.");
            }
            using (PaddleTensor output = _p.GetOutputTensor(_p.OutputNames[0]))
            {
                float[] rawData = output.GetData<float>();
                byte[] data = Array.ConvertAll(rawData, d => (byte)(d * 255));
                GCHandle gc = GCHandle.Alloc(data, GCHandleType.Pinned);
                dest = new Mat(output.Shape[2], output.Shape[3], MatType.CV_8UC1, gc.AddrOfPinnedObject());
                gc.Free();
            }
            return dest;
        }
        private static Mat Normalize(Mat src)
        {
            Mat normalized = new Mat();
            src.ConvertTo(normalized, MatType.CV_32FC3, 1.0 / 255);
            Mat[] bgr = normalized.Split();
            float[] scales = new[] { 2.0f, 2.0f, 2.0f };
            float[] means = new[] { 0.5f, 0.5f, 0.5f };
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 * scales[i], (0.0 - means[i]) * scales[i]);
            }
            normalized.Dispose();
            Mat dest = new Mat();
            Cv2.Merge(bgr, dest);
            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }
            return dest;
        }
        internal static float[] ExtractMat(Mat src)
        {
            int rows = src.Rows;
            int cols = src.Cols;
            float[] result = new float[rows * cols * 3];
            GCHandle resultHandle = default;
            try
            {
                resultHandle = GCHandle.Alloc(result, GCHandleType.Pinned);
                IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
                for (int i = 0; i < src.Channels(); ++i)
                {
                    Mat dest = new Mat(rows, cols, MatType.CV_32FC1, resultPtr + i * rows * cols * sizeof(float));
                    Cv2.ExtractChannel(src, dest, i);
                    dest.Dispose();
                }
            }
            finally
            {
                resultHandle.Free();
            }
            return result;
        }
        public void Dispose()
        {
            _p.Dispose();
        }
    }
}

下载

源码下载

相关推荐
极客代码1 分钟前
【Python TensorFlow】进阶指南(续篇二)
开发语言·人工智能·python·深度学习·tensorflow
池央20 分钟前
丹摩征文活动 | 搭建 CogVideoX-2b详细教程:用短短6秒展现创作魅力
人工智能
慕容复之巅27 分钟前
基于MATLAB刻度线表盘识别系统
图像处理·计算机视觉·matlab
QYR市场调研32 分钟前
5G时代的关键元件:射频微波MLCCs市场前景广阔
人工智能
AI小白日记42 分钟前
深入探索AutoDL平台:深度学习GPU算力最佳选择
人工智能·深度学习·gpu算力
一尘之中1 小时前
元宇宙及其技术
人工智能
电子手信1 小时前
AI知识库在行业应用中的未来趋势与案例分析
大数据·人工智能·自然语言处理·数据挖掘
三维重建--小博主2 小时前
人群计数制作私有数据集教程-----自用
python·opencv·计算机视觉
严文文-Chris2 小时前
【卷积神经网络】
人工智能·神经网络·cnn
不脱发的牧码人3 小时前
C#中Task和Thread的全解析
c#·多线程·thread·task