NetCore百度人脸识别HTTP SDk实战:基于C# ASP.NETCore Net 6
Posted 厦门德仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NetCore百度人脸识别HTTP SDk实战:基于C# ASP.NETCore Net 6相关的知识,希望对你有一定的参考价值。
预计这是第一篇介绍在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("活体检验通过");
转自:https://blog.csdn.net/weixin_41372626/article/details/108370961
来演示一下:
1.注册百度账号,导入人脸库,设置组和用户ID(与本地数据库ID一致)
2.免费领取资源(才允许调用百度API)
3.nuget 安装百度.AI
4.识别后查询本地实体数据并返回。
本地localhost运行正常,发布到服务器,浏览器版本较高。存在打开摄像头方法权限过高。
TypeError: Cannot read property ‘getUserMedia’ of undefined
步骤:
1、在浏览器的地址栏里输入 chrome://flags/#unsafely-treat-insecure-origin-as-secure 并回车
然后作如下修改
输入框中填写需要开启的域名,也就是你报错的地址,譬如 http://example.com",多个以逗号分隔。
2、修改完后,在右下角点击重启
以上是关于NetCore百度人脸识别HTTP SDk实战:基于C# ASP.NETCore Net 6的主要内容,如果未能解决你的问题,请参考以下文章