软件编程
位置:首页>> 软件编程>> C#编程>> 百度人脸识别之人脸识别FaceIdentify(签到考勤)

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

作者:杨小哥  发布时间:2022-08-24 18:25:03 

标签:百度人脸识别,人脸识别,FaceIdentify

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

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

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

百度人脸识别之人脸识别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 = "失败,请重新识别!";
       }
     }
   }
 }
}

来源:https://blog.csdn.net/qq_38962400/article/details/79642562

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com