Exemplo n.º 1
0
        /// <summary>
        /// 人脸识别
        /// </summary>
        /// <returns></returns>
        public JsonResult FaceDistinguish()
        {
            // 设置APPID/AK/SK
            var API_KEY    = "mxzzu1vLxca9KjnLwBCgOZs5";                //你的 Api Key
            var SECRET_KEY = "D9CkVbdziW9GrHiAZDENt8rOf0tVw9im";        //你的 Secret Key
            var client     = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);

            client.Timeout = 60000;      // 修改超时时间

            var    imageType = "BASE64"; //BASE64   URL
            string imgData64 = Request["imgData64"];

            imgData64 = imgData64.Substring(imgData64.IndexOf(",") + 1);       //将‘,’以前的多余字符串删除

            ResultInfo result = new ResultInfo();

            try
            {
                var groupId = "TestGroupA";
                var userId  = "TestUserA";


                var result323 = client.Detect(imgData64, imageType);


                //活体检测阈值是多少
                //0.05 活体误拒率:万分之一;拒绝率:63.9%
                //0.3 活体误拒率:千分之一;拒绝率:90.3%
                //0.9 活体误拒率:百分之一;拒绝率:97.6%
                //1误拒率: 把真人识别为假人的概率. 阈值越高,安全性越高, 要求也就越高, 对应的误识率就越高
                //2、通过率=1-误拒率
                //所以你thresholds参数返回 和 face_liveness 比较大于推荐值就是活体

                ////活体判断
                var faces = new JArray
                {
                    new JObject
                    {
                        { "image", imgData64 },
                        { "image_type", "BASE64" }
                    }
                };
                var Living     = client.Faceverify(faces); //活体检测交互返回
                var LivingJson = Newtonsoft.Json.JsonConvert.SerializeObject(Living);
                var LivingObj  = Newtonsoft.Json.JsonConvert.DeserializeObject(LivingJson) as JObject;
                if (LivingObj["error_code"].ToString() == "0" && LivingObj["error_msg"].ToString() == "SUCCESS")
                {
                    var    Living_result = Newtonsoft.Json.JsonConvert.DeserializeObject(LivingObj["result"].ToString()) as JObject;
                    var    Living_list   = Living_result["thresholds"];
                    double face_liveness = Convert.ToDouble(Living_result["face_liveness"]);
                    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(frr_1eObj["frr_1e-4"]);
                    if (face_liveness < frr_1e4)
                    {
                        result.info = "识别失败:这是相片之类的非活体!";
                        return(Json(result, JsonRequestBehavior.AllowGet));
                    }
                }

                //首先查询是否存在人脸
                var result2 = client.Search(imgData64, imageType, groupId);
                var strJson = Newtonsoft.Json.JsonConvert.SerializeObject(result2);
                var o2      = Newtonsoft.Json.JsonConvert.DeserializeObject(strJson) as JObject;


                //判断是否存在当前人脸,相识度是否大于80
                if (o2["error_code"].ToString() == "0" && o2["error_msg"].ToString() == "SUCCESS")
                {
                    var result_list = Newtonsoft.Json.JsonConvert.DeserializeObject(o2["result"].ToString()) as JObject;
                    var user_list   = result_list["user_list"];
                    var Obj         = JArray.Parse(user_list.ToString());
                    foreach (var item in Obj)
                    {
                        //80分以上可以判断为同一人,此分值对应万分之一误识率
                        var score = Convert.ToInt32(item["score"]);
                        if (score > 80)
                        {
                            result.info      = result2.ToString();
                            result.res       = true;
                            result.startcode = 221;
                            return(Json(result, JsonRequestBehavior.AllowGet));
                        }
                    }
                }
                else
                {
                    result.info = strJson.ToString();
                    result.res  = false;
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception e)
            {
                result.info = e.Message;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }