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

devtools/2024/10/22 13:38:39/

 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<Bbox> boxes;
            boxes = detect_face.detect(source_img);

            int position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况
            //List<Point2f> face_landmark_5of68 = new List<Point2f>();
            List<Point2f> 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<float> source_face_embedding = face_embedding.detect(source_img, face68landmarks);

            boxes = detect_face.detect(target_img);


            position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况
            List<Point2f> 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);
        }
    }
}

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<Bbox> boxes;boxes = detect_face.detect(source_img);int position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况//List<Point2f> face_landmark_5of68 = new List<Point2f>();List<Point2f> 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<float> source_face_embedding = face_embedding.detect(source_img, face68landmarks);boxes = detect_face.detect(target_img);position = 0; //一张图片里可能有多个人脸,这里只考虑1个人脸的情况List<Point2f> 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);}}
}

下载

exe可运行程序下载


http://www.ppmy.cn/devtools/6892.html

相关文章

软件设计师软考中项学习(二)之计算机系统基础知识

读者大大们好呀&#xff01;&#xff01;!☀️☀️☀️ &#x1f525; 欢迎来到我的博客 &#x1f440;期待大大的关注哦❗️❗️❗️ &#x1f680;欢迎收看我的主页文章➡️寻至善的主页 文章目录 学习目标学习内容学习笔记学习总结 学习目标 计算机系统硬件基本组成 中央处理…

隧道网络对讲广播音频终端-智慧工地网络报警求助箱

隧道网络对讲广播音频终端-智慧工地网络报警求助箱 SV-6007 网络对讲求助终端 一、描述 SV-6007是我司的一款壁挂式双按键求助对讲终端&#xff0c;具有10/100M以太网接口&#xff0c;其接收网络的音频数据&#xff0c;实时解码播放&#xff0c;还配置了麦克风输入和扬声器输…

【微服务】spring读取配置文件多种方式深入详解

目录 一、前言 二、java配置文件介绍 2.1 java配置文件产生原因 2.2 项目使用配置文件好处 2.3 springboot项目配置文件的必要性 2.4 微服务架构下配置文件使用场景 三、java读取配置文件常用方法 3.1 使用Properties类读取配置文件 3.1.1 使用getResourceAsStream读取…

开源Windows12网页版HTML源码

源码介绍 开源Windows12网页版HTML源码&#xff0c;无需安装就能用的Win12网页版来了Windows12概念版&#xff08;PoweredbyPowerPoint&#xff09;后深受启发&#xff0c;于是通过使用HTML、CSS、js等技术做了这样一个模拟板的Windows12系统&#xff0c;并已发布至github进行…

学习 Rust 的第五天:了解程序的基本控制流程

大家好呀 欢迎来到这个学习 Rust 的 30 天系列的第五天&#xff0c;今天我们将深入了解 Rust 中的控制流。 控制流&#xff0c;顾名思义&#xff0c;根据条件来 控制程序的流程。 If 表达式 当你想要在满足条件时执行一段代码块时&#xff0c;可以使用 if 表达式。 示例 …

网络邻居没有显示群晖NAS,但能通过IP访问?

问题描述 内网&#xff0c;通过路由器连接NAS&#xff0c;电脑可ping通NAS。设置群晖NAS后&#xff0c;添加了共享文件夹&#xff0c;可以通过\ip地址的方式进行访问。但是在网络里却发现不了NAS。通过\NAS名的方式也不行。 只是想在网络里发现NAS&#xff0c;并通过网络邻居…

【gradle】idea创建的gradle项目每个mudule有多余的iml文件

idea创建的gradle项目每个mudule有多余的iml文件 比如: module_name.iml module_name.main.iml module_name.test.iml 文件&#xff0c;而且每个module都有。 只需要在项目根目录的.idea文件夹下的gradle.xml文件添加<option name"resolveModulePerSourceSet" va…

抓取电商产品数据的方法|电商平台商品详情数据|批量上架|商品搬家|电商封装API数据采集接口更高效安全的数据采集

大量级电商数据采集时使用电商API接口有以下优势&#xff1a; 1. 数据准确性&#xff1a;通过电商API接口获取数据&#xff0c;可以保证数据的准确性和实时性&#xff0c;避免了手动采集可能出现的错误和延迟。 2. 自动化采集&#xff1a;API接口可以实现自动化的数据获取和更…