调用百度AI的人脸识别,实现人脸检测、人脸对比、人脸登录
使用C#和百度AI接口实现人脸检测、人脸比对、人脸注册、人脸登录等功能。通过检测图像中的人脸,提取并分析人脸的质量属性,如年龄、模糊度和光照等,确保图像质量符合要求,并通过比较两张人脸图像来确定相似度。
调用百度AI的人脸识别,实现人脸搜索、人脸对比、人脸登录
目录
百度云网址 https://cloud.baidu.com/
一、系统背景及意义
随着计算机视觉技术及人工智能算法的蓬勃发展,人脸识别的概念逐渐被应用到日常生活场景和商业场景中来;另一方面,百度、阿里、腾讯等互联网技术平台提出云服务的概念,将已有技术共享到云端,以接口的形式对普通大众提供服务。
本程序通过调用百度AI的人脸识别,实现人脸检测、人脸对比、人脸登录等功能。
二、采取的技术
本程序的最终实现形式为Windows桌面程序,本程序主要采用微软的Winform模式进行开发,编程语言为C#。开发所使用的IDE为Visual Studio,辅助工具为Postman、son格式转换工具、Base64编码工具等。
除此之外,本程序还用到了异步技术、Base64编码技术、Json相关技术、Http传输技术等。其中,异步技术用于实时监测摄像头中出现的图像,Base64编码技术用于对图像进行编码、Json相关技术用于对数据进行统一格式处理、Http传输相关技术用于本程序与百度云服务进行通信。
三、编写目的
引导使用者部署、配置、使用人脸识别小程序。
四、运行环境
硬件:带有摄像头的计算机。
软件:Windows10 操作系统。
网络:设备连接外网。
五、注册百度AI,领取免费权限
(1)注册登录百度智能云
百度云网址 https://cloud.baidu.com/
(2)点击创建应用
(3)开通人脸识别相关资源
六、代码实现
(1)winform界面设计
(2)代码实现
1.导入创建的百度人脸识别应用信息
private string APP_ID = "你的APP_ID";
private string API_KEY = "你的API_KEY";
private string SECRET_KEY = "你的SECRET_KEY";
private Face client = null;
/// <summary>
/// 是否可以检测人脸
/// </summary>
private bool IsStart = false;
/// <summary>
/// 人脸在图像中的位置
/// </summary>
private FaceLocation location = null;
private FilterInfoCollection videoDevices = null;
private VideoCaptureDevice videoSource;
2.定义辅助函数
把图片转化成base64
public string ConvertImageToBase64(Image file)
{
using (MemoryStream memoryStream = new MemoryStream())
{
file.Save(memoryStream, file.RawFormat);
byte[] imageBytes = memoryStream.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
导入图片
public string ReadImg(string img)
{
return Convert.ToBase64String(File.ReadAllBytes(img));
}
连接并且打开摄像头
private void CameraConn()
{
if (comboBox1.Items.Count<=0)
{
MessageBox.Show("请插入视频设备");
return;
}
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
videoSource.DesiredFrameRate = 1;
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
}
3.人脸识别
这个代码的主要实现的功能是接受并处理图像数据,也就是将传入的图像转换为 Bitmap 格式,并进一步处理为百度人脸识别所需的 Base64 格式字符串。然后调用百度人脸检测API,并返回检测结果。最后是解析并显示人脸检测结果,提取出人脸的位置、年龄、模糊程度等信息,并在用户界面上显示。
public void Detect(object image)
{
if (image!=null && image is Bitmap)
{
try
{
Bitmap img = (Bitmap)image;
var imgByte = Bitmap2Byte(img);
Image im =img ;
string image1 = ConvertImageToBase64(im);
string imageType = "BASE64";
if (imgByte != null)
{
// 如果有可选参数
var options = new Dictionary<string, object>{
{"max_face_num", 2},
{"face_fields", "age,qualities,beauty"}
};
var result = client.Detect(image1, imageType,options);
FaceDetectInfo detect = JsonHelper.DeserializeObject<FaceDetectInfo>(result.ToString());
if (detect!=null && detect.result_num>0)
{
ageText.Text = detect.result[0].age.TryToString();
this.location = detect.result[0].location;
StringBuilder sb = new StringBuilder();
if (detect.result[0].qualities != null)
{
if (detect.result[0].qualities.blur >= 0.7)
{
sb.AppendLine("人脸过于模糊");
}
if (detect.result[0].qualities.completeness >= 0.4)
{
sb.AppendLine("人脸不完整");
}
if (detect.result[0].qualities.illumination <= 40)
{
sb.AppendLine("灯光光线质量不好");
}
if (detect.result[0].qualities.occlusion!=null)
{
if (detect.result[0].qualities.occlusion.left_cheek>=0.8)
{
sb.AppendLine("左脸颊不清晰");
}
if (detect.result[0].qualities.occlusion.left_eye >= 0.6)
{
sb.AppendLine("左眼不清晰");
}
if (detect.result[0].qualities.occlusion.mouth >= 0.7)
{
sb.AppendLine("嘴巴不清晰");
}
if (detect.result[0].qualities.occlusion.nose >= 0.7)
{
sb.AppendLine("鼻子不清晰");
}
if (detect.result[0].qualities.occlusion.right_cheek >= 0.8)
{
sb.AppendLine("右脸颊不清晰");
}
if (detect.result[0].qualities.occlusion.right_eye >= 0.6)
{
sb.AppendLine("右眼不清晰");
}
if (detect.result[0].qualities.occlusion.chin >= 0.6)
{
sb.AppendLine("下巴不清晰");
}
if (detect.result[0].pitch>=20)
{
sb.AppendLine("俯视角度太大");
}
if (detect.result[0].roll>=20)
{
sb.AppendLine("脸部应该放正");
}
if (detect.result[0].yaw>=20)
{
sb.AppendLine("脸部应该放正点");
}
}
}
if (detect.result[0].location.height<=100 || detect.result[0].location.height<=100)
{
sb.AppendLine("人脸部分过小");
}
textBox4.Text = sb.ToString();
if (textBox4.Text.IsNull())
{
textBox4.Text = "OK";
}
}
}
}
catch (Exception ex)
{
ClassLoger.Error("Form1.image", ex);
}
}
}
4.人脸对比
人脸比对技术是一种将两张人脸图像进行比对,判断它们的相似度。
首先进行用户输入检查,检查用户是否已经选择了两张用于比对的人脸图片,从文本框中获取两张人脸图片的路径。然后通过构建人脸比对请求,创建了一个 JSON 数组 faces,其中包含两张人脸图片的信息。调用百度AI的 Match 方法,传入包含人脸信息的 JSON 数组 faces,进行人脸比对。
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("请选择要对比的人脸图片");
return;
}
try
{
string path1 = textBox2.Text;
string path2 = textBox3.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);
textBox1.Text = result.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
5.其他代码
人脸注册和人脸登录等代码不便于展示
七、使用展示
(1)人脸识别
显示预测的年龄和beauty值
(2)人脸对比
两张图片的相似度score是92.345
(3)连接视频输入设备,拍照并保存
(4)输入ID和姓名,进行人脸注册
(5)人脸登陆
登录成功之后,会有语音播报登录成功,还会显示相似度
八、总结
本博客展示了如何使用C#和百度AI接口实现人脸检测、人脸比对、人脸注册、人脸登录等功能。通过检测图像中的人脸,提取并分析人脸的质量属性,如年龄、模糊度和光照等,确保图像质量符合要求,并通过比较两张人脸图像来确定相似度。

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)