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

        }

    }

}

下载

源码下载

相关推荐
孙同学要努力2 小时前
全连接神经网络案例——手写数字识别
人工智能·深度学习·神经网络
Eric.Lee20212 小时前
yolo v5 开源项目
人工智能·yolo·目标检测·计算机视觉
其实吧33 小时前
基于Matlab的图像融合研究设计
人工智能·计算机视觉·matlab
丕羽3 小时前
【Pytorch】基本语法
人工智能·pytorch·python
ctrey_3 小时前
2024-11-1 学习人工智能的Day20 openCV(2)
人工智能·opencv·学习
SongYuLong的博客3 小时前
Air780E基于LuatOS编程开发
人工智能
Jina AI3 小时前
RAG 系统的分块难题:小型语言模型如何找到最佳断点?
人工智能·语言模型·自然语言处理
-派神-3 小时前
大语言模型(LLM)量化基础知识(一)
人工智能·语言模型·自然语言处理
johnny_hhh3 小时前
AI大模型重塑软件开发流程:定义、应用场景、优势、挑战及未来展望
人工智能
Elastic 中国社区官方博客4 小时前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理