一、设置
登录百度云控制台,添加应用-添加人脸识别,查找,对比等。
记住API Key和Secret Key
二、创建Demo程序
1、使用Nuget安装 Baidu.AI 和 Newtonsoft.Json
2、直接下载SDK https://ai.baidu.com/file/7D961BC013AB4AA7905001FAF89EE327
3、案例代码
private void simpleButton1_Click(object sender, EventArgs e) { //Devepxress 控件 Bitmap bitMap = cameraControl1.TakeSnapshot(); // 设置APPID/AK/SK var API_KEY = "你的ApiKey"; var SECRET_KEY = "你的Secret_Key"; var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 client.AppId = APP_ID; string guidName = Guid.NewGuid() + ".Jpeg"; bitMap.Save(guidName, ImageFormat.Jpeg); var image = ReadImg(guidName); var imageType = "BASE64"; var groupIdList = "bim"; // 如果有可选参数 var options = new Dictionary{ {"max_face_num", 1}, {"match_threshold", 90}, {"quality_control", "NORMAL"}, {"liveness_control", "LOW"}, {"max_user_num", 1}}; // 调用人脸搜索 M:N 识别,可能会抛出网络等异常,请使用try/catch捕获 // 带参数调用人脸搜索 M:N 识别 try { var result = client.MultiSearch(image, imageType, groupIdList, options); //判断是否成功 if (result["error_msg"].ToString() == "SUCCESS") { var list = JsonConvert.DeserializeObject(result.ToString()); if (list.result.face_list != null) { //获取用户标识 string userId = list.result.face_list[0].user_list[0].user_id; memoEdit1.Text += $"{userId}:{DateTime.Now}"; } } else { memoEdit1.Text += $"认证失败{guidName}:{DateTime.Now}"; } } catch(Exception ex) { XtraMessageBox.Show("验证失败!"); Console.WriteLine(ex); } } public string ReadImg(string img) { return Convert.ToBase64String(File.ReadAllBytes(img)); }
4、最终界面