C# 調用百度 API 進行活體檢測


活體檢測有多種情形,本文所指:從攝像頭獲取的影像中判斷是活體,還是使用了相片等靜態圖片。

場景描述

用戶個人信息中上傳了近照,當用戶經過攝像頭時進行身份識別。

此時,如果單純的使用攝像頭獲取的影像進行人臉相似度比對,則舉一張合適的相片對准攝像頭也是可以通過的。於是檢測攝像頭前影像是否為活體的需求就產生了。

解決方案

使用百度AI開放平台,它免費開放一定並發量的該場景活體檢測 API:
https://ai.baidu.com/tech/face/faceliveness

第一步,申請百度應用


點擊“立即使用”,登錄后“創建應用”,可以得到 API Key 與 Secret Key 等信息。

第二步,使用 API 進行活體檢測

這里的場景比較簡單,攝像頭獲取的影像可以保存為圖片,則功能接口可以這樣定義:給定圖片(這里使用URL),判斷其活體影像的概率。根據百度建議,概率設置為 99.5%,即達到此值或以上認為活體檢測通過。

(1)獲取 accessToken
accessToken 有效期為 30 天,因此,可以緩存起來使用。此為示例,時長又足夠長,所以未加刷新機制。代碼如下,其中,clientId 為百度應用中的 API Key,clientSecret 為百度應用中的 Secret Key。

public static class AccessToken
{
    // 有效期30天,緩存獲取的 access token
    public static String TOKEN = null;

    // 百度雲中開通對應服務應用的 API Key
    private static String clientId = "API Key";
    // 百度雲中開通對應服務應用的 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)調用 API 取得活體概率
API 的返回結果為 JSON,其中包括了活體概率,這里,方法直接返回 API 的 JSON 結果。

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;
    }
}

詳細 API 文檔見此:https://ai.baidu.com/docs#/Face-Liveness-V3/top

結果中:face_liveness 即表示“活體分數值”。

(3)應用
API 的調用結果中,error_code 為 0 時表示執行成功,此時,會有 result 屬性表示計算的相關值,從中取出 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 時表示執行成功,其它表示失敗
if (jresult.Value<int>("error_code") == 0)
{
    double face_liveness = lvresult.Value<double>("face_liveness");
    // 活體率達到要求
    if (face_liveness >= 0.995)
    {
        // 通過檢測
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM