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

下载

源码下载

相关推荐
知来者逆几秒前
探索大型语言模型在文化常识方面的理解能力与局限性
人工智能·gpt·深度学习·语言模型·自然语言处理·chatgpt·llm
Python极客之家37 分钟前
基于深度学习的乳腺癌分类识别与诊断系统
人工智能·深度学习·分类
mftang1 小时前
TMR传感器的实现原理和特性介绍
人工智能
无情大菜刀1 小时前
C# 雷赛运动控制器 SMC304 新建工程
c#
吃什么芹菜卷1 小时前
深度学习:词嵌入embedding和Word2Vec
人工智能·算法·机器学习
chnyi6_ya1 小时前
论文笔记:Online Class-Incremental Continual Learning with Adversarial Shapley Value
论文阅读·人工智能
中杯可乐多加冰1 小时前
【AI驱动TDSQL-C Serverless数据库技术实战】 AI电商数据分析系统——探索Text2SQL下AI驱动代码进行实际业务
c语言·人工智能·serverless·tdsql·腾讯云数据库
萱仔学习自我记录3 小时前
PEFT库和transformers库在NLP大模型中的使用和常用方法详解
人工智能·机器学习
BulingQAQ6 小时前
论文阅读:PET/CT Cross-modal medical image fusion of lung tumors based on DCIF-GAN
论文阅读·深度学习·生成对抗网络·计算机视觉·gan
hsling松子6 小时前
使用PaddleHub智能生成,献上浓情国庆福
人工智能·算法·机器学习·语言模型·paddlepaddle