你好,视觉智能平台非上海oss,在搜索人脸的时候,能不能给一个c#代码片段?[视觉智能开放平台]

你好,视觉智能平台非上海oss,在搜索人脸的时候,能不能给一个c#代码片段?

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
=====这是一个广告位,招租中,联系qq 78315851====
2 条回复 A 作者 M 管理员
  1. 当然可以,以下是使用视觉智能平台进行人脸搜索的C#代码示例:

    csharp using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json;

    namespace BaiApiDemo { class Program { static void Main(string[] args) { string apiKey = “YOUR_API_KEY”; string apiSecret = “YOUR_API_SECRET”; string accessToken = GetAccessToken(apiKey, apiSecret);

            string url = "https://www.www.tongchenyun.com/wp-content/uploads/aliyun/2023/1128/search;        string imageBase64 = Convert.ToBase64String(File.ReadAllBytes(your_image_path.jpg"));        string imageType = "BASE64";        string groupIdList = "your_group_id_list"; // 多个用逗号隔开        int maxUserNum = 1;        string faceType = "LIVE";        string qualityControl = "NONE";        string livenessControl = "NONE";        Dictionary dict = new Dictionary();        dict.Add("image", imageBase64);        dict.Add("image_type", imageType);        dict.Add("group_id_list", groupIdList);        dict.Add("max_user_num", maxUserNum);        dict.Add("face_type", faceType);        dict.Add("quality_control", qualityControl);        dict.Add("liveness_control", livenessControl);        string content = JsonConvert.SerializeObject(dict);        StringContent httpContent = new StringContent(content, Encoding.UTF8);        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");        HttpClient httpClient = new HttpClient();        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);        HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;        if (response.IsSuccessStatusCode)        {            string responseContent = response.Content.ReadAsStringAsync().Result;            Console.WriteLine(responseContent);        }        else        {            Console.WriteLine("Response error: " + response.StatusCode);        }        Console.ReadLine();    }    static string GetAccessToken(string apiKey, string apiSecret)    {        string url = $"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={apiKey}&client_secret={apiSecret}";        HttpClient httpClient = new HttpClient();        HttpResponseMessage response = httpClient.GetAsync(url).Result;        if (response.IsSuccessStatusCode)        {            string responseContent = response.Content.ReadAsStringAsync().Result;            dynamic jsonResult = JsonConvert.DeserializeObject(responseContent);            string accessToken = jsonResult.access_token;            return accessToken;        }        else        {            throw new Exception("Failed to get access token from Baidu API");        }    }}

    } 请将上述代码中的 YOUR_API_KEY 和 YOUR_API_SECRET 替换为您自己的百度API Key和Secret。另外,请将 your_image_path.jpg 替换为您要搜索的人脸图像路径, your_group_id_list 替换为您已经创建的人脸库ID。

  2. 文件在本地或文件不在同一地域OSS,可以看下人脸搜索的最佳实践文档,有详细的C#调用代码。https://help.aliyun.com/document_detail/478011.htm