人脸识别之人脸对比 【C#】
程序员文章站
2022-07-12 20:40:19
...
第一步:登录百度云
网址:https://login.bce.baidu.com
没有账号就自己注册一个
第二步:创建应用,获取API Key 和 Secret Key
第三步:获取Access Token
代码:
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace com.baidu.ai
{
public static class AccessToken
{
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
public static String TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567";
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
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;
//Console.WriteLine(result);
return result;
}
}
}
注意的点:System.Net.Http 这个包的引入必须是.Net Framework 4.5及以上才有!
第四步:发送请求至百度云人脸对比接口
官方文档
代码:
using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace com.baidu.ai
{
public class FaceMatch
{
// 人脸对比
public static string match()
{
string token = AccessToken.getAccessToken();//调用鉴权接口获取的token
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";
string basePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//D:\colde\workspace\visualstudio2015\project\com.baidu.ai\com.baidu.ai\bin\Debug\zp.bmp
string image_IdCard = Convert.ToBase64String(System.IO.File.ReadAllBytes(basePath + "zp.bmp"));
string image_Live = Convert.ToBase64String(System.IO.File.ReadAllBytes(basePath + "liveIMG.jpg"));
request.KeepAlive = true;
String str = "[{\"image\":\""+ image_IdCard + "\",\"image_type\":\"BASE64\",\"face_type\":\"IDCARD\",\"quality_control\":\"LOW\",\"liveness_control\":\"HIGH\"},{\"image\":\"" + image_Live + "\",\"image_type\":\"BASE64\",\"face_type\":\"LIVE\",\"quality_control\":\"LOW\",\"liveness_control\":\"HIGH\"}]";
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);
JObject returnJson = (JObject)JsonConvert.DeserializeObject(result);
if ("0".Equals(returnJson["error_code"].ToString())&& "SUCCESS".Equals(returnJson["error_msg"].ToString()))
{
JObject resultJson = (JObject)JsonConvert.DeserializeObject(returnJson["result"].ToString());
if (Convert.ToDecimal(resultJson["score"].ToString()) > 90)
{
result = "为同一人!";
}
else
{
result = "不是同一人,请视情况报警!";
}
}else
{
Console.WriteLine(result);
}
return result;
}
}
}
第五步:结果展示
返回值:
{"error_code":0,"error_msg":"SUCCESS","log_id":1545101351575,"timestamp":1531378774,"cached":0,
"result":{"score":18.9715271,
"face_list":[{"face_token":"672bfbfb06e20c881e8b75df44bec5e7"},{"face_token":"c57fab58fdb600f03d6b6b1e5863532e"}]}
}
下一篇: VS Code