C# OpenCvSharp DNN 部署L2CS-Net人脸朝向估计

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

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

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;
        Mat result_image;

        Net opencv_net;
        Mat BN_image;

        StringBuilder sb = new StringBuilder();

        int reg_max = 16;
        int num_class = 1;

        int inpWidth = 640;
        int inpHeight = 640;

        float score_threshold = 0.25f;
        float nms_threshold = 0.5f;

        L2CSNet gaze_predictor;

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\yolov8n-face.onnx";
            //初始化网络类,读取本地模型
            opencv_net = CvDnn.ReadNetFromOnnx(model_path);

            gaze_predictor = new L2CSNet("l2cs_net_1x3x448x448.onnx");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            image = new Mat(image_path);
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            int newh = 0, neww = 0, padh = 0, padw = 0;
            Mat resize_img = Common.ResizeImage(image, inpHeight, inpWidth, ref newh, ref neww, ref padh, ref padw);
            float ratioh = (float)image.Rows / newh, ratiow = (float)image.Cols / neww;

            dt1 = DateTime.Now;

            //数据归一化处理
            BN_image = CvDnn.BlobFromImage(resize_img, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            //模型推理,读取推理结果
            Mat[] outs = new Mat[3] { new Mat(), new Mat(), new Mat() };
            string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

            opencv_net.Forward(outs, outBlobNames);

            List<Rect> position_boxes = new List<Rect>();
            List<float> confidences = new List<float>();
            List<List<OpenCvSharp.Point>> landmarks = new List<List<OpenCvSharp.Point>>();

            Common.GenerateProposal(inpHeight, inpWidth, reg_max, num_class, score_threshold, 40, 40, outs[0], position_boxes, confidences, landmarks, image.Rows, image.Cols, ratioh, ratiow, padh, padw);
            Common.GenerateProposal(inpHeight, inpWidth, reg_max, num_class, score_threshold, 20, 20, outs[1], position_boxes, confidences, landmarks, image.Rows, image.Cols, ratioh, ratiow, padh, padw);
            Common.GenerateProposal(inpHeight, inpWidth, reg_max, num_class, score_threshold, 80, 80, outs[2], position_boxes, confidences, landmarks, image.Rows, image.Cols, ratioh, ratiow, padh, padw);

            //NMS非极大值抑制
            int[] indexes = new int[position_boxes.Count];
            CvDnn.NMSBoxes(position_boxes, confidences, score_threshold, nms_threshold, out indexes);

            List<Rect> re_result = new List<Rect>();
            List<List<OpenCvSharp.Point>> re_landmarks = new List<List<OpenCvSharp.Point>>();
            List<float> re_confidences = new List<float>();

            for (int i = 0; i < indexes.Length; i++)
            {
                int index = indexes[i];
                re_result.Add(position_boxes[index]);
                re_landmarks.Add(landmarks[index]);
                re_confidences.Add(confidences[index]);
            }

            float[] gaze_yaw_pitch = new float[2];
            float length = (float)(image.Cols / 1.5);

            result_image = image.Clone();


            if (re_result.Count > 0)
            {
                sb.Clear();

                for (int i = 0; i < re_result.Count; i++)
                {
                    Mat crop_img = new Mat(result_image, re_result[i]);

                    gaze_predictor.Detect(crop_img, gaze_yaw_pitch);

                    //draw gaze	
                    float pos_x = (float)(re_result[i].X + 0.5 * re_result[i].Width);
                    float pos_y = (float)(re_result[i].Y + 0.5 * re_result[i].Height);

                    float dy = (float)(-length * Math.Sin(gaze_yaw_pitch[0]) * Math.Cos(gaze_yaw_pitch[1]));
                    float dx = (float)(-length * Math.Sin(gaze_yaw_pitch[1]));

                    OpenCvSharp.Point from = new OpenCvSharp.Point((int)pos_x, (int)pos_y);
                    OpenCvSharp.Point to = new OpenCvSharp.Point((int)(pos_x + dx), (int)(pos_y + dy));

                    Cv2.ArrowedLine(result_image, from, to, new Scalar(255, 0, 0), 2, 0, 0, 0.18);

                    Cv2.Rectangle(result_image, re_result[i], new Scalar(0, 0, 255), 2, LineTypes.Link8);
                    //Cv2.Rectangle(result_image, new OpenCvSharp.Point(re_result[i].X, re_result[i].Y), new OpenCvSharp.Point(re_result[i].X + re_result[i].Width, re_result[i].Y+ re_result[i].Height), new Scalar(0, 255, 0), 2);

                    Cv2.PutText(result_image, "face-" + re_confidences[i].ToString("0.00"),
                        new OpenCvSharp.Point(re_result[i].X, re_result[i].Y - 10),
                        HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);

                    foreach (var item in re_landmarks[i])
                    {
                        Cv2.Circle(result_image, item, 2, new Scalar(0, 255, 0), -1);
                    }

                    sb.AppendLine(string.Format("{0}:{1},({2},{3},{4},{5})"
                       , "face"
                       , re_confidences[i].ToString("0.00")
                       , re_result[i].TopLeft.X
                       , re_result[i].TopLeft.Y
                       , re_result[i].BottomRight.X
                       , re_result[i].BottomRight.Y
                       ));

                }

                dt2 = DateTime.Now;

                sb.AppendLine("--------------------------");
                sb.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");

                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                textBox1.Text = sb.ToString();
            }
            else
            {
                textBox1.Text = "无信息";
            }
        }

    }
}

