百度AI—人臉在線比對
阿新 • • 發佈:2018-12-12
tle ima private access acc cloud 結果 public als
首先訪問百度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 classAccessToken { // 調用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:\Compare\web.jpg", @"D:\Compare\web1.jpg"); Console.ReadKey(); } } }
輸出的結果如下:
score 是比對的結果分值,滿分100,根據比對的要求判斷分值多少屬於比對程序即可。
更加詳細的參數說明可以訪問官網查看:https://ai.baidu.com/docs#/Face-Match-V3/top
補充:代碼中用到Http.Net這些類可以去Nuget管理中在線查找並下載:
對返回結果的json格式需要解析的話也可以在Nuget中搜索Newtonsoft或者Litjson進行解析!
百度AI—人臉在線比對