百度人脸识别HTTP SDk实战:基于C# ASP.NET CORE MVC 3.1

tech2022-08-05  134

预计这是第一篇介绍在ASP.NET CORE 3.1平台下使用百度人脸识别在线sdk的文章,主要介绍人脸1:n检测/活体检验/人脸注册三大关键功能。

先看几个效果图吧

(1)人脸1:N检测,返回人脸对应用户信息

(2)活体检测:识别活体还好图片,防止人脸作弊

(3)人脸注册:检测用户是否存在

开始

在正式开始之前,需要一些准备以及预备知识,这里可以参考https://www.cnblogs.com/xiongze520/p/10387355.html以及https://www.cnblogs.com/xiongze520/p/10688545.html,讲的很详细。这里主要将后端的代码逻辑。

(1)人脸注册

在人脸注册前需要检测人脸库是否存在该人脸,已经存在了,则终止注册行为

代码如下:改方法接受前端返回的data64的图片字节串流,调用百度人脸识别接口方法进行判断,这里写了一个_faceEFService的数据库服务类,采用了EF core, var userModel = _faceEFService.GetByGuiIdAsync(userId).Result;返回数据库中userId对应的用户信息,其中userId是在注册人脸的时候,就保存到了百度云控制后台。

public IActionResult CheckIfUserHave(string imgData64FromAjax) { imgData64 = imgData64FromAjax; string ApiKey = "ApiKey "; string SecretKey = "SecretKey "; var client = new Baidu.Aip.Face.Face(ApiKey, SecretKey); client.Timeout = 10000; var imageType = "BASE64"; //BASE64 URL string requestImgData64 = imgData64FromAjax; requestImgData64 = requestImgData64.Substring(imgData64.IndexOf(",") + 1); string groupId = "FaceGroupForNetCore"; //string userId = new Guid().ToString(); var findUserJsonResult = client.Search(requestImgData64, imageType, groupId); //会出现222207(未找到用户)这个错误 if (findUserJsonResult["error_code"].ToString() == "0" && findUserJsonResult["error_msg"].ToString() == "SUCCESS") { var userList = JArray.Parse(findUserJsonResult["result"]["user_list"].ToString()); foreach (var user in userList) { var score = Convert.ToInt32(user["score"].ToString().Substring(0, 2)); if (score > 80) { Guid userId = Guid.Parse(user["user_id"].ToString()); var userModel = _faceEFService.GetByGuiIdAsync(userId).Result; if (userModel != null) { var returnModel = new FaceReconitionModel { UserName = userModel.UserName }; return Json(returnModel.UserName); } } } } if (findUserJsonResult["error_code"].ToString() == "222202") { return Json("无法识别到人脸"); } return View(); }

(2)人脸注册:这块没啥了,大家应该都懂,就不介绍了。。。

[HttpPost] public IActionResult FaceRegister( [FromForm] FaceReconitionModel faceReconitionModel) { string ApiKey = "ApiKey "; string SecretKey = "ApiKey "; var client = new Baidu.Aip.Face.Face(ApiKey, SecretKey); client.Timeout = 10000; var imageType = "BASE64"; //BASE64 URL imgData64 = imgData64.Substring(imgData64.IndexOf(",") + 1); string groupId = "FaceGroupForNetCore"; string userId = Guid.NewGuid().ToString("N"); var userInfo = Guid.NewGuid(); var options = new Dictionary<string, object>{ {"user_info", userInfo.ToString()}, {"quality_control", "NORMAL"}, {"liveness_control", "LOW"}, {"action_type", "REPLACE"} }; var addUserFace = client.UserAdd(imgData64, imageType, groupId, userId, options); var model = new FaceMdel { UserName = faceReconitionModel.UserName, Month = faceReconitionModel.Month.ToString(), Sex = faceReconitionModel.Sex, Works = faceReconitionModel.Works, Position = faceReconitionModel.Position, GuidId = Guid.Parse(userId) }; _faceEFService.AddAsync(model); // _faceEFService.SaveChangeAsnc(); return RedirectToAction("index"); }

(3)人脸活体检测以及人脸1:N检测

  这里只要是人脸活体检测以及1:N检测,根据返回的UserId查找出数据库中对应的用户信息,依然很简单,大家应该看的懂。。

public IActionResult FaceDistinguish(string faceImgData64) { FaceImgData64 = faceImgData64; string ApiKey = "ApiKey "; string SecretKey = "ApiKey "; var client = new Baidu.Aip.Face.Face(ApiKey, SecretKey); client.Timeout = 10000; // var imageType = "BASE64"; //BASE64 URL faceImgData64 = faceImgData64.Substring(faceImgData64.IndexOf(",") + 1); // string groupId = "FaceGroupForNetCore"; var faces = new JArray { new JObject { {"image", faceImgData64}, {"image_type", "BASE64"} } }; var checkLiving = client.Faceverify(faces); //"error_code": 222202, if (checkLiving["error_code"].ToString() == "0" && checkLiving["error_msg"].ToString() == "SUCCESS") { // var Living_result = Newtonsoft.Json.JsonConvert.DeserializeObject(LivingObj["result"].ToString()) as JObject; var livingList = checkLiving["result"]["thresholds"]; double faceLiveness = Convert.ToDouble(checkLiving["result"]["face_liveness"].ToString()); //var frr = Newtonsoft.Json.JsonConvert.SerializeObject(Living_list.ToString()); //var frr_1eObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Living_list.ToString()) as JObject; double frr_1e4 = Convert.ToDouble(livingList["frr_1e-4"].ToString()); if (faceLiveness < frr_1e4) { return Json("不是活体"); } } if (checkLiving["error_code"].ToString() == "222202") { return Json("无法识别到人脸"); } return Json("活体检验通过"); }

完结。。。。有啥再留言吧,估计也不会有问题,比较忙,随意记录一下吧。。

最新回复(0)