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

时间:2024-02-24 18:43:56

目录

效果

项目

代码

下载 


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

        }

    }

}

下载 

源码下载