参考

GitHub - Ahmednull/L2CS-Net: The official PyTorch implementation of L2CS-Net for gaze estimation and tracking

下载

可执行程序exe包0积分下载

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/112728.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

怎么监控钉钉聊天记录内容(监控钉钉聊天记录的3种形式)

企业沟通工具的普及&#xff0c;越来越多的企业开始使用钉钉作为内部沟通工具。然而&#xff0c;对于企业管理者来说&#xff0c;如何监控钉钉聊天记录内容成为了一个重要的问题。本文将介绍几种方法&#xff0c;帮助企业管理者实现监控钉钉聊天记录内容的目的。 一、钉钉自带功…

利用Vue2实现印章徽章组件

需要实现的组件效果&#xff1a; 该组件有设置颜色、大小、旋转度数和文本内容功能。 一、组件实现代码 <template><divclass"first-ring"v-bind"getBindValue":class"getStampBadgeClass":style"{ transform: rotate(${rotate}…

18.自监督视觉`transformer`模型DINO

文章目录 自监督视觉`transformer`模型DINO总体介绍DINO中使用的SSL和KD方法multicrop strategy损失函数定义`teacher`输出的中心化与锐化模型总体结构及应用reference欢迎访问个人网络日志🌹🌹知行空间🌹🌹 自监督视觉transformer模型DINO 总体介绍 论文:1.Emerging …

国际多语言出海商城源码/返佣产品自动匹配拼单商城源码

源码介绍&#xff1a; 国际多语言出海商城返佣产品自动匹配订单拼单商城源码&#xff0c;8国多语言出海拼单商城。此网站是很多巴西客户定制的原型&#xff0c;已投放运营符合当地本地化。 多语言商城返利返佣投资理财派单自带余额宝&#xff0c;采取全新支付端口&#xff0c…

Hadoop RPC简介

数新网络-让每个人享受数据的价值https://www.datacyber.com/ 前 言 RPC&#xff08;Remote Procedure Call&#xff09;远程过程调用协议&#xff0c;一种通过网络从远程计算机上请求服务&#xff0c;而不需要了解底层网络技术的协议。RPC它假定某些协议的存在&#xff0c;例…

师从IEEE Fellow|民办高校计算机专业教师自费赴美访学

D老师科研背景较弱&#xff0c;拟自费访学并带孩子出国就读&#xff0c;故要求申请到美国生活成本低且有较好公立中学教育资源的地区&#xff0c;并希望对方不收管理费。最终我们落实了德克萨斯大学达拉斯分校的邀请函&#xff0c;对方是IEEE Fellow、IET Fellow和EAI Fellow三…

链表的结点个数统计及查找

链表节点个数统计 要统计链表中的节点个数&#xff0c;只需要遍历整个链表&#xff0c;并在遍历的过程中计数即可。具体实现代码如下&#xff1a;(仍然使用C#) 先定义一个整型函数(节点个数的返回值一定是整型变量) int getLinkNodeNum(struct Test *head) {int cnt 0;whil…

类和对象(中)

目录 1.类的6个默认成员函数 2.构造函数 2.1 概念 2.2 特性 3.析构函数 3.1 概念 3.2 特性 4.拷贝构造函数 4.1 概念​ 4.2 特性 5.赋值运算符重载 5.1 运算符重载 &#xff08;重要&#xff09; 5.2 赋值运算符重载 5.3 Date类的其他运算符重载 6.const成员函数…

Linux开机、重启、关机和用户登录注销

1.【关机】 shutdown shutdown now 表示立即关机 shutdown -h now 表示立即关机 shutdown -h 1 表示1分钟后关机 halt 用来关闭正在运行的Linux操作系统 2.【重启】 shutdown -r now 表示立即重启 reboot 重启系统 sync …

