C#調(diào)用百度API實(shí)現(xiàn)活體檢測的方法
前言
活體檢測有多種情形,本文所指:從攝像頭獲取的影像中判斷是活體,還是使用了相片等靜態(tài)圖片。
場景描述
用戶個人信息中上傳了近照,當(dāng)用戶經(jīng)過攝像頭時(shí)進(jìn)行身份識別。
此時(shí),如果單純的使用攝像頭獲取的影像進(jìn)行人臉相似度比對,則舉一張合適的相片對準(zhǔn)攝像頭也是可以通過的。于是檢測攝像頭前影像是否為活體的需求就產(chǎn)生了。
解決方案
使用百度AI開放平臺,它免費(fèi)開放一定并發(fā)量的該場景活體檢測 API:
https://ai.baidu.com/tech/face/faceliveness
第一步,申請百度應(yīng)用

點(diǎn)擊“立即使用”,登錄后“創(chuàng)建應(yīng)用”,可以得到 API Key 與 Secret Key 等信息。
第二步,使用 API 進(jìn)行活體檢測
這里的場景比較簡單,攝像頭獲取的影像可以保存為圖片,則功能接口可以這樣定義:給定圖片(這里使用URL),判斷其活體影像的概率。根據(jù)百度建議,概率設(shè)置為 99.5%,即達(dá)到此值或以上認(rèn)為活體檢測通過。
(1)獲取 accessToken
accessToken 有效期為 30 天,因此,可以緩存起來使用。此為示例,時(shí)長又足夠長,所以未加刷新機(jī)制。代碼如下,其中,clientId 為百度應(yīng)用中的 API Key,clientSecret 為百度應(yīng)用中的 Secret Key。
public static class AccessToken
{
// 有效期30天,緩存獲取的 access token
public static String TOKEN = null;
// 百度云中開通對應(yīng)服務(wù)應(yīng)用的 API Key
private static String clientId = "API Key";
// 百度云中開通對應(yīng)服務(wù)應(yīng)用的 Secret Key
private static String clientSecret = "Secret Key";
public static String getAccessToken()
{
if (String.IsNullOrEmpty(TOKEN))
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
JObject jr = JObject.Parse(result);
TOKEN = jr.Value<string>("access_token");
}
return TOKEN;
}
}
(2)調(diào)用 API 取得活體概率
API 的返回結(jié)果為 JSON,其中包括了活體概率,這里,方法直接返回 API 的 JSON 結(jié)果。
public class FaceLivenessHelper
{
// 在線活體檢測
public static string FaceVerify(string imgUrl)
{
string token = AccessToken.getAccessToken();
string host = "https://aip.baidubce.com/rest/2.0/face/v3/faceverify?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// String str = "[{\"image\":\"sfasq35sadvsvqwr5q...\",\"image_type\":\"BASE64\",\"face_field\":\"age,beauty,expression\"}]";
String str = "[{\"image\":\"" + imgUrl + "\",\"image_type\":\"URL\",\"face_field\":\"age,beauty,expression\"}]";
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("在線活體檢測:");
Console.WriteLine(result);
return result;
}
}
詳細(xì) API 文檔見此:https://ai.baidu.com/docs#/Face-Liveness-V3/top
結(jié)果中:face_liveness 即表示“活體分?jǐn)?shù)值”。
(3)應(yīng)用
API 的調(diào)用結(jié)果中,error_code 為 0 時(shí)表示執(zhí)行成功,此時(shí),會有 result 屬性表示計(jì)算的相關(guān)值,從中取出 face_liveness 即可,其值為 0 ~ 1之間。
string imgUrl = "------";
string result = FaceLivenessHelper.FaceVerify(imgUrl);
JObject jresult = JObject.Parse(result);
JObject lvresult = jresult.Value<JObject>("result");
// error_code 為 0 時(shí)表示執(zhí)行成功,其它表示失敗
if (jresult.Value<int>("error_code") == 0)
{
double face_liveness = lvresult.Value<double>("face_liveness");
// 活體率達(dá)到要求
if (face_liveness >= 0.995)
{
// 通過檢測
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
- C# 調(diào)用WebApi的實(shí)現(xiàn)
- c# 調(diào)用Win32Api關(guān)閉當(dāng)前應(yīng)用的方法
- c# 通過WinAPI播放PCM聲音
- c# webapi 配置swagger的方法
- C#調(diào)用Win32的API函數(shù)--User32.dll
- C#調(diào)用新浪微博API實(shí)例代碼
- C# WebApi Get請求方式傳遞實(shí)體參數(shù)的方法示例
- C#凈化版WebApi框架的實(shí)現(xiàn)
- C# WebApi 路由機(jī)制剖析
- C# WebApi 接口傳參詳解
- c#在WebAPI使用Session的方法
- c# 常見文件路徑Api的使用示例
相關(guān)文章
C#使用Exchange實(shí)現(xiàn)發(fā)送郵件
最近項(xiàng)目中需要用到exchange的操作,所以本文就參照msdn弄了一個簡單的C#操作類,實(shí)現(xiàn)了發(fā)送郵件和拉取收件箱的功能,感興趣的小伙伴可以了解下2023-10-10
C#讀取數(shù)據(jù)庫返回泛型集合詳解(DataSetToList)
本篇文章主要是對C#讀取數(shù)據(jù)庫返回泛型集合(DataSetToList)進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
C#以太網(wǎng)Sockets客戶端設(shè)計(jì)實(shí)現(xiàn)
本文主要介紹了C#以太網(wǎng)Sockets客戶端設(shè)計(jì)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C#實(shí)現(xiàn)排列組合算法完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下2014-09-09

