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

ops/2025/1/21 18:25:43/

目录

介绍

效果

模型信息

项目

代码

下载


介绍

github地址:https://github.com/derronqi/yolov8-face

yolov8 face detection with landmark

效果

模型信息

Model Properties

YOLOv8litetpose_model_trained_on_widerfaceyaml%0AauthorUltralytics%0Akpt_shape5_3%0Ataskpose%0AlicenseAGPL30_httpsultralyticscomlicense%0Aversion8085%0Astride32%0Abatch1%0Aimgsz640_640%0Anames0_face_32">description:Ultralytics YOLOv8-lite-t-pose model trained on widerface.yaml
author:Ultralytics
kpt_shape:[5, 3]
task:pose
license:AGPL-3.0 https://ultralytics.com/license
version:8.0.85
stride:32
batch:1
imgsz:[640, 640]
names:{0: ‘face’}

Inputs

name:images
tensor:Float[1, 3, 640, 640]

Outputs

name:output0
tensor:Float[1, 80, 80, 80]
name:884
tensor:Float[1, 80, 40, 40]
name:892
tensor:Float[1, 80, 20, 20]

项目

代码

GenerateProposal函数

public static unsafe void GenerateProposal (int inpHeight, int inpWidth, int reg_max, int num_class, float score_threshold, int feat_h, int feat_w, Mat output, List position_boxes, List confidences, List<List<OpenCvSharp.Point>> landmarks, int imgh, int imgw, float ratioh, float ratiow, int padh, int padw)
{
int stride = (int)Math.Ceiling((double)(inpHeight / feat_h));
int area = feat_h /* feat_w;

float/* ptr = (float/*)output.DataStart;
float/* ptr_cls = ptr + area /* reg_max /* 4;
float/* ptr_kp = ptr + area /* (reg_max /* 4 + num_class);for (int i = 0; i < feat_h; i++)
{for (int j = 0; j < feat_w; j++){int cls_id = -1;float max_conf = -10000;int index = i /* feat_w + j;for (int k = 0; k < num_class; k++){float conf = ptr_cls[k /* area + index];if (conf > max_conf){max_conf = conf;cls_id = k;}}float box_prob = Common.sigmoid_x(max_conf);if (box_prob > score_threshold){float[] pred_ltrb = new float[4];float[] dfl_value = new float[reg_max];float[] dfl_softmax = new float[reg_max];for (int k = 0; k < 4; k++){for (int n = 0; n < reg_max; n++){dfl_value[n] = ptr[(k /* reg_max + n) /* area + index];}Common.softmax_(ref dfl_value, ref dfl_softmax, reg_max);float dis = 0f;for (int n = 0; n < reg_max; n++){dis += n /* dfl_softmax[n];}pred_ltrb[k] = dis /* stride;}float cx = (j + 0.5f) /* stride;float cy = (i + 0.5f) /* stride;float xmin = Math.Max((cx - pred_ltrb[0] - padw) /* ratiow, 0f);  ///还原回到原图float ymin = Math.Max((cy - pred_ltrb[1] - padh) /* ratioh, 0f);float xmax = Math.Min((cx + pred_ltrb[2] - padw) /* ratiow, (float)(imgw - 1));float ymax = Math.Min((cy + pred_ltrb[3] - padh) /* ratioh, (float)(imgh - 1));Rect box = new Rect((int)xmin, (int)ymin, (int)(xmax - xmin), (int)(ymax - ymin));position_boxes.Add(box);confidences.Add(box_prob);List<OpenCvSharp.Point> kpts = new List<OpenCvSharp.Point>();for (int k = 0; k < 5; k++){float x = ((ptr_kp[(k /* 3) /* area + index] /* 2 + j) /* stride - padw) /* ratiow;  ///还原回到原图float y = ((ptr_kp[(k /* 3 + 1) /* area + index] /* 2 + i) /* stride - padh) /* ratioh;kpts.Add(new OpenCvSharp.Point((int)x, (int)y));}landmarks.Add(kpts);}}
}

}

	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.ppmy.cn/ops/151969.html

相关文章

C语言内存之旅:从静态到动态的跨越

大家好&#xff0c;这里是小编的博客频道 小编的博客&#xff1a;就爱学编程 很高兴在CSDN这个大家庭与大家相识&#xff0c;希望能在这里与大家共同进步&#xff0c;共同收获更好的自己&#xff01;&#xff01;&#xff01; 本文目录 引言正文一 动态内存管理的必要性二 动态…

彻底讲清楚 单体架构、集群架构、分布式架构及扩展架构

目录 什么是系统架构 单体架构 介绍 示例图 优点 缺点 集群架构 介绍 示意图 优点 缺点 分布式架构 示意图 优点 缺点 生态扩展 介绍 示意图 优点 缺点 扩展&#xff1a;分布式服务解析 纵切拆服务 全链路追踪能力 循环依赖 全链路日志&#xff08;En…

PyCharm中解决依赖冲突

1. 创建虚拟环境 确保为项目创建了一个虚拟环境&#xff0c;这样可以隔离项目的依赖&#xff0c;避免全局依赖冲突。 检查当前项目的 Python 环境 打开 PyCharm。点击菜单栏的 File > Settings > Project: [Your Project Name] > Python Interpreter。确保已选择一…

AI 之网:网络诈骗者的 “高科技伪装术”—— 智能诈骗的神秘面纱

本篇文章博主将以AI的反面应用为例&#xff1b;配合代码辅助说明&#xff1b;带大家了解背后的“黑面纱”&#xff1b;也同时希望大家能够正反结合&#xff1b;不要误入歧途。 &#xff1a;羑悻的小杀马特.-CSDN博客羑悻的小杀马特.擅长C/C题海汇总,AI学习,c的不归之路,等方面的…

栈和队列(C语言)

目录 数据结构之栈 定义 实现方式 基本功能实现 1&#xff09;定义&#xff0c;初始化栈 2&#xff09;入栈 3&#xff09;出栈 4&#xff09;获得栈顶元素 5)获得栈中有效元素个数 6&#xff09;检测栈是否为空 7&#xff09;销毁栈 数据结构之队列 定义 实现方…

Jupyter notebook中运行dos指令运行方法

Jupyter notebook中运行dos指令运行方法 目录 Jupyter notebook中运行dos指令运行方法一、DOS(磁盘操作系统&#xff09;指令介绍1.1 DOS介绍1.2 DOS指令1.2.1 DIR - 显示当前目录下的文件和子目录列表。1.2.2 CD 或 CHDIR - 改变当前目录1.2.3 使用 CD .. 可以返回上一级目录1…

PHP企业IM客服系统

&#x1f4ac; 企业IM客服系统——高效沟通&#xff0c;无缝连接的智慧桥梁 &#x1f680; 卓越性能&#xff0c;释放无限可能 在瞬息万变的商业环境中&#xff0c;我们深知沟通的力量。因此&#xff0c;基于先进的ThinkPHP5框架与高性能的Swoole扩展&#xff0c;我们匠心独运…

matlab实现了一个完整的语音通信系统的模拟,包括语音信号的读取、编码(PCM 和汉明码)、调制

% step 1: 读入语音信号,并进行归一化处理 [audio, fs] = audioread(D:\txyp4.m4a); len_speech = length(audio); % 计算语音信号的采样点个数