首先访问百度AI网站:https://cloud.baidu.com/,按照下图的指示点开到应用管理的页面。
穿件完成之后到管理中可以查看到对应的
添加工具类:
using System; using System.IO; namespace FaceCompare_Console { class FileUtils { /// <summary> /// 返回路径所指文件的base64编码 /// </summary> /// <param name="path"></param> /// <returns></returns> public static string getFileBase64(string path) { if (!File.Exists(path)) return ""; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); return Convert.ToBase64String(buffer); } } }
添加Token类:(注意:获取的Token30天才会更新一次,因此最好不要每次都获取,而是应该获取一次存储在本地,30天后再更新)
using System; using System.Collections.Generic; using System.Net.Http; namespace FaceCompare_Console { public static class AccessToken { // 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存 // 返回token示例 public static String TOKEN = "24.4c28a8feedb8a2c71fa552522284641f.2592000.1546578079.282335-15054444"; // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务 private static String clientId = "*********************"; // 百度云中开通对应服务应用的 Secret Key private static String clientSecret = "**********************"; /// <summary> /// 获取Token /// </summary> /// <returns></returns> public static String getAccessToken() { String authHost = "https://aip.baidubce.com/oauth/2.0/token"; HttpClient client = new HttpClient(); List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>(); paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); paraList.Add(new KeyValuePair<string, string>("client_id", clientId)); paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret)); HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result; String result = response.Content.ReadAsStringAsync().Result; LitJson.JsonData jd = LitJson.JsonMapper.ToObject(result); return jd["access_token"].ToString(); } } }
接下来是面部比对的代码:
using System; using System.IO; using System.Net; using System.Text; namespace FaceCompare_Console { class FaceMatch { /// <summary> /// 人脸比对 /// </summary> /// <param name="token">token</param> /// <param name="img1">图像1文件路径</param> /// <param name="img2">图像2文件路径</param> /// <returns>json格式</returns> public static string match(string token, string img1, string img2) { string host = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.KeepAlive = true; String str = "[{"image":"" + FileUtils.getFileBase64(img1) + "","image_type":"BASE64","face_type":"LIVE","quality_control":"LOW","liveness_control":"NONE"},{"image":"" + FileUtils.getFileBase64(img2) + "","image_type":"BASE64","face_type":"LIVE","quality_control":"LOW","liveness_control":"NONE"}]"; byte[] buffer = encoding.GetBytes(str); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string result = reader.ReadToEnd(); Console.WriteLine("人脸对比:"); Console.WriteLine(result); return result; } } }
最后是主程序中调用比对程序进行比对:
using System; namespace FaceCompare_Console { class Program { static void Main(string[] args) { AccessToken.getAccessToken(); FaceMatch.match(AccessToken.TOKEN, @"D:Compareweb.jpg", @"D:Compareweb1.jpg"); Console.ReadKey(); } } }
输出的结果如下:
score 是比对的结果分值,满分100,根据比对的要求判断分值多少属于比对程序即可。
更加详细的参数说明可以访问官网查看:https://ai.baidu.com/docs#/Face-Match-V3/top
补充:代码中用到Http.Net这些类可以去Nuget管理中在线查找并下载:
对返回结果的json格式需要解析的话也可以在Nuget中搜索Newtonsoft或者Litjson进行解析!