赞
踩
目录
基于以下开源软件做了一个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);
}
}
}
- 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);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。