C# OpenCvSharp+DlibDotNet 人脸替换 换脸
效果
Demo下载
项目
VS2022 .net4.8 OpenCvSharp4 DlibDotNet
代码
-
using DlibDotNet;
-
using OpenCvSharp.Extensions;
-
using OpenCvSharp;
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Globalization;
-
-
namespace OpenCvSharp_人脸替换
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
Bitmap bmp;
-
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
-
string imgPath = "";
-
string startupPath = "";
-
-
Bitmap bmp2;
-
string imgPath2 = "";
-
-
FrontalFaceDetector fd;
-
ShapePredictor sp;
-
-
private void button2_Click(object sender, EventArgs e)
-
{
-
OpenFileDialog ofd = new OpenFileDialog();
-
ofd.Filter = fileFilter;
-
if (ofd.ShowDialog() != DialogResult.OK) return;
-
-
pictureBox1.Image = null;
-
-
imgPath = ofd.FileName;
-
bmp = new Bitmap(imgPath);
-
pictureBox1.Image = new Bitmap(imgPath);
-
}
-
-
private void button3_Click(object sender, EventArgs e)
-
{
-
OpenFileDialog ofd = new OpenFileDialog();
-
ofd.Filter = fileFilter;
-
if (ofd.ShowDialog() != DialogResult.OK) return;
-
-
pictureBox2.Image = null;
-
-
imgPath2 = ofd.FileName;
-
bmp2 = new Bitmap(imgPath2);
-
pictureBox2.Image = new Bitmap(imgPath2);
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
if (pictureBox1.Image==null || pictureBox2.Image==null)
-
{
-
return;
-
}
-
pictureBox3.Image = ProcessImage(bmp, bmp2);
-
}
-
-
/// <summary>
-
/// Process the original selfie and produce the face-swapped image.
-
/// </summary>
-
/// <param name="image">The original selfie image.</param>
-
/// <param name="newImage">The new face to insert into the selfie.</param>
-
/// <returns>A new image with faces swapped.</returns>
-
public Bitmap ProcessImage(Bitmap image, Bitmap newImage)
-
{
-
-
// convert image to dlib format
-
var img = Dlib.LoadImage<RgbPixel>(imgPath);
-
-
// find bradley's faces in image
-
var faces = fd.Operator(img);
-
if (faces.Count()==0)
-
{
-
return null;
-
}
-
var bradley = faces[0];
-
-
// get bradley's landmark points
-
var bradleyShape = sp.Detect(img, bradley);
-
var bradleyPoints = (from i in Enumerable.Range(0, (int)bradleyShape.Parts)
-
let p = bradleyShape.GetPart((uint)i)
-
select new OpenCvSharp.Point(p.X, p.Y)).ToArray();
-
-
// get convex hull of bradley's points
-
var hull = Cv2.ConvexHullIndices(bradleyPoints);
-
var bradleyHull = from i in hull
-
select bradleyPoints[i];
-
-
// find landmark points in face to swap
-
var imgMark = Dlib.LoadImage<RgbPixel>(imgPath2);
-
var faces2 = fd.Operator(imgMark);
-
if (faces2.Count() == 0)
-
{
-
return null;
-
}
-
var mark = faces2[0];
-
var markShape = sp.Detect(imgMark, mark);
-
var markPoints = (from i in Enumerable.Range(0, (int)markShape.Parts)
-
let p = markShape.GetPart((uint)i)
-
select new OpenCvSharp.Point(p.X, p.Y)).ToArray();
-
-
// get convex hull of mark's points
-
var hull2 = Cv2.ConvexHullIndices(bradleyPoints);
-
var markHull = from i in hull2
-
select markPoints[i];
-
-
-
// calculate Delaunay triangles
-
var triangles = Utility.GetDelaunayTriangles(bradleyHull);
-
-
// get transformations to warp the new face onto Bradley's face
-
var warps = Utility.GetWarps(markHull, bradleyHull, triangles);
-
-
// apply the warps to the new face to prep it for insertion into the main image
-
var warpedImg = Utility.ApplyWarps(newImage, image.Width, image.Height, warps);
-
-
-
// prepare a mask for the warped image
-
var mask = new Mat(image.Height, image.Width, MatType.CV_8UC3);
-
mask.SetTo(0);
-
Cv2.FillConvexPoly(mask, bradleyHull, new Scalar(255, 255, 255), LineTypes.Link8);
-
-
// find the center of the warped face
-
var r = Cv2.BoundingRect(bradleyHull);
-
var center = new OpenCvSharp.Point(r.Left r.Width / 2, r.Top r.Height / 2);
-
-
// blend the warped face into the main image
-
var selfie = BitmapConverter.ToMat(image);
-
var blend = new Mat(selfie.Size(), selfie.Type());
-
Cv2.SeamlessClone(warpedImg, selfie, mask, center, blend, SeamlessCloneMethods.NormalClone);
-
-
// return the modified main image
-
return BitmapConverter.ToBitmap(blend);
-
-
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
fd = Dlib.GetFrontalFaceDetector();
-
sp = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat");
-
Dlib.Encoding = Environment.OSVersion.Platform == PlatformID.Win32NT ? Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage) : Encoding.UTF8;
-
}
-
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbikah
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01