Exemple #1
0
        /// <summary>
        /// 人脸匹配
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public JObject NetFaceMatch(Image image)
        {
            try
            {
                var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY)
                {
                    Timeout = 60000  // 修改超时时间
                };

                //图片转为Base64
                string base64 = ImageToBase64(image);

                string        imageType = "BASE64";
                List <string> groupList = new List <string>();

                groupList.Add("UsualUser");
                groupList.Add("StarUser");

                string  group  = string.Join(",", groupList.ToArray());
                JObject result = client.Search(base64, imageType, group);
                return(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接人脸识别API出错:" + ex);
                return(new JObject());
            }
        }
        protected static void Search()
        {
            var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);

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

            using (var s = new FileStream("C:/Users/admin/Desktop/微信图片_20190107152711.jpg", FileMode.Open))
            {
                image = ComFunc.StreamToBase64String(s);
            }


            var imageType = "BASE64";

            var groupIdList = "F001";

            // 调用人脸搜索,可能会抛出网络等异常,请使用try/catch捕获
            var result = client.Search(image, imageType, groupIdList);

            Console.WriteLine(result);
            // 如果有可选参数
            //        var options = new Dictionary<string, object>{
            //    {"quality_control", "NORMAL"},
            //    {"liveness_control", "LOW"},
            //    {"user_id", "233451"},
            //    {"max_user_num", 3}
            //};
            //        // 带参数调用人脸搜索
            //        result = client.Search(image, imageType, groupIdList, options);
            //        Console.WriteLine(result);
        }
        /// <summary>
        /// 刷脸识别
        /// </summary>
        /// <param name="reqData"></param>
        /// <returns></returns>
        public XAIResFind Find(XAIReqFind reqData)
        {
            LogModule.Info("XAI->BIDU:Find--->入参:" + reqData.ToJson());
            string resJson;

            try
            {
                resJson = client.Search(reqData.Image.Split(new string[] { ";base64," }, StringSplitOptions.RemoveEmptyEntries)[1], "BASE64", reqData.GroupId).ToJson();
            }
            catch (Exception ex)
            {
                throw new XAIException(7100, "请求人脸识别服务异常,请重试!" + ex.Message);
            }
            LogModule.Info("XAI->BIDU:Find--->出参:" + resJson);
            var res = resJson.ToEntity <BIDUResponse>();

            if (res.error_code != 0)
            {
                throw new XAIException(7100, typeof(BIDUErrorCodeEnum).GetEnumName(res.error_code.ToInt()));
            }
            res.result.user_list = res.result.user_list.Where(w => w.score > 80).OrderByDescending(o => o.score).ToList();
            if (res.result.user_list.Count() == 0)
            {
                throw new XAIException(7101, "未识别出有效用户");
            }
            return(new XAIResFind()
            {
                UserId = res.result.user_list.OrderByDescending(w => w.score).First().user_id
            });
        }
Exemple #4
0
        public static BaseResponse <FaceSearchResult> FaceSearch(byte[] imageByte, string[] groupIds, SearchFaceOption option = null)
        {
            if (imageByte.Length > 1024 * 1024 * 10)
            {
                throw new Exception("图片大小必须小于10Mb");
            }
            var imageBase64 = Convert.ToBase64String(imageByte);

            return(Execute <FaceSearchResult>(client.Search(imageBase64, ImageType, string.Join(",", groupIds), option?.Options)));
        }
Exemple #5
0
        /// <summary>
        /// 查找用户
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public JObject SereachUserByImage(byte[] bytes)
        {
            var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);

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

            var image = Convert.ToBase64String(bytes);

            var imageType = "BASE64";

            var options = new Dictionary <string, object> {
                { "face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark72,landmark150,race,quality,eye_status,emotion,face_type" },
                { "max_user_num", 3 },
            };

            var ret = client.Search(image, imageType, groupId, options);

            return(ret);
        }
Exemple #6
0
        public String  FaceServices(dynamic obj)

        {
            try
            {
                string id      = obj.id.ToString();
                string place   = obj.place.ToString();
                string image1  = obj.image1.ToString();
                string groupid = obj.groupid.ToString();

                m_logger.Info("FaceServices调用开始:" + id + "," + place);
                var client = new Baidu.Aip.Face.Face(ApiKey, SecretKey);
                client.Timeout = timeout;
                var result = client.Search(image1, "BASE64", groupid);
                m_logger.Info(id + "," + place + "FaceServices调用结束:" + result.ToString());
                return(result.ToString());
            }
            catch (Exception ex)
            {
                m_logger.Error("FaceServices" + ex);
                return("");
            }
        }
        /// <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));
        }
        /// <summary>
        /// 人脸注册
        /// </summary>
        /// <returns></returns>
        public JsonResult FaceRegistration()
        {
            // 设置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 result2 = client.Search(imgData64, imageType, userId);  //会出现222207(未找到用户)这个错误
                var strJson = Newtonsoft.Json.JsonConvert.SerializeObject(result2);
                var o2      = Newtonsoft.Json.JsonConvert.DeserializeObject(strJson) as JObject;


                //判断是否存在当前人脸,相识度是否大于88
                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)
                    {
                        //88分以上可以判断为同一人,此分值对应万分之一误识率
                        var score = Convert.ToInt32(item["score"]);
                        if (score > 88)
                        {
                            result.info      = result2.ToString();
                            result.res       = true;
                            result.startcode = 221;
                            return(Json(result, JsonRequestBehavior.AllowGet));
                        }
                    }
                }

                var guid = Guid.NewGuid();
                // 调用人脸注册,可能会抛出网络等异常,请使用try/catch捕获
                // 如果有可选参数
                var options = new Dictionary <string, object> {
                    { "user_info", guid }
                };
                // 带参数调用人脸注册
                var resultData = client.UserAdd(imgData64, imageType, groupId, userId, options);
                result.info  = resultData.ToString();
                result.res   = true;
                result.other = guid.ToString();
            }
            catch (Exception e)
            {
                result.info = e.Message;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }