百度人脸识别之人脸注册AddUser

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

public class FaceRegister : MonoBehaviour {

    public Text userInput;//用户账号输入
    public Text passwordInput;//密码输入
    public Text phoneInput;//电话信息
    public Text RegisterCallbackText;
    public Text RegisterCallbackText1;
    public Text RegisterCallbackText2;

    private Face client;//调用百度API接口
    private byte[] image;//注册时用到的图片数据
    private Dictionary<string, object> options = new Dictionary<string, object>();//多参数
    private JObject result;
    private string uid;//用户id,这是存到人脸库中的
    private string userInfo;//用户信息
    private string groupid;//用户所在组,部门
    private int error_code = 0;//百度API返回的错误代码编号
    private string error_msg;//错误描述信息,帮助理解和解决发生的错误。


   public bool isSuccess = false;
 /*   //错误代码编号
4	Open api request limit reached	集群超限额
14	IAM Certification failed	IAM鉴权失败,建议用户参照文档自查生成sign的方式是否正确,或换用控制台中ak sk的方式调用
17	Open api daily request limit reached	每天流量超限额
18	Open api qps request limit reached	QPS超限额
19	Open api total request limit reached	请求总量超限额
100	Invalid parameter	无效参数
110	Access token invalid or no longer valid	Access Token失效
111	Access token expired	Access token过期
216015	module closed	模块关闭
216100	invalid param	参数异常
216101	not enough param	缺少必须的参数
216102	service not support	请求了不支持的服务,请检查调用的url
216103	param too long	请求超长,一般为一次传入图片个数超过系统限制
216110	appid not exist	appid不存在,请重新检查后台应用列表中的应用信息
216111	invalid userid	userid信息非法,请检查对应的参数
216200	empty image	图片为空或者base64解码错误
216201	image format error	图片格式错误
216202	image size error	图片大小错误
216300	db error	数据库异常,少量发生时重试即可
216400	backend error	后端识别服务异常,可以根据具体msg查看错误原因
216401	internal error	内部错误
216402	face not found	未找到人脸,请检查图片是否含有人脸
216500	unknown error	未知错误
216611	user not exist	用户不存在,请确认该用户是否注册或注册已经生效(需要已经注册超过5s)
216613	fail to delete user record	删除用户图片记录失败,重试即可
216614	not enough images	两两比对中图片数少于2张,无法比较
216615	fail to process images	服务处理该图片失败,发生后重试即可
216616	image existed	图片已存在
216617	fail to add user	新增用户图片失败
216618	no user in group	组内用户为空,确认该group是否存在或已经生效(需要已经注册超过5s)
216631	request add user overlimit	本次请求添加的用户数量超限
 */
    void Awake () {
        
        client = new Face(AccessToken.client_id, AccessToken.client_secret);//通过APIKey和Secret Key取得接口
        AccessToken.ClientCallback();//下面我贴上了,获取授权的证书
    }
  
    private void Start()
    {

        RegisterCallbackText.text = "";
        RegisterCallbackText1.text = "";
        RegisterCallbackText2.text = "";
    }
    /*人脸注册 返回示例

// 注册成功
{
    "log_id": 73473737,
}
// 注册发生错误
{
  "error_code": 216616,
  "log_id": 674786177,
  "error_msg": "image exist"
}*/
    public void OnClickAddUser()
    {
        Invoke("AddUser", 5.0f);
    }

    //实时监测用户输入的信息
    private void Update()
    {
        //如果用户名和密码和手机号都为空
        if (string.IsNullOrEmpty(userInput.text)&& string.IsNullOrEmpty(passwordInput.text)&& string.IsNullOrEmpty(phoneInput.text))
        {
             isSuccess = true;//bool,为了错误信息输出
        }
        //都不为空
        if(!string.IsNullOrEmpty(userInput.text) && !string.IsNullOrEmpty(passwordInput.text) && !string.IsNullOrEmpty(phoneInput.text))
        {
            isSuccess = false;//bool
            userInfo = userInput.text.ToString() + passwordInput.text.ToString() + phoneInput.text.ToString();//用户信息就是这三个的信息
            RegisterCallbackText1.text = "";//清楚提示信息
        }
    }
    //用户人脸注册
    void AddUser()
    {
        if (isSuccess==false)
        {
            uid = "007";
            groupid = "test1";
            //获取图片存放的路径
            string path = Application.dataPath + "/ScreenShot/" + WebCamera.ScreenShotTexture2D + ".jpg";
            image = File.ReadAllBytes(path);
            options = new Dictionary<string, object>()
            {
              {"action_type", "replace" }//replace替换更新用户的脸部照片,append新增照片到用户的uid下
            };
            result = client.UserAdd(uid, userInfo, groupid, image, options);
          
        }
           
            try//避免出现网络异常导致错误
            {
                error_code = int.Parse(result["error_code"].ToString());//先把json数据转成字符串,再转成int类型
                error_msg = result["error_msg"].ToString();//把返回的json错误信息转成字符串
                switch (error_code)
                {
                    case 216100:
                        RegisterCallbackText.text = "invalid param	参数异常,请重新填写注册信息";
                        break;
                    case 216101:
                        RegisterCallbackText1.text = "not enough param	缺少必须的注册信息,请重新填写注册信息";
                        break;
                    case 216401:
                        RegisterCallbackText.text = "internal error	内部错误";
                        break;
                    case 216402:
                        RegisterCallbackText1.text = "face not found 未找到人脸,请检查图片是否含有人脸";
                        break;
                    case 216500:
                        RegisterCallbackText.text = "unknown error	未知错误";
                        break;
                    case 216615:
                        RegisterCallbackText1.text = "fail to process images	服务处理该图片失败,发生后重试即可";
                        break;
                    default:
                        RegisterCallbackText.text = error_msg;
                        break;
                }
            }
           catch (System.Exception e)
           {
            if (isSuccess == true)
            {
                error_code = 216101;
                RegisterCallbackText1.text = "缺少必须的注册信息,请重新填写注册信息";
            }
            else
            {
                RegisterCallbackText.text = e.ToString();
                RegisterCallbackText.text = "面部注册成功,请点击登录!";
                RegisterCallbackText2.text = result.ToString();//显示返回的数据信息
            }
        }
        //finally//还不能放到这里面,这里面即便有错误也会注册成功
        //{
        //    RegisterCallbackText.text = "面部注册成功,请点击登录!";
        //}
    }   
}

AccessToken.ClientCallback();
 //通过服务器回调,获取证书
    public static void ClientCallback()
    {
        ServicePointManager.ServerCertificateValidationCallback +=
            delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
            System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return true;//总是允许
            };
    }

猜你喜欢

转载自blog.csdn.net/qq_38962400/article/details/79626866