通过两张照片进行比对得出相似度,来判断两张照片是否为同一人。此功能可以用在多种场景,例如学校的门禁系统。
private string APP_ID = "33711276";
private string API_KEY = "1nhS4AcrLFffxqDKB73bb1U7";
private string SECRET_KEY = "rtnRFv5RcE0dMxBWqmL6PX3LYwVAdETW";
private Face client = null;
本段代码定义了三个字符串变量 APP_ID
、API_KEY
和 SECRET_KEY
,以及一个 Face
类型的私有变量 client
,它被初始化为 null
。本段代码的功能为初始化一个与某种人脸识别服务交互的客户端。
private bool IsStart = false;
该行代码的作用是判断是否可以检测人脸
public Form1()
{
InitializeComponent();
client = new Face(API_KEY, SECRET_KEY);
}
本段代码初始化了窗体组件 InitializeComponent()
,并随后创建了一个 Face
类的实例,将 API_KEY
和 SECRET_KEY
作为参数传递给该实例的构造函数。
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "F:\\";
dialog.Filter = "所有文件|*.*";
dialog.RestoreDirectory = true;
dialog.FilterIndex = 2;
if (dialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(textBox2.Text))
{
textBox2.Text = dialog.FileName;
}
else
{
textBox3.Text = dialog.FileName;
}
}
}
public string ReadImg(string img)
{
return Convert.ToBase64String(File.ReadAllBytes(img));
}
该段代码实现了将读取两个图片文件的内容转换为Base64编码的字符串,
private void button1_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)
{ }
}
该段代码的作用是当text.box2或text.box3中没有图片时,输出“请选择要对比的人脸图片”。然后构建一个JSON数组,该数组包含两个对象,每个对象代表一个要用于人脸比对的图片。接着调用一个名为 client.Match
的方法来执行人脸比对,并将结果输出到 textBox1
。
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.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
namespace 作业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;
/// <summary>
/// 是否可以检测人脸
/// </summary>
private bool IsStart = false;
public Form1()
{
InitializeComponent();
client = new Face(API_KEY, SECRET_KEY);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "F:\\";
dialog.Filter = "所有文件|*.*";
dialog.RestoreDirectory = true;
dialog.FilterIndex = 2;
if (dialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(textBox2.Text))
{
textBox2.Text = dialog.FileName;
}
else
{
textBox3.Text = dialog.FileName;
}
}
}
public string ReadImg(string img)
{
return Convert.ToBase64String(File.ReadAllBytes(img));
}
private void button1_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)
{ }
}
}
}
通过本次实验我学习到了人脸比对的相关代码知识,通过此代码我们可以实现人脸对比功能,可运用于多个场景,例如校园录入人脸信息,通过人脸比对实现刷脸进门进门功能。
因篇幅问题不能全部显示,请点此查看更多更全内容