一、介绍
百度AI人脸识别是一项基于深度学习的人脸识别技术,具有高度的准确性和广泛的应用场景。无论是身份认证、门禁考勤、安防监控还是智能相册分类,百度AI人脸识别都能满足各种需求,为用户提供更好的体验。
1.功能
(1)人脸检测
能够精准定位图中人脸,获得眼、口、鼻等72个关键点位置。
能够分析性别、年龄、表情等多种人脸属性。
(2)人脸对比
对比两张人脸的相似度,并给出相似度评分,从而判断是否同一个人。
(3)人脸查找
针对一张人脸照片,在指定人脸集合中搜索,找出最相似的一张脸或多张人脸,并给出相似度分值。
2.应用场景
远程身份认证、刷脸门禁考勤、安防监控、智能相册分类、人脸美颜等
二、功能介绍
在本文中我使用WindowsForms的窗体应用项目实现了人脸识别和人脸对比这两个功能。
人脸识别功能允许用户导入一张照片并对照片中的人脸进行年龄检测和颜值测评;人脸对比功能能够对两张照片进行对比识别,判断两张照片中的人是否是同一个人,即通常所说的人脸识别。
三、功能代码实现
1.使用的控件
在窗体设计界面我使用了三个Button控件,分别执行“选择人脸图”、“识别图片”、“人脸对比”的功能;同时还使用了三个textBox控件,其中两个textBox控件用来接收“选择人脸图”按键传输来的图片数据,另一个textBox控件用来显示识别图片”和“人脸对比”的结果。
2.需要的包
在编写实现功能的代码前我们先要导入几个功能包,以便简化我们的代码并使代码功能正常运行。
需要的功能包有:AForge、Newtonsoft.Json和Baidu.AI等。其中Baidu.AI包能够让程序对图片中的人脸进行识别和对比,是必不可少的。
3.各功能的代码实现
(1)“选择人脸图”功能
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "C:\\Users\\Administrator\\Desktop";
dialog.Filter = "所有文件|*.*";
dialog.RestoreDirectory = true;
dialog.FilterIndex = 2;
if (dialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = dialog.FileName;
}
else
{
textBox2.Text = dialog.FileName;
}
}
}
(2)“识别图片”功能
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory ="C:\\Users\\Administrator\\Desktop" ;
dialog.Filter = "所有文件|*.*";
dialog.RestoreDirectory = true;
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == DialogResult.OK)
{
string filename = dialog.FileName;
try
{
Image im = Image.FromFile(filename);
var image = ConvertImageToBase64(im);
string imageType = "BASE64";
// 如果有可选参数
var options = new Dictionary<string, object>{
//{"max_face_num", 2},
{"face_field", "age,beauty"},
{"face_fields", "age,qualities,beauty"}
};
var options1 = new Dictionary<string, object>{
{"face_field", "age"},
{"max_face_num", 2},
{"face_type", "LIVE"},
{"liveness_control", "LOW"}
};
var result = client.Detect(image, imageType, options);
textBox3.Text = result.ToString();
//FaceDetectInfo detect = JsonHelper.DeserializeObject<FaceDetectInfo>(result.ToString());
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
}
(3)“人脸对比”功能
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("请选择要对比的人脸图片");
return;
}
try
{
string path1 = textBox1.Text;
string path2 = textBox2.Text;
var faces = new JArray
{
new JObject
{
{"image", ReadImg(path1)},
{"image_type", "BASE64"},
{"face_type", "LIVE"},
{"quality_control", "LOW"},
{"liveness_control", "NONE"},
},
new JObject
{
{"image", ReadImg(path2)},
{"image_type", "BASE64"},
{"face_type", "LIVE"},
{"quality_control", "LOW"},
{"liveness_control", "NONE"},
}
};
// 带参数调用人脸比对
var result = client.Match(faces);
textBox3.Text = result.ToString();
}
catch (Exception ex)
{ }
}
4.代码实现的结果
(1)“识别图片”功能
(2)“人脸对比”功能
5.整体代码
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using Baidu.Aip.Face;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace WindowsForms作业3
{
public partial class Form1 : Form
{
private string APP_ID = "33711276";
private string API_KEY = "1nhS4AcrLFffxqDKB73bb1U7";
private string SECRET_KEY = "rtnRFv5RcE0dMxBWqmL6PX3LYwVAdETW";
private Face client = null;
public Form1()
{
client = new Face(API_KEY, SECRET_KEY);
InitializeComponent();
}
public string ConvertImageToBase64(Image file)
{
using (MemoryStream memoryStream = new MemoryStream())
{
file.Save(memoryStream, file.RawFormat);
byte[] imageBytes = memoryStream.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "C:\\Users\\Administrator\\Desktop";
dialog.Filter = "所有文件|*.*";
dialog.RestoreDirectory = true;
dialog.FilterIndex = 2;
if (dialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = dialog.FileName;
}
else
{
textBox2.Text = dialog.FileName;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory ="C:\\Users\\Administrator\\Desktop" ;
dialog.Filter = "所有文件|*.*";
dialog.RestoreDirectory = true;
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == DialogResult.OK)
{
string filename = dialog.FileName;
try
{
Image im = Image.FromFile(filename);
var image = ConvertImageToBase64(im);
string imageType = "BASE64";
// 如果有可选参数
var options = new Dictionary<string, object>{
//{"max_face_num", 2},
{"face_field", "age,beauty"},
{"face_fields", "age,qualities,beauty"}
};
var options1 = new Dictionary<string, object>{
{"face_field", "age"},
{"max_face_num", 2},
{"face_type", "LIVE"},
{"liveness_control", "LOW"}
};
var result = client.Detect(image, imageType, options);
textBox3.Text = result.ToString();
//FaceDetectInfo detect = JsonHelper.DeserializeObject<FaceDetectInfo>(result.ToString());
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
}
public string ReadImg(string img)
{
return Convert.ToBase64String(File.ReadAllBytes(img));
}
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("请选择要对比的人脸图片");
return;
}
try
{
string path1 = textBox1.Text;
string path2 = textBox2.Text;
var faces = new JArray
{
new JObject
{
{"image", ReadImg(path1)},
{"image_type", "BASE64"},
{"face_type", "LIVE"},
{"quality_control", "LOW"},
{"liveness_control", "NONE"},
},
new JObject
{
{"image", ReadImg(path2)},
{"image_type", "BASE64"},
{"face_type", "LIVE"},
{"quality_control", "LOW"},
{"liveness_control", "NONE"},
}
};
// 带参数调用人脸比对
var result = client.Match(faces);
textBox3.Text = result.ToString();
}
catch (Exception ex)
{ }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
代码网址:https://gitee.com/ydn777/baidu-ai-facial-recognition
四、总结
本次作业我使用WindowsForms的窗体应用项目实现了人脸识别和人脸对比这两个功能。首先在设计界面拖入需要的控件,然后点击进入相应的控件并编写各个功能的代码。需要注意的是我们要先下载好要用到的包来简化我们的代码并确保代码的正常实现。