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.mzph.cn/news/126316.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hadoop hdfs的API调用,在mall商城代码中添加api的调用

在网上下载了现成的商城代码的源码 本次旨在熟悉hdfs的api调用&#xff0c;不关注前后端代码的编写&#xff0c;所以直接下载现成的代码&#xff0c;代码下载地址。我下载的是前后端在一起的代码&#xff0c;这样测试起来方便 GitHub - newbee-ltd/newbee-mall: &#x1f525; …

Seata入门系列【18】Seata集成Mybatis-Plus多数据源

1 前言 在使用单个服务&#xff0c;多数据源时&#xff0c;也存在分布式事务问题。 当单体系统需要访问多个数据库&#xff08;实例&#xff09;时就会产生分布式事务。 比如&#xff1a;用户信 息和订单信息分别在两个MySQL实例存储&#xff0c;用户管理系统删除用户信息&am…

驱动开发11-2 编写SPI驱动程序-点亮数码管

驱动程序 #include <linux/init.h> #include <linux/module.h> #include <linux/spi/spi.h>int m74hc595_probe(struct spi_device *spi) {printk("%s:%d\n",__FILE__,__LINE__);char buf[]{0XF,0X6D};spi_write(spi,buf,sizeof(buf));return 0; …

【SpringMVC篇】5种类型参数传递json数据传参

&#x1f38a;专栏【SpringMVC】 &#x1f354;喜欢的诗句&#xff1a;天行健&#xff0c;君子以自强不息。 &#x1f386;音乐分享【如愿】 &#x1f384;欢迎并且感谢大家指出小吉的问题&#x1f970; 文章目录 &#x1f33a;普通参数&#x1f33a;POJO参数&#x1f33a;嵌套…

2024年申报国自然项目基金撰写及技巧:基本要求和标准、项目撰写的方法和技巧、申请流程

随着社会经济发展和科技进步&#xff0c;基金项目对创新性的要求越来越高。申请人需要提出独特且有前瞻性的研究问题&#xff0c;具备突破性的科学思路和方法。因此&#xff0c;基金项目申请往往需要进行跨学科的技术融合。申请人需要与不同领域结合&#xff0c;形成多学科交叉…

Spring Boot 解决跨域问题的 5种方案

跨域问题本质是浏览器的一种保护机制&#xff0c;它的初衷是为了保证用户的安全&#xff0c;防止恶意网站窃取数据。 一、跨域三种情况 在请求时&#xff0c;如果出现了以下情况中的任意一种&#xff0c;那么它就是跨域请求&#xff1a; 协议不同&#xff0c;如 http 和 https…

nacos 常见问题整理包含容器环境

文章目录 0. nacos客户端日志文件位置最常见的问题1. 容器环境端口开放不够导致的问题原理解析 2.服务端启用了鉴权客户端常见错误信息如下服务端报错信息如下 其他一些问题0. nacos高版本服务端是否支持旧的客户端&#xff1f;1. Error code:503,msg:server is DOWN now, plea…

【UE5】如何在UE5.1中创建级联粒子系统

1. 可以先新建一个actor蓝图&#xff0c;然后在该蓝图中添加一个“Cascade Particle System Component” 2. 在右侧的细节面板中&#xff0c;点击“模板”一项中的下拉框&#xff0c;然后点击“Cascade粒子系统&#xff08;旧版&#xff09;” 然后就可以选择在哪个路径下创建级…

前端Vue框架系列—— 学习笔记总结Day04

❤ 作者主页&#xff1a;欢迎来到我的技术博客&#x1f60e; ❀ 个人介绍&#xff1a;大家好&#xff0c;本人热衷于Java后端开发&#xff0c;欢迎来交流学习哦&#xff01;(&#xffe3;▽&#xffe3;)~* &#x1f34a; 如果文章对您有帮助&#xff0c;记得关注、点赞、收藏、…

Java NIO为何导致堆外内存OOM了?

Java NIO为何导致堆外内存OOM了&#xff1f; 描述 某天报警&#xff1a;某台机器部署的一个服务突然无法访问。谨记第一反应登录机器查看日志&#xff0c;因为服务挂掉&#xff0c;很可能因OOM。这个时候在机器的日志中发现了如下的一些信息&#xff1a; nio handle failed j…

C++对象和类

类的基础 1.cpp中对象默认访问权限都是private的 2.私有成员只能通过公有函数访问使用 3.类方法名称需包含类名&#xff0c;为函数的限定名 在头文件中定义类&#xff0c;同时将公有方法原型声明(类似Java接口道理). #ifndef STOCK00_H_ #define STOCK00_H_ #include<s…

Leetcode—485.最大连续1的个数【中等】明天修改

2023每日刷题&#xff08;十五&#xff09; Leetcode—2.两数相加 迭代法实现代码 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l…

Python使用got库如何写一个爬虫代码?

got库是一个Python的HTTP库&#xff0c;可以用于爬取网页数据。它提供了简单易用的API&#xff0c;支持异步请求和爬虫IP设置等功能。使用got库进行爬虫开发&#xff0c;可以快速地获取所需数据。下面是使用got库进行爬虫的基本步骤&#xff1a; 1、安装got库&#xff1a;可以使…

LeetCode454. 4Sum II

文章目录 一、题目二、题解 一、题目 Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 < i, j, k, l < n nums1[i] nums2[j] nums3[k] nums4[l] 0 Example 1: Input: nums1 …

sort函数用法

1、基本数据类型数组的排序 若比较函数不填&#xff0c;默认升序排序。 #include <stdio.h> #include <algorithm> using namespace std; int main() { int a[5] {3,5,2,1,4}; sort(a,a5); for(int i0;i<5;i) { printf("%d",a[i]); } r…

PTA 秀恩爱分得快(树)

题目 古人云&#xff1a;秀恩爱&#xff0c;分得快。 互联网上每天都有大量人发布大量照片&#xff0c;我们通过分析这些照片&#xff0c;可以分析人与人之间的亲密度。如果一张照片上出现了 K 个人&#xff0c;这些人两两间的亲密度就被定义为 1/K。任意两个人如果同时出现在…

ruoyi框架前端修改message消失时间

修改教程 打开modal.js文件&#xff0c;找到Message.success&#xff0c;然后把参数设置进去就行。单位是10001秒。 // 可以设置的参数如下export interface ElMessageOptions {/** Message text */message: string | VNode/** Message type */type?: MessageType/** Custom …

4.5 Object类

思维导图&#xff1a; 4.5 Object类笔记总结 1. 定义和重要性 Java提供的Object类是所有Java类的根类。直接或间接&#xff0c;所有的Java类都继承自Object类。它被称为超类。 2. 默认行为 当创建一个新的类且没有显式地使用extends关键字指定一个父类时&#xff0c;该类默认…

Microsoft Edge不能工作了,可能原因不少,那么如何修复呢

Microsoft Edge打不开或不能加载网页是用户在Windows 10、Android、Mac和iOS设备上的网络浏览器上遇到的许多错误之一。其他Microsoft Edge问题可能包括浏览器窗口和选项卡冻结、网站崩溃、互联网连接错误消息以及丢失Microsoft Edge书签、收藏夹、密码和收藏。 Microsoft Edg…

金蝶云星空自定义校验器和使用

文章目录 金蝶云星空自定义校验器和使用 金蝶云星空自定义校验器和使用 1、创建类&#xff0c;并继承抽象接口 using Kingdee.BOS.Core; using Kingdee.BOS.Core.Validation; using System;namespace mm.K3.SCM.App.Service.PlugIn.SC.Validator {public class AfterOrderChe…