当前位置: 首页 > article >正文

C# OpenCvSharp 部署3D人脸重建3DDFA-V3

目录

说明

效果

模型信息

landmark.onnx

net_recon.onnx

net_recon_mbnet.onnx

retinaface_resnet50.onnx

项目

代码

下载

参考


C# OpenCvSharp 部署3D人脸重建3DDFA-V3

说明

地址:https://github.com/wang-zidu/3DDFA-V3

3DDFA_V3 uses the geometric guidance of facial part segmentation for face reconstruction, improving the alignment of reconstructed facial features with the original image and excelling at capturing extreme expressions. The key idea is to transform the target and prediction into semantic point sets, optimizing the distribution of point sets to ensure that the reconstructed regions and the target share the same geometry.

效果

模型信息

landmark.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[1, 3, 224, 224]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 212]
---------------------------------------------------------------

net_recon.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[1, 3, 224, 224]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 257]
---------------------------------------------------------------

net_recon_mbnet.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[1, 3, 224, 224]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 257]
---------------------------------------------------------------

retinaface_resnet50.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[1, 3, 512, 512]
---------------------------------------------------------------

Outputs
-------------------------
name:loc
tensor:Float[1, 10752, 4]
name:conf
tensor:Float[1, 10752, 2]
name:land
tensor:Float[1, 10752, 10]
---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


