• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C# OpenCvSharp+DlibDotNet 人脸替换 换脸

武飞扬头像
天天代码码天天
帮助1

效果

学新通

学新通

Demo下载

项目

VS2022 .net4.8 OpenCvSharp4 DlibDotNet

学新通

 相关介绍参考

代码

  1.  
    using DlibDotNet;
  2.  
    using OpenCvSharp.Extensions;
  3.  
    using OpenCvSharp;
  4.  
    using System;
  5.  
    using System.Collections.Generic;
  6.  
    using System.ComponentModel;
  7.  
    using System.Data;
  8.  
    using System.Drawing;
  9.  
    using System.Linq;
  10.  
    using System.Text;
  11.  
    using System.Windows.Forms;
  12.  
    using System.Globalization;
  13.  
     
  14.  
    namespace OpenCvSharp_人脸替换
  15.  
    {
  16.  
    public partial class Form1 : Form
  17.  
    {
  18.  
    public Form1()
  19.  
    {
  20.  
    InitializeComponent();
  21.  
    }
  22.  
     
  23.  
    Bitmap bmp;
  24.  
    string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  25.  
    string imgPath = "";
  26.  
    string startupPath = "";
  27.  
     
  28.  
    Bitmap bmp2;
  29.  
    string imgPath2 = "";
  30.  
     
  31.  
    FrontalFaceDetector fd;
  32.  
    ShapePredictor sp;
  33.  
     
  34.  
    private void button2_Click(object sender, EventArgs e)
  35.  
    {
  36.  
    OpenFileDialog ofd = new OpenFileDialog();
  37.  
    ofd.Filter = fileFilter;
  38.  
    if (ofd.ShowDialog() != DialogResult.OK) return;
  39.  
     
  40.  
    pictureBox1.Image = null;
  41.  
     
  42.  
    imgPath = ofd.FileName;
  43.  
    bmp = new Bitmap(imgPath);
  44.  
    pictureBox1.Image = new Bitmap(imgPath);
  45.  
    }
  46.  
     
  47.  
    private void button3_Click(object sender, EventArgs e)
  48.  
    {
  49.  
    OpenFileDialog ofd = new OpenFileDialog();
  50.  
    ofd.Filter = fileFilter;
  51.  
    if (ofd.ShowDialog() != DialogResult.OK) return;
  52.  
     
  53.  
    pictureBox2.Image = null;
  54.  
     
  55.  
    imgPath2 = ofd.FileName;
  56.  
    bmp2 = new Bitmap(imgPath2);
  57.  
    pictureBox2.Image = new Bitmap(imgPath2);
  58.  
    }
  59.  
     
  60.  
    private void button1_Click(object sender, EventArgs e)
  61.  
    {
  62.  
    if (pictureBox1.Image==null || pictureBox2.Image==null)
  63.  
    {
  64.  
    return;
  65.  
    }
  66.  
    pictureBox3.Image = ProcessImage(bmp, bmp2);
  67.  
    }
  68.  
     
  69.  
    /// <summary>
  70.  
    /// Process the original selfie and produce the face-swapped image.
  71.  
    /// </summary>
  72.  
    /// <param name="image">The original selfie image.</param>
  73.  
    /// <param name="newImage">The new face to insert into the selfie.</param>
  74.  
    /// <returns>A new image with faces swapped.</returns>
  75.  
    public Bitmap ProcessImage(Bitmap image, Bitmap newImage)
  76.  
    {
  77.  
     
  78.  
    // convert image to dlib format
  79.  
    var img = Dlib.LoadImage<RgbPixel>(imgPath);
  80.  
     
  81.  
    // find bradley's faces in image
  82.  
    var faces = fd.Operator(img);
  83.  
    if (faces.Count()==0)
  84.  
    {
  85.  
    return null;
  86.  
    }
  87.  
    var bradley = faces[0];
  88.  
     
  89.  
    // get bradley's landmark points
  90.  
    var bradleyShape = sp.Detect(img, bradley);
  91.  
    var bradleyPoints = (from i in Enumerable.Range(0, (int)bradleyShape.Parts)
  92.  
    let p = bradleyShape.GetPart((uint)i)
  93.  
    select new OpenCvSharp.Point(p.X, p.Y)).ToArray();
  94.  
     
  95.  
    // get convex hull of bradley's points
  96.  
    var hull = Cv2.ConvexHullIndices(bradleyPoints);
  97.  
    var bradleyHull = from i in hull
  98.  
    select bradleyPoints[i];
  99.  
     
  100.  
    // find landmark points in face to swap
  101.  
    var imgMark = Dlib.LoadImage<RgbPixel>(imgPath2);
  102.  
    var faces2 = fd.Operator(imgMark);
  103.  
    if (faces2.Count() == 0)
  104.  
    {
  105.  
    return null;
  106.  
    }
  107.  
    var mark = faces2[0];
  108.  
    var markShape = sp.Detect(imgMark, mark);
  109.  
    var markPoints = (from i in Enumerable.Range(0, (int)markShape.Parts)
  110.  
    let p = markShape.GetPart((uint)i)
  111.  
    select new OpenCvSharp.Point(p.X, p.Y)).ToArray();
  112.  
     
  113.  
    // get convex hull of mark's points
  114.  
    var hull2 = Cv2.ConvexHullIndices(bradleyPoints);
  115.  
    var markHull = from i in hull2
  116.  
    select markPoints[i];
  117.  
     
  118.  
     
  119.  
    // calculate Delaunay triangles
  120.  
    var triangles = Utility.GetDelaunayTriangles(bradleyHull);
  121.  
     
  122.  
    // get transformations to warp the new face onto Bradley's face
  123.  
    var warps = Utility.GetWarps(markHull, bradleyHull, triangles);
  124.  
     
  125.  
    // apply the warps to the new face to prep it for insertion into the main image
  126.  
    var warpedImg = Utility.ApplyWarps(newImage, image.Width, image.Height, warps);
  127.  
     
  128.  
     
  129.  
    // prepare a mask for the warped image
  130.  
    var mask = new Mat(image.Height, image.Width, MatType.CV_8UC3);
  131.  
    mask.SetTo(0);
  132.  
    Cv2.FillConvexPoly(mask, bradleyHull, new Scalar(255, 255, 255), LineTypes.Link8);
  133.  
     
  134.  
    // find the center of the warped face
  135.  
    var r = Cv2.BoundingRect(bradleyHull);
  136.  
    var center = new OpenCvSharp.Point(r.Left r.Width / 2, r.Top r.Height / 2);
  137.  
     
  138.  
    // blend the warped face into the main image
  139.  
    var selfie = BitmapConverter.ToMat(image);
  140.  
    var blend = new Mat(selfie.Size(), selfie.Type());
  141.  
    Cv2.SeamlessClone(warpedImg, selfie, mask, center, blend, SeamlessCloneMethods.NormalClone);
  142.  
     
  143.  
    // return the modified main image
  144.  
    return BitmapConverter.ToBitmap(blend);
  145.  
     
  146.  
     
  147.  
    }
  148.  
     
  149.  
    private void Form1_Load(object sender, EventArgs e)
  150.  
    {
  151.  
    fd = Dlib.GetFrontalFaceDetector();
  152.  
    sp = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat");
  153.  
    Dlib.Encoding = Environment.OSVersion.Platform == PlatformID.Win32NT ? Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage) : Encoding.UTF8;
  154.  
    }
  155.  
     
  156.  
    }
  157.  
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbikah
系列文章
更多 icon
同类精品
更多 icon
继续加载