C# OpenVINO 百度PaddleSeg实时人像抠图PP-MattingV2

目录

效果

项目

代码

下载


C# OpenVINO 百度PaddleSeg实时人像抠图PP-MattingV2

效果

项目

代码

using OpenCvSharp;

using Sdcb.OpenVINO;

using System;

using System.Diagnostics;

using System.Drawing;

using System.Security.Cryptography;

using System.Text;

using System.Windows.Forms;

namespace C__OpenVINO_Demo

{

public partial class Form1 : Form

{

Bitmap bmp;

string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

string img = "";

string model_path;

CompiledModel cm;

InferRequest ir;

StringBuilder sb = new StringBuilder();

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

model_path = "models/ppmattingv2-stdc1-human_512/model.pdmodel";

Model rawModel = OVCore.Shared.ReadModel(model_path);

var ad = OVCore.Shared.AvailableDevices;

Console.WriteLine("可用设备");

foreach (var item in ad)

{

Console.WriteLine(item);

}

cm = OVCore.Shared.CompileModel(rawModel, "CPU");

ir = cm.CreateInferRequest();

img = "1.jpg";

bmp = new Bitmap(img);

pictureBox1.Image = new Bitmap(img);

}

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = fileFilter;

if (ofd.ShowDialog() != DialogResult.OK) return;

pictureBox1.Image = null;

img = ofd.FileName;

bmp = new Bitmap(img);

pictureBox1.Image = new Bitmap(img);

textBox1.Text = "";

}

private void button2_Click(object sender, EventArgs e)

{

if (img == "") { return; }

textBox1.Text = "";

sb.Clear();

pictureBox2.Image = null;

Application.DoEvents();

Mat src = new Mat(img);

Stopwatch stopwatch = new Stopwatch();

Mat resize_image = new Mat();

Cv2.Resize(src, resize_image, new OpenCvSharp.Size(512, 512));

Mat normalized = Common.Normalize(resize_image);

float[] input_tensor_data = Common.ExtractMat(normalized);

Tensor input_img = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 512, 512));

ir.Inputs[0] = input_img;

double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;

stopwatch.Restart();

ir.Run();

double inferTime = stopwatch.Elapsed.TotalMilliseconds;

stopwatch.Restart();

Tensor output_0 = ir.Outputs[0];

float[] mask = output_0.GetData<float>().ToArray();

Mat mask_mat = new Mat(512, 512, MatType.CV_32FC1, mask);

mask_mat *= 255;

mask_mat.ConvertTo(mask_mat, MatType.CV_8UC1);

Cv2.Resize(mask_mat, mask_mat, new OpenCvSharp.Size(512, 512));

double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;

stopwatch.Stop();

double totalTime = preprocessTime + inferTime + postprocessTime;

sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");

sb.AppendLine($"Infer: {inferTime:F2}ms");

sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");

sb.AppendLine($"Total: {totalTime:F2}ms");

textBox1.Text = sb.ToString();

Cv2.ImShow("mask", mask_mat);

//string s = mask_mat.Dump();

Cv2.Merge(new Mat[] { mask_mat, mask_mat, mask_mat }, mask_mat);

Mat dst = new Mat();

Cv2.BitwiseAnd(resize_image, mask_mat, dst);

//或者 dst = mask_mat & resize_image;

//string s2 = dst.Dump();

//透明图处理

//B,G,R,A B,G,R -> 0 黑色 255 白色

//A为透明度 -> 255为不透明,0为全透。

//Cv2.ImShow("dst", dst);

var srcBgr = Cv2.Split(dst);

var alphaBgr = Cv2.Split(mask_mat);

var bgra = new[] { srcBgr[0], srcBgr[1], srcBgr[2], alphaBgr[0] };

Cv2.Merge(bgra, dst);

//Cv2.ImShow("dst_alpha_0", dst);

Cv2.ImWrite("1.png", dst);

//string s3 = dst.Dump();