2023年免费CRM软件盘点:14款热门工具全面比较(含开源)

在初创企业或小型企业阶段&#xff0c;特别是在预算有限且客户管理需求较为基础的情境下&#xff0c;使用免费的CRM系统通常是一个理智的选择。这类系统虽然在功能上可能不如付费版本丰富&#xff0c;但基本的客户信息管理、销售跟踪和沟通记录等核心功能通常都能满足需求。 对…

【Proteus仿真】【51单片机】贪吃蛇游戏

文章目录 一、功能简介二、软件设计三、实验现象联系作者 一、功能简介 本项目使用Proteus8仿真51单片机控制器&#xff0c;使用8*8LED点阵、按键模块等。 主要功能&#xff1a; 系统运行后&#xff0c;可操作4个按键控制小蛇方向。 二、软件设计 /* 作者&#xff1a;嗨小易…

蜜罐系统HFish的部署与功能实测

1. 引入 根据参考1对蜜罐的定义&#xff1a; 蜜罐&#xff08;Honeypot&#xff09;是一个计算机科学领域的术语&#xff0c;指用于检测或防御未经授权的行为或黑客攻击的陷阱。其名称来源于其工作原理类似于用来诱捕昆虫的蜜罐。蜜罐通常伪装成看似有利用价值的网路、资料、…

CHS零壹视频恢复程序高级版视频修复OCR使用方法

目前CHS零壹视频恢复程序监控版、专业版、高级版已经支持了OCR&#xff0c;OCR是一种光学识别系统&#xff0c;高级版最新版本中不仅仅是在视频恢复中支持OCR&#xff0c;同时视频修复模块也增加了OCR功能&#xff0c;此功能可以针对一些批量修复的视频文件&#xff08;如执法仪…

ubuntu(18.04)中架设HiGlass docker镜像服务,已尝试mcool、bedpe、wig格式文件

前言 使用到的软件 docker 文档 &#xff1a; https://www.docker.com/ HiGlass 文档&#xff1a;http://docs.higlass.io/higlass_docker.html#running-locally https://github.com/higlass/higlass-dockerhiglass-docker 地址&#xff1a;https://github.com/higla…

【JAVA学习笔记】 57 - 本章作业

项目代码 https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter14/src/com/yinhai/homework 1. (1)封装个新闻类&#xff0c;包含标题和内容属性&#xff0c;提供get, set方法&#xff0c; 重写toString方法&#xff0c;打印对象时只打印标题; (2)只提供…

【机器学习合集】模型设计之注意力机制动态网络 ->(个人学习记录笔记)

文章目录 注意力机制1. 注意力机制及其应用1.1 注意力机制的定义1.2 注意力机制的典型应用 2. 注意力模型设计2.1 空间注意力机制2.2 空间注意力模型2.3 通道注意力机制2.4 空间与通道注意力机制2.5 自注意力机制2.5 级联attention 动态网络1. 动态网络的定义2. 基于丢弃策略的…

小程序day02

目标 WXML模板语法 数据绑定 事件绑定 那麽問題來了&#xff0c;一次點擊會觸發兩個組件事件的話&#xff0c;該怎么阻止事件冒泡呢&#xff1f; 文本框和data的双向绑定 注意点: 只在标签里面用value“{{info}}”&#xff0c;只会是info到文本框的单向绑定&#xff0c;必须在…

安装docker报错:except yum.Errors.RepoError, e:

问题描述&#xff1a; 在安装docker的时候&#xff0c;配置阿里云地址出现以下问题 问题原因&#xff1a; linux 系统中存在多版本的python. yum 依赖 python 2, 而个人使用 python 3 导致. 解决办法&#xff1a; 修改 /usr/bin/yum-config-manager文件中第一行 #!/usr/bin/p…

arcgis图上添加发光效果!

看完本文, 你可以不借助外部图片素材, 让你的图纸符号表达出你想要的光! 我们以之前的某个项目图纸为例,来介绍下让符号发光的技术! 第一步—底图整理 准备好栅格影像底图、行政边界的矢量数据,确保“数据合适、位置正确、边界吻合”。 确定好图纸的大小、出图比例、投…

字体文件名称成中的Bold, Light,Italic,Regular, Medium是什么意思?

解释 字体文件名&#xff1a; IntelOneMono-Bold.ttf其中IntelOneMono字体名称 Bold 字体的样式 .ttf字体后缀 样式英文 中文Bold粗体BoldItalic粗体斜体Italic斜体Light细体LightItalic斜细体Medium中等MediumItalic中等斜体Regular标准以下来自鸿蒙字体以下来自鸿蒙字体TC…
最新文章