百度人脸识别之人脸识别FaceIdentify(签到考勤)

2022-10-16,,,,

本文实例为大家分享了百度人脸识别之人脸识别faceidentify,供大家参考,具体内容如下

using system.collections.generic;
using unityengine;
using baidu.aip.face;
using newtonsoft.json.linq;
using unityengine.ui;
using system.io;
 
//识别,(用于签到考勤)
public class faceidentify : monobehaviour {
 
  private face client;//百度api接口
  private jobject result;//识别数据的返回结果
  private byte[] image;//图片字节数
  private dictionary<string, object> options = new dictionary<string, object>();//多参数
  private double scores;//返回的分数,用来判定识别是否达标
  private string group_id;//用户所在的用户组,或者说部门
  private int error_code;//返回得错误代码
  private string error_msg;//返回得错误提示信息
  public text debugtext;
  public text debugtext1;
  public text debugtext2;
  private string result_group_id;//result返回的用户组数据
  private string result_uid;//result返回的用户id数据
  private string result_user_info;//result 返回的用户信息数据
  public bool issuccess = false;
 
  private void awake()
  {
    client = new face(accesstoken.client_id, accesstoken.client_secret);
    accesstoken.clientcallback();//获取证书
  }
 
  private void start()
  {
    debugtext.text = "";
    debugtext1.text = "";
    debugtext2.text = "";
  }
  public void faceidentify()
  {
    invoke("identify", 5.0f);
  }
 
  void identify()
  {
    group_id = "u3d1";
    string path = application.datapath + "/screenshot/" + webcamera.screenshottexture2d + ".jpg";
    image = file.readallbytes(path);
    options = new dictionary<string, object>()
    {
      {"ext_fileds","faceliveness" },
      {"user_top_num",1 }//最大返回5个识别数
    };
    try//避免出现网络异常导致错误
    {
      result = client.identify(group_id, 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 216100:
          debugtext.text = "invalid param 参数异常,请重新填写注册信息";
          break;
        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;
      }
      if (error_code != 216100 || error_code != 216101|| error_code != 216401
        || error_code != 216402 || error_code != 216500 || error_code != 216615 || error_code != 216618)
      {
        debugtext1.text = result.tostring();//显示返回的数据信息
       
 
      }
    }
    catch
    {
      if (error_code != 216100 || error_code != 216101 || error_code != 216401
         || error_code != 216402 || error_code != 216500 || error_code != 216615 || error_code != 216618)
      {
        jtoken res = result["result"];
        scores = double.parse(res[0]["scores"][0].tostring());
        if (scores > 80.0f)
        {
          result_uid = res[0]["uid"].tostring();
          result_group_id = res[0]["group_id"].tostring();
          result_user_info = res[0]["user_info"].tostring();
 
          debugtext1.text = "识别成功,今日已签到!";
          debugtext2.text = result_uid+"\n"+ result_group_id+"\n" + result_user_info;
          debug.log(result_uid.tostring() + result_group_id.tostring() + result_user_info.tostring());
        }
        else
        {
          debugtext1.text = "失败,请重新识别!";
        }
      }
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《百度人脸识别之人脸识别FaceIdentify(签到考勤).doc》

下载本文的Word格式文档,以方便收藏与打印。