pictureBox2.Image = new System.Drawing.Bitmap(dst.ToMemoryStream());

}

}

}

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace C__OpenVINO_Demo
{
    public partial class Form1 : Form
    {
        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";
        string model_path;
        CompiledModel cm;
        InferRequest ir;
        StringBuilder sb = new StringBuilder();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "models/ppmattingv2-stdc1-human_512/model.pdmodel";
            Model rawModel = OVCore.Shared.ReadModel(model_path);

            var ad = OVCore.Shared.AvailableDevices;
            Console.WriteLine("可用设备");
            foreach (var item in ad)
            {
                Console.WriteLine(item);
            }

            cm = OVCore.Shared.CompileModel(rawModel, "CPU");
            ir = cm.CreateInferRequest();

            img = "1.jpg";
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;

            img = ofd.FileName;
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);
            textBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (img == "") { return; }

            textBox1.Text = "";
            sb.Clear();
            pictureBox2.Image = null;
            Application.DoEvents();

            Mat src = new Mat(img);

            Stopwatch stopwatch = new Stopwatch();

            Mat resize_image = new Mat();
            Cv2.Resize(src, resize_image, new OpenCvSharp.Size(512, 512));

            Mat normalized = Common.Normalize(resize_image);

            float[] input_tensor_data = Common.ExtractMat(normalized);

            Tensor input_img = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 512, 512));

            ir.Inputs[0] = input_img;

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            ir.Run();

            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            Tensor output_0 = ir.Outputs[0];

            float[] mask = output_0.GetData<float>().ToArray();

            Mat mask_mat = new Mat(512, 512, MatType.CV_32FC1, mask);

            mask_mat *= 255;
            mask_mat.ConvertTo(mask_mat, MatType.CV_8UC1);

            Cv2.Resize(mask_mat, mask_mat, new OpenCvSharp.Size(512, 512));

            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();
            double totalTime = preprocessTime + inferTime + postprocessTime;

            sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
            sb.AppendLine($"Infer: {inferTime:F2}ms");
            sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
            sb.AppendLine($"Total: {totalTime:F2}ms");

            textBox1.Text = sb.ToString();

            Cv2.ImShow("mask", mask_mat);
            //string s = mask_mat.Dump();

            Cv2.Merge(new Mat[] { mask_mat, mask_mat, mask_mat }, mask_mat);

            Mat dst = new Mat();

            Cv2.BitwiseAnd(resize_image, mask_mat, dst);
            //或者 dst = mask_mat & resize_image;
            //string s2 = dst.Dump();

            //透明图处理
            //B,G,R,A   B,G,R -> 0 黑色 255 白色
            //A为透明度 -> 255为不透明,0为全透。
            //Cv2.ImShow("dst", dst);

            var srcBgr = Cv2.Split(dst);
            var alphaBgr = Cv2.Split(mask_mat);
            var bgra = new[] { srcBgr[0], srcBgr[1], srcBgr[2], alphaBgr[0] };
            Cv2.Merge(bgra, dst);

            //Cv2.ImShow("dst_alpha_0", dst);
            Cv2.ImWrite("1.png", dst);
            //string s3 = dst.Dump();

            pictureBox2.Image = new System.Drawing.Bitmap(dst.ToMemoryStream());

        }

    }

}

下载

源码下载

相关推荐
古希腊掌管学习的神19 分钟前
[机器学习]XGBoost(3)——确定树的结构
人工智能·机器学习
ZHOU_WUYI1 小时前
4.metagpt中的软件公司智能体 (ProjectManager 角色)
人工智能·metagpt
靴子学长1 小时前
基于字节大模型的论文翻译(含免费源码)
人工智能·深度学习·nlp
AI_NEW_COME2 小时前
知识库管理系统可扩展性深度测评
人工智能
海棠AI实验室3 小时前
AI的进阶之路:从机器学习到深度学习的演变(一)
人工智能·深度学习·机器学习
hunteritself3 小时前
AI Weekly『12月16-22日』:OpenAI公布o3,谷歌发布首个推理模型,GitHub Copilot免费版上线!
人工智能·gpt·chatgpt·github·openai·copilot
IT古董3 小时前
【机器学习】机器学习的基本分类-强化学习-策略梯度(Policy Gradient,PG)
人工智能·机器学习·分类
centurysee4 小时前
【最佳实践】Anthropic:Agentic系统实践案例
人工智能
mahuifa4 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai
四口鲸鱼爱吃盐4 小时前
Pytorch | 从零构建GoogleNet对CIFAR10进行分类
人工智能·pytorch·分类