• C#版Facefusion ,换脸器和增强器


     C#版Facefusion ,换脸器和增强器

    目录

    说明

    效果

    项目

    调用代码 

    下载


    说明

    Facefusion是一款最新的开源AI视频/图片换脸项目。是原来ROOP的项目的延续。项目官方介绍只有一句话,下一代换脸器和增强器。

    代码实现参考

    https://github.com/facefusion/facefusion

    https://github.com/hpc203/facefusion-onnxrun

    效果

    项目

    调用代码 

    using OpenCvSharp;
    using OpenCvSharp.Extensions;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    namespace FaceFusionSharp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

            string startupPath = "";

            string source_path = "";
            string target_path = "";

            Yolov8Face detect_face;
            Face68Landmarks detect_68landmarks;
            FaceEmbdding face_embedding;
            SwapFace swap_face;
            FaceEnhance enhance_face;

            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = fileFilter;
                if (ofd.ShowDialog() != DialogResult.OK) return;

                pictureBox1.Image = null;

                source_path = ofd.FileName;
                pictureBox1.Image = new Bitmap(source_path);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = fileFilter;
                if (ofd.ShowDialog() != DialogResult.OK) return;

                pictureBox2.Image = null;

                target_path = ofd.FileName;
                pictureBox2.Image = new Bitmap(target_path);
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (pictureBox1.Image == null || pictureBox2.Image == null)
                {
                    return;
                }

                pictureBox3.Image = null;
                Application.DoEvents();

                Mat source_img = Cv2.ImRead(source_path);
                Mat target_img = Cv2.ImRead(target_path);

                List boxes;
                boxes = detect_face.detect(source_img);

                int position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况
                //List face_landmark_5of68 = new List();
                List face68landmarks = detect_68landmarks.detect(source_img, boxes[position]);

                //绘图
                //Cv2.Rectangle(source_img, new OpenCvSharp.Point(boxes[0].xmin, boxes[0].ymin), new OpenCvSharp.Point(boxes[0].xmax, boxes[0].ymax), new Scalar(255, 0, 0), 2);
                //foreach (Point2f item in face68landmarks)
                //{
                //    Cv2.Circle(source_img, (int)item.X, (int)item.Y, 4, new Scalar(0, 255, 0), -1);
                //}
                //Cv2.ImShow("source_img", source_img);

                List source_face_embedding = face_embedding.detect(source_img, face68landmarks);

                boxes = detect_face.detect(target_img);


                position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况
                List target_landmark_5;
                target_landmark_5 = detect_68landmarks.detect(target_img, boxes[position]);

                //绘图
                //Cv2.Rectangle(target_img, new OpenCvSharp.Point(boxes[0].xmin, boxes[0].ymin), new OpenCvSharp.Point(boxes[0].xmax, boxes[0].ymax), new Scalar(255, 0, 0), 2);
                //foreach (Point2f item in target_landmark_5)
                //{
                //    Cv2.Circle(target_img, (int)item.X, (int)item.Y, 4, new Scalar(0, 255, 0), -1);
                //}
                //Cv2.ImShow("target_img", target_img);


                Mat swapimg = swap_face.process(target_img, source_face_embedding, target_landmark_5);

                Mat resultimg = enhance_face.process(swapimg, target_landmark_5);

                //pictureBox3.Image = swapimg.ToBitmap();

                pictureBox3.Image = resultimg.ToBitmap();

                // Cv2.ImShow("resultimg", resultimg);

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                detect_face = new Yolov8Face("model/yoloface_8n.onnx");
                detect_68landmarks = new Face68Landmarks("model/2dfan4.onnx");
                face_embedding = new FaceEmbdding("model/arcface_w600k_r50.onnx");
                swap_face = new SwapFace("model/inswapper_128.onnx");
                enhance_face = new FaceEnhance("model/gfpgan_1.4.onnx");

                target_path = "images/target.jpg";
                source_path = "images/5.jpg";

                //target_path = "images/5.jpg";
                //source_path = "images/14.jpg";

                pictureBox1.Image = new Bitmap(source_path);
                pictureBox2.Image = new Bitmap(target_path);
            }
        }
    }

    1. using OpenCvSharp;
    2. using OpenCvSharp.Extensions;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Drawing;
    6. using System.Windows.Forms;
    7. namespace FaceFusionSharp
    8. {
    9. public partial class Form1 : Form
    10. {
    11. public Form1()
    12. {
    13. InitializeComponent();
    14. }
    15. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    16. string startupPath = "";
    17. string source_path = "";
    18. string target_path = "";
    19. Yolov8Face detect_face;
    20. Face68Landmarks detect_68landmarks;
    21. FaceEmbdding face_embedding;
    22. SwapFace swap_face;
    23. FaceEnhance enhance_face;
    24. private void button2_Click(object sender, EventArgs e)
    25. {
    26. OpenFileDialog ofd = new OpenFileDialog();
    27. ofd.Filter = fileFilter;
    28. if (ofd.ShowDialog() != DialogResult.OK) return;
    29. pictureBox1.Image = null;
    30. source_path = ofd.FileName;
    31. pictureBox1.Image = new Bitmap(source_path);
    32. }
    33. private void button3_Click(object sender, EventArgs e)
    34. {
    35. OpenFileDialog ofd = new OpenFileDialog();
    36. ofd.Filter = fileFilter;
    37. if (ofd.ShowDialog() != DialogResult.OK) return;
    38. pictureBox2.Image = null;
    39. target_path = ofd.FileName;
    40. pictureBox2.Image = new Bitmap(target_path);
    41. }
    42. private void button1_Click(object sender, EventArgs e)
    43. {
    44. if (pictureBox1.Image == null || pictureBox2.Image == null)
    45. {
    46. return;
    47. }
    48. pictureBox3.Image = null;
    49. Application.DoEvents();
    50. Mat source_img = Cv2.ImRead(source_path);
    51. Mat target_img = Cv2.ImRead(target_path);
    52. List<Bbox> boxes;
    53. boxes = detect_face.detect(source_img);
    54. int position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况
    55. //List<Point2f> face_landmark_5of68 = new List<Point2f>();
    56. List<Point2f> face68landmarks = detect_68landmarks.detect(source_img, boxes[position]);
    57. //绘图
    58. //Cv2.Rectangle(source_img, new OpenCvSharp.Point(boxes[0].xmin, boxes[0].ymin), new OpenCvSharp.Point(boxes[0].xmax, boxes[0].ymax), new Scalar(255, 0, 0), 2);
    59. //foreach (Point2f item in face68landmarks)
    60. //{
    61. // Cv2.Circle(source_img, (int)item.X, (int)item.Y, 4, new Scalar(0, 255, 0), -1);
    62. //}
    63. //Cv2.ImShow("source_img", source_img);
    64. List<float> source_face_embedding = face_embedding.detect(source_img, face68landmarks);
    65. boxes = detect_face.detect(target_img);
    66. position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况
    67. List<Point2f> target_landmark_5;
    68. target_landmark_5 = detect_68landmarks.detect(target_img, boxes[position]);
    69. //绘图
    70. //Cv2.Rectangle(target_img, new OpenCvSharp.Point(boxes[0].xmin, boxes[0].ymin), new OpenCvSharp.Point(boxes[0].xmax, boxes[0].ymax), new Scalar(255, 0, 0), 2);
    71. //foreach (Point2f item in target_landmark_5)
    72. //{
    73. // Cv2.Circle(target_img, (int)item.X, (int)item.Y, 4, new Scalar(0, 255, 0), -1);
    74. //}
    75. //Cv2.ImShow("target_img", target_img);
    76. Mat swapimg = swap_face.process(target_img, source_face_embedding, target_landmark_5);
    77. Mat resultimg = enhance_face.process(swapimg, target_landmark_5);
    78. //pictureBox3.Image = swapimg.ToBitmap();
    79. pictureBox3.Image = resultimg.ToBitmap();
    80. // Cv2.ImShow("resultimg", resultimg);
    81. }
    82. private void Form1_Load(object sender, EventArgs e)
    83. {
    84. detect_face = new Yolov8Face("model/yoloface_8n.onnx");
    85. detect_68landmarks = new Face68Landmarks("model/2dfan4.onnx");
    86. face_embedding = new FaceEmbdding("model/arcface_w600k_r50.onnx");
    87. swap_face = new SwapFace("model/inswapper_128.onnx");
    88. enhance_face = new FaceEnhance("model/gfpgan_1.4.onnx");
    89. target_path = "images/target.jpg";
    90. source_path = "images/5.jpg";
    91. //target_path = "images/5.jpg";
    92. //source_path = "images/14.jpg";
    93. pictureBox1.Image = new Bitmap(source_path);
    94. pictureBox2.Image = new Bitmap(target_path);
    95. }
    96. }
    97. }

    下载

    exe可运行程序下载

  • 相关阅读:
    C# 人像卡通化 Onnx photo2cartoon
    JVM运行时数据区域详解
    2022 SpeechHome 语音技术研讨会-回顾
    KMP,ACM集训
    性能优化-中间件tomcat调优
    哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
    Pyqt常用代码片段
    【Python】一文详细介绍 plt.rc_context() 在 Matplotlib 中的原理、作用、注意事项
    北邮22级信通院数电:Verilog-FPGA(4)第三周实验:按键消抖、呼吸灯、流水灯 操作流程&&注意事项
    分类预测 | MATLAB实现1-DCNN一维卷积神经网络分类预测
  • 原文地址:https://blog.csdn.net/weixin_46771779/article/details/137881477