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

        }

    }

}

下载

源码下载

相关推荐
有Li4 分钟前
跨视角差异-依赖网络用于体积医学图像分割|文献速递-生成式模型与transformer在医学影像中的应用
人工智能·计算机视觉
新加坡内哥谈技术24 分钟前
Mistral推出“Le Chat”,对标ChatGPT
人工智能·chatgpt
GOTXX33 分钟前
基于Opencv的图像处理软件
图像处理·人工智能·深度学习·opencv·卷积神经网络
IT古董37 分钟前
【人工智能】Python在机器学习与人工智能中的应用
开发语言·人工智能·python·机器学习
CV学术叫叫兽1 小时前
快速图像识别:落叶植物叶片分类
人工智能·分类·数据挖掘
嵌入式大圣1 小时前
单片机结合OpenCV
单片机·嵌入式硬件·opencv
xrgs_shz1 小时前
MATLAB读入不同类型图像并显示图像和相关信息
图像处理·计算机视觉·matlab
WeeJot嵌入式1 小时前
卷积神经网络:深度学习中的图像识别利器
人工智能
糖豆豆今天也要努力鸭1 小时前
torch.__version__的torch版本和conda list的torch版本不一致
linux·pytorch·python·深度学习·conda·torch
脆皮泡泡1 小时前
Ultiverse 和web3新玩法?AI和GameFi的结合是怎样
人工智能·web3