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

C# OpenCvSharp Yolov8 Face Landmarks 人脸特征检测

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace OpenCvSharp_Yolov8_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;private void Form1_Load(object sender, EventArgs e){startupPath = System.Windows.Forms.Application.StartupPath;model_path = startupPath + "\\yolov8-lite-t.onnx";//初始化网络类,读取本地模型opencv_net = CvDnn.ReadNetFromOnnx(model_path);}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;//数据归一化处理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();dt1 = DateTime.Now;opencv_net.Forward(outs, outBlobNames);dt2 = DateTime.Now;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]);}if (re_result.Count > 0){sb.Clear();sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");sb.AppendLine("--------------------------");//将识别结果绘制到图片上result_image = image.Clone();for (int i = 0; i < re_result.Count; i++){Cv2.Rectangle(result_image, re_result[i], new Scalar(0, 0, 255), 2, LineTypes.Link8);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, 4, 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));}pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());textBox1.Text = sb.ToString();}else{textBox1.Text = "无信息";}}}
}

下载

exe可执行程序包免费下载

源码下载

http://www.lryc.cn/news/208250.html

相关文章:

  • 计算机网络之数据链路层(全)
  • 前端TypeScript学习-交叉类型与泛型
  • 科聪协作(复合)移动机器人整体解决方案
  • RTE(Runtime Environment)
  • 搭建自己的搜索引擎——oh-my-search使用
  • 微信小程序实现文章内容详情
  • 行情分析——加密货币市场大盘走势(10.27)
  • 设计模式:桥接模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
  • error: the following arguments are required: --model, --data 解决方法
  • Kafka - 消息队列的两种模式
  • 【Go】格式化字符串指令大全 Redis常用命令
  • Windows 和 Linux 这2个系统在进行编程实现的时候的一些区别:
  • [SQL开发笔记]SQL 别名:为表名称或列名称指定别名
  • 风险管理案例题
  • NFC读卡器ST25R3911B-AQWT、ST25R3917B-AQET、ST25R3919B-AQET产品描述、功能框图
  • JVM进阶(2)
  • 2023大湾区杯粤港澳金融数学建模竞赛思路+模型+代码
  • 【Note详细图解】中缀表达式如何转为后缀表达式?数据结构
  • 常用到的资源共享网站
  • 关于JAVA中字节码文件版本号、产品版本号及开发版本号的关系
  • ModbusTCP 转 Profinet 主站网关在博图配置案例
  • 抖音上怎么挂小程序?制作小程序挂载抖音视频
  • AI新能量!FortiGate NGFW面向数据中心全面集成FortiGuard AI 安全服务
  • Git总结
  • 初级前端面试题(一) 之 html/css/js
  • python实现excel的数据提取
  • Vue的MVVM实现原理
  • vue+iView 动态侧边栏菜单保持高亮选中
  • 标准的听觉检测环境应满足哪些条件?
  • Fabric.js 样式不更新怎么办?