C# 人脸检测 人脸比对 活体检测 口罩检测 年龄预测 性别预测 眼睛状态检测
基于以下开源软件做了一个Demo
GitHub - ViewFaceCore/ViewFaceCore: C# 超简单的离线人脸识别库。( 基于 SeetaFace6 )
效果
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ViewFaceCore.Core;
using ViewFaceCore.Model;
using System.Drawing;
using System.Diagnostics;
namespace ViewFaceCoreDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.ImageLocation = ofd.FileName;
}
private void button5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox2.ImageLocation = ofd.FileName;
}
/// <summary>
/// 人脸检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
FaceDetector faceDetector = new FaceDetector();
Bitmap bitmap = new Bitmap(pictureBox1.Image);
FaceImage faceImage = bitmap.ToFaceImage();
FaceInfo[] infos = faceDetector.Detect(faceImage);
if (infos.Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
return;
}
//Console.WriteLine("识别到的人脸数量:" + infos.Length + " 个人脸信息:\n");
//Console.WriteLine("No.\t人脸置信度\t位置信息");
//for (int i = 0; i < infos.Length; i++)
//{
// Console.WriteLine(String.Format("{0}\t{1}\t{2}", i, infos[i].Score, infos[i].Location));
//}
//画方框,标记人脸
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawRectangles(new Pen(Color.Red, 2), infos.Select(p => new RectangleF(p.Location.X, p.Location.Y, p.Location.Width, p.Location.Height)).ToArray());
}
pictureBox1.Image = bitmap;
}
/// <summary>
/// 活体检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
string msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
FaceDetector faceDetector = new FaceDetector();
FaceLandmarker faceMark = new FaceLandmarker();
FaceAntiSpoofing faceAntiSpoofing = new FaceAntiSpoofing();
if (faceDetector.Detect(bitmap).Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
return;
}
var info = faceDetector.Detect(bitmap).First();
var markPoints = faceMark.Mark(bitmap, info);
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
var result = faceAntiSpoofing.AntiSpoofing(bitmap, info, markPoints);
msg = "活体检测,结果:" + result.Status + ",清晰度:" + result.Clarity + ",真实度:" + result.Reality + ",耗时:" + sw.ElapsedMilliseconds + "ms";
richTextBox1.Text += msg + "\r\n";
Console.WriteLine(msg);
sw.Stop();
}
/// <summary>
/// 人脸比对
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
String msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
if (pictureBox2.Image == null)
{
msg = "图片2不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
var faceImage0 = new Bitmap(pictureBox1.Image).ToFaceImage();
var faceImage1 = new Bitmap(pictureBox2.Image).ToFaceImage();
//检测人脸信息
FaceDetector faceDetector = new FaceDetector();
FaceInfo[] infos0 = faceDetector.Detect(faceImage0);
FaceInfo[] infos1 = faceDetector.Detect(faceImage1);
if (infos0.Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
sw.Stop();
return;
}
if (infos1.Length == 0)
{
msg = "图片2未检测到人脸";
richTextBox1.Text += msg + "\r\n";
sw.Stop();
return;
}
//标记人脸位置
FaceLandmarker faceMark = new FaceLandmarker();
FaceMarkPoint[] points0 = faceMark.Mark(faceImage0, infos0[0]);
FaceMarkPoint[] points1 = faceMark.Mark(faceImage1, infos1[0]);
//提取特征值
FaceRecognizer faceRecognizer = new FaceRecognizer();
float[] data0 = faceRecognizer.Extract(faceImage0, points0);
float[] data1 = faceRecognizer.Extract(faceImage1, points1);
//对比特征值
bool isSelf = faceRecognizer.IsSelf(data0, data1);
//计算相似度
float similarity = faceRecognizer.Compare(data0, data1);
msg = "识别到的人脸是否为同一人:" + isSelf + ",相似度:" + similarity + ",对比耗时:" + sw.ElapsedMilliseconds + "ms";
richTextBox1.Text += msg + "\r\n";
Console.WriteLine(msg);
sw.Stop();
}
/// <summary>
/// 口罩检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
FaceDetector faceDetector = new FaceDetector();
MaskDetector maskDetector = new MaskDetector();
if (faceDetector.Detect(bitmap).Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
return;
}
var info = faceDetector.Detect(bitmap).First();
PlotMaskResult plotMaskResult = maskDetector.PlotMask(bitmap, info);
if (plotMaskResult.Masked)
{
richTextBox1.Text += "口罩:是 \r\n";
}
else
{
richTextBox1.Text += "口罩:否 \r\n";
}
Console.WriteLine(msg);
}
/// <summary>
/// 年龄预测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
string msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
FaceDetector faceDetector = new FaceDetector();
if (faceDetector.Detect(bitmap).Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
return;
}
var info = faceDetector.Detect(bitmap).First();
AgePredictor agePredictor = new AgePredictor();
FaceLandmarker faceMark = new FaceLandmarker();
var markPoints = faceMark.Mark(bitmap, info);
int age = agePredictor.PredictAge(bitmap, markPoints);
richTextBox1.Text += "年龄:" + age + " \r\n";
Console.WriteLine(msg);
}
/// <summary>
/// 性别预测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
string msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
FaceDetector faceDetector = new FaceDetector();
if (faceDetector.Detect(bitmap).Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
return;
}
var info = faceDetector.Detect(bitmap).First();
GenderPredictor genderPredictor = new GenderPredictor();
FaceLandmarker faceMark = new FaceLandmarker();
var markPoints = faceMark.Mark(bitmap, info);
Gender gender = genderPredictor.PredictGender(bitmap, markPoints);
richTextBox1.Text += "性别:" + gender + " \r\n";
Console.WriteLine(msg);
}
/// <summary>
/// 眼睛状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
string msg = "";
if (pictureBox1.Image == null)
{
msg = "图片1不能为空";
richTextBox1.Text += msg + "\r\n";
return;
}
FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
FaceDetector faceDetector = new FaceDetector();
if (faceDetector.Detect(bitmap).Length == 0)
{
msg = "图片1未检测到人脸";
richTextBox1.Text += msg + "\r\n";
return;
}
var info = faceDetector.Detect(bitmap).First();
EyeStateDetector eyeStateDetector = new EyeStateDetector();
FaceLandmarker faceMark = new FaceLandmarker();
var markPoints = faceMark.Mark(bitmap, info);
EyeStateResult eyeStateResult = eyeStateDetector.Detect(bitmap, markPoints);
richTextBox1.Text += "眼睛状态:" + eyeStateResult + " \r\n";
Console.WriteLine(msg);
}
}
}
C# 人脸检测 人脸比对 活体检测 口罩检测 年龄预测 性别预测 眼睛状态检测的更多相关文章
- python——进行年龄和性别检测
年龄和性别检测 使用Python编程语言带你完成使用机器学习进行年龄和性别检测的任务. 首先需要编写用于检测人脸的代码,因为如果没有人脸检测,我们将无法进一步完成年龄和性别预测的任务. 下一步是预测图 ...
- 虹软人脸识别 - faceId及IR活体检测的更新介绍
虹软人脸识别 - faceId及IR活体检测的介绍 前几天虹软推出了 Android ArcFace 2.2版本的SDK,相比于2.1版本,2.2版本中的变化如下: VIDEO模式新增faceId(类 ...
- python3 百度AI-v3之 人脸对比 & 人脸检测 & 在线活体检测 接口
#!/usr/bin/python3 # 百度人脸对比 & 人脸检测api-v3 import sys, tkinter.messagebox, ast import ssl, json,re ...
- 虹软人脸识别 - faceId及IR活体检测的介绍
虹软人脸识别 - faceId及IR活体检测的介绍 前几天虹软推出了 Android ArcFace 2.2版本的SDK,相比于2.1版本,2.2版本中的变化如下: VIDEO模式新增faceId(类 ...
- iOS开发中使用CIDetector检测人脸
在iOS5 系统中,苹果就已经有了检测人脸的api,能够检测人脸的位置,包括左右眼睛,以及嘴巴的位置,返回的信息是每个点位置.在 iOS7中,苹果又加入了检测是否微笑的功能.通过使用 CIDetect ...
- UWP 使用Windows.Media.FaceAnalysis.FaceDetector检测人脸
话说现在检测人脸的技术有很多.有在线AI服务,比如Megvii Face++,Microsoft Cognitive Services,Tencent AI等等.还有本地的库实现的,比如OpenCV. ...
- python opencv3 静态图片检测人脸
git:https://github.com/linyi0604/Computer-Vision # coding:utf-8 import cv2 filename = "../data/ ...
- PCA检测人脸的简单示例_matlab实现
PCA检测人脸的简单示例,matlab R2009b上实现训练:训练用的20副人脸: %训练%Lx=X'*Xclear;clc;train_path='..\Data\TrainingSet\';ph ...
- 使用dlib基于CNN(卷积神经网络)的人脸检测器来检测人脸
基于机器学习CNN方法来检测人脸比之前介绍的效率要慢很多 需要先下载一个训练好的模型数据: 地址点击下载 // dlib_cnn_facedetect.cpp: 定义控制台应用程序的入口点. // # ...
- 20行Python代码检测人脸是否佩戴口罩
最近,口罩成为绝对热门的话题,在疫情之下,出门不戴口罩不仅对自己不负责,对他人而言也是一种潜在的威胁.所以许多小区都有保安在门口守着,谁要是不戴口罩就吼回去(吓死我了). 很多人学习python,不知 ...
随机推荐
- Python Print 显示颜色
1.顺序:显示方式,前景颜色,背景颜色 2.顺序非固定,但尽量按默认书写方式 3.也可以在input中输出使用 4.格式: print('\033[显示方式:前景颜色:背景颜色m .......... ...
- 基于javaweb的个人音乐网站
一 .需求 ① 前台用户: 用户注册和登录以及完善个人资料: 添加好友功能(用户之间可通过发送好友申请添加好友,被添加人可以同意或拒绝好友申请): 下载歌曲功能(当用户点击下载按钮时,就能下载相对应的 ...
- 51从零开始用Rust编写nginx,江湖救急,TLS证书快过期了
wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 负载均衡, 静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实 ...
- 【Azure Developer】使用 Microsoft Graph API查看用户状态和登录记录
问题描述 通过Microsoft Graph的API如何来查看用户信息和登录记录呢? 问题解答 第一步:需要一个授权Token 比如一个拥有查看用户权限的Azure账号,通过Azure CLI 命令获 ...
- 【Azure 应用服务】App Service"访问控制/流量监控"四问
问题描述 一问:App Service有那些访问限制的方式 二问:访问限制中,是否可以通过域名来进行限制,而不只是IP地址 三问:App Service如何查看到访问者(客户端)的IP地址,访问时间 ...
- 【Azure 应用服务】在Azure App Service多实例的情况下,如何在应用中通过代码获取到实例名(Instance ID)呢?
问题描述 App Service开启多实例后,如何在代码中获取当前请求所真实到达的实例ID(Instance ID)呢? 问题答案 App Service 通过 环境变量的方式 显示的输出实例ID等信 ...
- C# 多线程(17):小总结
前言 本篇内容是小总结和过渡,看完这篇后,就要开始继续学习 C# 多线程中的知识点啦~. 前面,经过 16 篇的学习,我们学习了多线程.锁.线程池.任务.同步.异步等知识,还没有使用到 async.a ...
- STL-string模拟实现
1 #pragma once 2 3 #include<iostream> 4 #include<string.h> 5 #include<assert.h> 6 ...
- Vue3音乐播放器组件,可显示歌词
在线体验地址 音乐播放器 1,安装 npm install apple-music-player 或 yarn add apple-music-player 2,在main.ts中引入 import ...
- kafka---面经
kafka深入理解 消息队列 作用,优点? 异步:比如查看文章,点赞收藏评论等操作,提升文章热度,提升个人社区贡献度,提升个人社区积分,刷新社区贡献度排行榜.将其他操作放到消息队列,相应的模块从消息队 ...