百度人臉識(shí)別之人臉識(shí)別FaceIdentify(簽到考勤)
本文實(shí)例為大家分享了百度人臉識(shí)別之人臉識(shí)別FaceIdentify,供大家參考,具體內(nèi)容如下



using System.Collections.Generic;
using UnityEngine;
using Baidu.Aip.Face;
using Newtonsoft.Json.Linq;
using UnityEngine.UI;
using System.IO;
//識(shí)別,(用于簽到考勤)
public class FaceIdentify : MonoBehaviour {
private Face client;//百度API接口
private JObject result;//識(shí)別數(shù)據(jù)的返回結(jié)果
private byte[] image;//圖片字節(jié)數(shù)
private Dictionary<string, object> options = new Dictionary<string, object>();//多參數(shù)
private double scores;//返回的分?jǐn)?shù),用來判定識(shí)別是否達(dá)標(biāo)
private string group_id;//用戶所在的用戶組,或者說部門
private int error_code;//返回得錯(cuò)誤代碼
private string error_msg;//返回得錯(cuò)誤提示信息
public Text DebugText;
public Text DebugText1;
public Text DebugText2;
private string result_group_id;//result返回的用戶組數(shù)據(jù)
private string result_uid;//result返回的用戶id數(shù)據(jù)
private string result_user_info;//result 返回的用戶信息數(shù)據(jù)
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個(gè)識(shí)別數(shù)
};
try//避免出現(xiàn)網(wǎng)絡(luò)異常導(dǎo)致錯(cuò)誤
{
result = client.Identify(group_id, image, options);
Debug.Log(result);
error_code = int.Parse(result["error_code"].ToString());//先把json數(shù)據(jù)轉(zhuǎn)成字符串,再轉(zhuǎn)成int類型
error_msg = result["error_msg"].ToString();//把返回的json錯(cuò)誤信息轉(zhuǎn)成字符串
switch (error_code)
{
case 216100:
DebugText.text = "invalid param 參數(shù)異常,請(qǐng)重新填寫注冊(cè)信息";
break;
case 216611:
DebugText.text = "user not exist 用戶id不存在,請(qǐng)確認(rèn)該用戶是否注冊(cè)或注冊(cè)已經(jīng)生效(需要已經(jīng)注冊(cè)超過5s)";
break;
case 216401:
DebugText.text = "internal error 內(nèi)部錯(cuò)誤";
break;
case 216402:
DebugText.text = "face not found 未找到人臉,請(qǐng)檢查圖片是否含有人臉";
break;
case 216500:
DebugText.text = "unknown error 未知錯(cuò)誤";
break;
case 216615:
DebugText.text = "fail to process images 服務(wù)處理該圖片失敗,發(fā)生后重試即可";
break;
case 216618:
DebugText.text = "no user in group 組內(nèi)用戶為空,確認(rèn)該group是否存在或已經(jīng)生效(需要已經(jīng)注冊(cè)超過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();//顯示返回的數(shù)據(jù)信息
}
}
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 = "識(shí)別成功,今日已簽到!";
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 = "失敗,請(qǐng)重新識(shí)別!";
}
}
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法
這篇文章主要介紹了C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
Unity 從UI中拖拽對(duì)象放置并拖動(dòng)效果 附demo
最近新接了個(gè)需求,要求模擬場(chǎng)景并生成3D對(duì)象,對(duì)象可以跟隨鼠標(biāo)移動(dòng)效果,今天小編把我實(shí)現(xiàn)的demo分享到腳本之家平臺(tái),對(duì)Unity UI拖拽相關(guān)知識(shí)感興趣的朋友跟隨小編一起學(xué)習(xí)吧2021-05-05
c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享(數(shù)字頻率計(jì)設(shè)計(jì))
c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享,大家參考使用吧2013-12-12
C#實(shí)現(xiàn)單鏈表(線性表)完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)單鏈表(線性表)的方法,結(jié)合完整實(shí)例形式分析了單鏈表的原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06
C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法
這篇文章主要介紹了C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法,需要的朋友可以參考下2014-07-07
c#求點(diǎn)到直線的投影點(diǎn)坐標(biāo)
這篇文章主要介紹了c#求直線外一點(diǎn)到該直線的投影點(diǎn),大家參考使用吧2013-12-12
C#中如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫界面
這篇文章講解了如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫界面,Winform相對(duì)于Wpf使用更簡單一些,系統(tǒng)要求更低,需要了解的朋友可以參考下2015-07-07