namespace C__OpenCvSharp_部署3D人脸重建3DDFA_V3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Stopwatch stopwatch = new Stopwatch();
        Mat image;
        string image_path;
        string startupPath;
        string model_path;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        const string DllName = "3DDFA_V3Sharp.dll";
        IntPtr engine;
        /*
         //初始化
        extern "C" _declspec(dllexport) int __cdecl  init(void** engine, char* model_path, char* msg);

        //forward
        extern "C" _declspec(dllexport) int __cdecl  forward(void* engine, Mat* srcimg, char* msg, Mat* render_shape, Mat* render_face, Mat* seg_face, Mat* landmarks);

        //释放
        extern "C" _declspec(dllexport) void __cdecl destroy(void* engine);
         */

        [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int init(ref IntPtr engine, string model_path, StringBuilder msg);

        [DllImport(DllName, EntryPoint = "forward", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int forward(IntPtr engine, IntPtr image, StringBuilder msg, IntPtr render_shape, IntPtr render_face, IntPtr seg_face, IntPtr landmarks);

        [DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int destroy(IntPtr engine);

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

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }


        Mat render_shape;
        Mat render_face;
        Mat seg_face;
        Mat landmarks;

        private void button2_Click(object sender, EventArgs e)
        {

            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;

            Application.DoEvents();

            Cv2.DestroyAllWindows();
            if (image != null) image.Dispose();
            if (render_shape != null) render_shape.Dispose();
            if (render_face != null) render_face.Dispose();
            if (seg_face != null) seg_face.Dispose();
            if (landmarks != null) landmarks.Dispose();
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

            StringBuilder msg = new StringBuilder(512);
            int out_imgs_size = 0;
            image = new Mat(image_path);
            render_shape = new Mat();
            render_face = new Mat();
            seg_face = new Mat();
            landmarks = new Mat();

            stopwatch.Restart();

            int res = forward(engine, image.CvPtr, msg, render_shape.CvPtr, render_face.CvPtr, seg_face.CvPtr, landmarks.CvPtr);
            if (res == 0)
            {
                stopwatch.Stop();
                double costTime = stopwatch.Elapsed.TotalMilliseconds;

                pictureBox2.Image = new Bitmap(landmarks.ToMemoryStream());

                Cv2.ImShow("render_shape", render_shape);
                Cv2.ImShow("render_face", render_face);
                Cv2.ImShow("seg_face", seg_face);

                textBox1.Text = $"耗时:{costTime:F2}ms";
            }
            else
            {
                textBox1.Text = "识别失败";
            }
            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;

            model_path = startupPath + "\\model\\";

            StringBuilder msg = new StringBuilder(512);

            int res = init(ref engine, model_path, msg);
            if (res == -1)
            {
                MessageBox.Show(msg.ToString());
                return;
            }
            else
            {
                Console.WriteLine(msg.ToString());
            }
            image_path = startupPath + "\\test_img\\1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            destroy(engine);
        }
    }
}
 

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


namespace C__OpenCvSharp_部署3D人脸重建3DDFA_V3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Stopwatch stopwatch = new Stopwatch();
        Mat image;
        string image_path;
        string startupPath;
        string model_path;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        const string DllName = "3DDFA_V3Sharp.dll";
        IntPtr engine;
        /*
         //初始化
        extern "C" _declspec(dllexport) int __cdecl  init(void** engine, char* model_path, char* msg);

        //forward
        extern "C" _declspec(dllexport) int __cdecl  forward(void* engine, Mat* srcimg, char* msg, Mat* render_shape, Mat* render_face, Mat* seg_face, Mat* landmarks);

        //释放
        extern "C" _declspec(dllexport) void __cdecl destroy(void* engine);
         */

        [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int init(ref IntPtr engine, string model_path, StringBuilder msg);

        [DllImport(DllName, EntryPoint = "forward", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int forward(IntPtr engine, IntPtr image, StringBuilder msg, IntPtr render_shape, IntPtr render_face, IntPtr seg_face, IntPtr landmarks);

        [DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int destroy(IntPtr engine);

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

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }


        Mat render_shape;
        Mat render_face;
        Mat seg_face;
        Mat landmarks;

        private void button2_Click(object sender, EventArgs e)
        {

            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;

            Application.DoEvents();

            Cv2.DestroyAllWindows();
            if (image != null) image.Dispose();
            if (render_shape != null) render_shape.Dispose();
            if (render_face != null) render_face.Dispose();
            if (seg_face != null) seg_face.Dispose();
            if (landmarks != null) landmarks.Dispose();
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

            StringBuilder msg = new StringBuilder(512);
            int out_imgs_size = 0;
            image = new Mat(image_path);
            render_shape = new Mat();
            render_face = new Mat();
            seg_face = new Mat();
            landmarks = new Mat();

            stopwatch.Restart();

            int res = forward(engine, image.CvPtr, msg, render_shape.CvPtr, render_face.CvPtr, seg_face.CvPtr, landmarks.CvPtr);
            if (res == 0)
            {
                stopwatch.Stop();
                double costTime = stopwatch.Elapsed.TotalMilliseconds;

                pictureBox2.Image = new Bitmap(landmarks.ToMemoryStream());

                Cv2.ImShow("render_shape", render_shape);
                Cv2.ImShow("render_face", render_face);
                Cv2.ImShow("seg_face", seg_face);

                textBox1.Text = $"耗时:{costTime:F2}ms";
            }
            else
            {
                textBox1.Text = "识别失败";
            }
            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;

            model_path = startupPath + "\\model\\";

            StringBuilder msg = new StringBuilder(512);

            int res = init(ref engine, model_path, msg);
            if (res == -1)
            {
                MessageBox.Show(msg.ToString());
                return;
            }
            else
            {
                Console.WriteLine(msg.ToString());
            }
            image_path = startupPath + "\\test_img\\1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            destroy(engine);
        }
    }
}

下载

源码下载

参考

https://github.com/hpc203/3DDFA-V3-opencv-dnn


http://www.kler.cn/a/510022.html

相关文章:

  • Mockito+PowerMock+Junit单元测试
  • Unity2017 控制.abc格式的三维动画播放
  • nuiapp在APP中的.nvue页面中使用webview展示空白的问题
  • 图数据库 | 19、高可用分布式设计(下)
  • 个人vue3-学习笔记
  • Redis系列之底层数据结构字典Dict
  • SpringBoot项目打war包要点
  • Java基础——概念和常识(语言特点、JVM、JDK、JRE、AOT/JIT等介绍)
  • 【线性代数】行列式的概念
  • 【Web】2025西湖论剑·中国杭州网络安全安全技能大赛题解(全)
  • 【k8s】k8s部署Argo CD
  • HTML学习笔记(4)
  • SparkSQL数据源与数据存储综合实践
  • [Effective C++]条款47 萃取器
  • 洛谷P4017 最大食物链计数(图的拓扑排序)
  • 从新手到高手的蜕变:MySQL 约束进阶全攻略
  • vue 实现打印功能
  • 期望最大化算法:机器学习中的隐变量与参数估计的艺术
  • AIGC - 深度洞察如何对大模型进行微调以满足特定需求
  • RPA编程实践:Electron实践开始
  • vllm稳定输出json
  • 素描风格渲染
  • 基于Java+Sql Server实现的(GUI)学籍管理系统
  • springboot基于微信小程序的传统美食文化宣传平台小程序
  • docker 基础语法学习,K8s基础语法学习,零基础学习
  • python-leetcode-存在重复元素 II