百度人脸识别之人脸登录修改BUG篇,unity

一不注意就是小bug,上一篇的写法会报空指针的错会:修改了一下,也测试了,错误不存在了.也可检测到返回的错误代码的编号了,

这也是自己慢慢整出来的,希望帮到有需要的,主要是网上没有完整的案例,自己摸索.

using Baidu.Aip.Face;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class FaceLogin : MonoBehaviour {
    private Face client;//调用百度API接口
    private byte[] image;//注册时用到的图片数据
    private Dictionary<string, object> options = new Dictionary<string, object>();//多参数
    private JObject result;
    private string groupid;//用户所在组,部门
    private double scores = 0.01f;//认证返回的分数(百度0-100,指数是80分可以通过认证,可以自己设置)
    private string uid;//用户的id
    public Text uidInput;
    public Text DebugText;//提示是否通过认证
    public Text DebugText1;
    public Text DebugText2;
    public bool isSuccess = false;
    private int error_code = 0;//百度API返回的错误代码编号
    private string error_msg;//错误描述信息,帮助理解和解决发生的错误。
    

    void Awake()
    {
        client = new Face(AccessToken.client_id, AccessToken.client_secret);//通过APIKey和Secret Key取得接口
        AccessToken.ClientCallback();
    }
    public void Login()
    {
        Invoke("FaceVerify", 5.0f);
    }

    public void Start()
    {
        DebugText.text = "";
        DebugText1.text = "";
        DebugText2.text = "";
    }
    private void Update()
    {
        if (string.IsNullOrEmpty(uidInput.text))
        {
            isSuccess = true;
        }
        if (!string.IsNullOrEmpty(uidInput.text))
        {
            isSuccess = false;
            DebugText1.text = "";
        }
    }
    //人脸认证登录,人脸认证需要指定具体的用户id即可
    void FaceVerify()
    {
        if (isSuccess == false)
        {
            uid = uidInput.text;
            groupid = "test1,group1,group2,group3,U3D1,U3D";//这是我所有的用户组
            string path = Application.dataPath + "/ScreenShot/" + WebCamera.ScreenShotTexture2D + ".jpg";
            image = File.ReadAllBytes(path);
            options = new Dictionary<string, object>()
            {
               {"top_num",1 },//最大认证数
               {"ext_fields","faceliveness" }//活体检测
            };
          
           
        }
        try//避免出现网络异常导致错误
        {
            result = client.Verify(uid, groupid, image, options);
            Debug.Log(result);
            error_code = int.Parse(result["error_code"].ToString());//先把json数据转成字符串,再转成int类型

            error_msg = result["error_msg"].ToString();//把返回的json错误信息转成字符串

            switch (error_code)
            {
                case 216611:
                    DebugText.text = "user not exist	用户id不存在,请确认该用户是否注册或注册已经生效(需要已经注册超过5s)";
                    break;
                case 216401:
                    DebugText.text = "internal error	内部错误";
                    break;
                case 216402:
                    DebugText.text = "face not found 未找到人脸,请检查图片是否含有人脸";
                    break;
                case 216500:
                    DebugText.text = "unknown error	未知错误";
                    break;
                case 216615:
                    DebugText.text = "fail to process images	服务处理该图片失败,发生后重试即可";
                    break;
                case 216618:
                    DebugText.text = "no user in group	组内用户为空,确认该group是否存在或已经生效(需要已经注册超过5s)";
                    break;
                default:
                    DebugText.text = error_msg;
                    break;

            }
        }
        catch
        {
            
            if (isSuccess == true)
            {
                DebugText1.text = "用户id不能为空,请重新填写!";
            }
         //主要修改了这个地方的写法,上一篇的写法有误,存在逻辑上的错误,通过与失败的验证只能取一个.
                if (isSuccess == false)
                {
                   if (error_code != 216611 || error_code != 216401 || error_code != 216402
                    || error_code != 216500 || error_code != 216615 || error_code != 216618)
                   {
                    scores = double.Parse(result["result"][0].ToString());
                    if (scores > 80.0f)
                    {
                        DebugText.text = "通过认证!";
                        //下个场景
                    }
                    else
                    {
                        DebugText.text = "认证失败!";
                        DebugText2.text = "分值为:" + scores + " 低于认证值 非法入侵!";
                    }
                }
            }


        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_38962400/article/details/79656269
今日推荐