PaddleSegSharp 是一個基於百度飛槳PaddleSeg專案的人像分割模組而開發的.NET的工具類庫。
PaddleSegSharp 中PaddleSeg.dll檔案是基於開源專案最新發布版本PaddleSeg PaddleSeg的版本修改而成的C++動態庫,基於opencv的x64編譯而成的。
PaddleSeg是基於飛槳PaddlePaddle的端到端影像分割套件,內建45+模型演算法及140+預訓練模型,支援配置化驅動和API呼叫開發方式,打通資料標註、模型開發、訓練、壓縮、部署的全流程,提供語義分割、互動式分割、Matting、全景分割四大分割能力,助力演算法在醫療、工業、遙感、娛樂等場景落地應用。
本專案只能在X64的CPU上編譯和使用,只能在avx指令集上的CPU上使用,目前僅支援windows平臺。
PaddleSegSharp 支援飛槳人像摳圖的所有模型。PP-MattingV2-512、PP-Matting-512、PP-Matting-1024、PP-HumanMatting、MODNet-MobileNetV2。 預設使用MODNet-MobileNetV2模型。
1、新建winform專案
2、新增引用PaddleSegSharp ,使用NuGet 搜尋新增PaddleSegSharp
3、窗體上新增圖片顯示控制元件以及按鈕
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PaddleSegSharp; namespace WindowsFormzhengjian { public partial class Form1 : Form { #region 欄位 private string[] bmpFilters = new string[] { ".bmp", ".jpg", ".jpeg", ".tiff", ".tif", ".png" }; private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; private PaddleSegMattingEngine engine; MattingParameter parameter; string modelPath = null; string path = Environment.CurrentDirectory + @"\out\"; /// <summary> /// 建立畫布的畫板背景 /// </summary> Bitmap backgroundImage = null; #endregion public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //初始化 引擎 engine = new PaddleSegMattingEngine(); //引數 parameter = new MattingParameter(); //parameter.outbgfile = true;//輸出mask圖 //parameter.bgtransparent = true;//背景透明 engine.Init(modelPath, parameter); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } /// <summary> /// 選擇圖片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSelectPic_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() == DialogResult.OK) { //pictureBox1.Image = new Bitmap(ofd.FileName); using (FileStream ms = File.OpenRead(ofd.FileName)) { //解決記憶體異常問題,以及this.pbx.BackgroundImage = System.Drawing.Image.FromStream(ms)導致的一般性gdi+ 問題 using (Bitmap bt = new Bitmap(ms)) { #region MyRegion //backgroundImage = new Bitmap(bt.Width, bt.Height); ////Graphics g = pbx.CreateGraphics(); //Graphics g = Graphics.FromImage(backgroundImage); ////g.DrawLine(Pens.Black, startPoint, e.Location); ////g.Clear(Color.White); ////Pen myPen = new Pen(Color.Black, int.TryParse(ConfigurationManager.AppSettings["penwidth"], out int penwidth) ? penwidth : 3); //g.SmoothingMode = SmoothingMode.AntiAlias; //g.CompositingQuality = CompositingQuality.HighQuality; //g.CompositingMode = CompositingMode.SourceOver; //g.DrawImage(bt, bt.Width, bt.Height); ////PointF pointFstart = PointToPointF(startPoint); ////PointF pointFend = PointToPointF(e.Location); ////g.DrawBeziers(myPen, new PointF[] { pointFstart, pointFend }); //pbx.BackgroundImage = backgroundImage; //g.Dispose(); bt.Save("Fing.jpg"); ////this.pbx.BackgroundImage = System.Drawing.Image.FromStream(ms); #endregion backgroundImage = new Bitmap(bt.Width, bt.Height, bt.PixelFormat); Graphics g = Graphics.FromImage(backgroundImage); g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; g.CompositingMode = CompositingMode.SourceOver; g.DrawImage(bt, 0, 0); backgroundImage.Save("Finge.jpg"); //pictureBox1.Image = Image.FromHbitmap(backgroundImage.GetHbitmap()); pictureBox1.Image = backgroundImage; g.Dispose(); pictureBox1.Refresh(); } } #region ok ////引數 //MattingParameter parameter = new MattingParameter(); ////初始化引擎 //PaddleSegMattingEngine engine = new PaddleSegMattingEngine(); //engine.Init(modelPath, parameter); ////設定背景顏色 //engine.Setbackground(45, 145, 255); ////分割後的檔案 //string outfile = Guid.NewGuid().ToString() + ".png"; //string outbgfile = Path.GetFileNameWithoutExtension(outfile) + "_bg.png"; //engine.Seg(ofd.FileName, outfile, outbgfile); #endregion } this.Refresh(); } /// <summary> /// 替換背景色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReplaceBackground_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { ////引數ok //MattingParameter parameter = new MattingParameter(); ////初始化引擎 //PaddleSegMattingEngine engine = new PaddleSegMattingEngine(); //engine.Init(modelPath, parameter); ////設定背景顏色 //Color bgcolor = Color.White; //ColorDialog colorDialog = new ColorDialog(); //colorDialog.Color = bgcolor; //if (colorDialog.ShowDialog() != DialogResult.OK) return; //bgcolor = colorDialog.Color; //engine.Setbackground(bgcolor.R, bgcolor.G, bgcolor.B); ////engine.Setbackground(45, 145, 255); ////分割後的檔案 //string outfile = Guid.NewGuid().ToString() + ".png"; //string outbgfile = Path.GetFileNameWithoutExtension(outfile) + "_bg.png"; ////engine.Seg("小一寸.png", outfile, outbgfile); ////engine.Seg(new Bitmap("小一寸.png"), outfile, outbgfile); //engine.Seg(pictureBox1.Image, outfile, outbgfile); engine = new PaddleSegMattingEngine(); engine.Init(modelPath, parameter); Color bgcolor = Color.White; ColorDialog colorDialog = new ColorDialog(); colorDialog.Color = bgcolor; if (colorDialog.ShowDialog() != DialogResult.OK) return; bgcolor = colorDialog.Color; engine.Setbackground(bgcolor.R, bgcolor.G, bgcolor.B); //engine.SetbackgroundFile(""); string outfile = path + Guid.NewGuid().ToString() + ".png"; string outbgfile = path + Path.GetFileNameWithoutExtension(outfile) + "_bg.png"; engine.Seg(pictureBox1.Image, outfile, outbgfile); //engine.Setbackground(45, 145, 255); ////分割後的檔案 //string outfile = "C:\\" + Guid.NewGuid().ToString() + ".bmp"; //engine.Seg("C:\\Users\\Administrator\\Desktop\\1.jpg", outfile, "mask圖路徑"); pictureBox2.Image = new Bitmap(outfile); } } /// <summary> /// 替換背景圖 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSetBackground_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { engine = new PaddleSegMattingEngine(); engine.Init(modelPath, parameter); OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() != DialogResult.OK) return; engine.SetbackgroundFile(ofd.FileName); string outfile = path + Guid.NewGuid().ToString() + ".png"; string outbgfile = path + Path.GetFileNameWithoutExtension(outfile) + "_bg.png"; //string outfile = path + "1.png"; //string outbgfile = path + "1_bg.png"; engine.Seg(pictureBox1.Image, outfile, outbgfile); pictureBox2.Image = new Bitmap(outfile); } } private void btnSave_Click(object sender, EventArgs e) { if (pictureBox2.Image != null) { SaveFileDialog fileDialog = new SaveFileDialog(); fileDialog.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" + "JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" + "mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" + "iles (*.*)|*.*"; if ((fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)) { string path = fileDialog.FileName.ToString(); pictureBox2.Image.Save(path); } } } private void btnOCR_Click(object sender, EventArgs e) { //OpenFileDialog ofd = new OpenFileDialog(); //ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; //if (ofd.ShowDialog() != DialogResult.OK) // return; //var imagebyte = File.ReadAllBytes(ofd.FileName); //Bitmap bitmap = new Bitmap(new MemoryStream(imagebyte)); //OCRModelConfig config = null; //OCRParameter oCRParameter = new OCRParameter(); //OCRResult ocrResult = new OCRResult(); //using (PaddleOCREngineengine = new PaddleOCREngine(config, oCRParameter)) //{ // ocrResult = engine.DetectText(bitmap); //} //if (ocrResult != null) //{ // MessageBox.Show(ocrResult.Text, "識別結果"); //} } } }