你好,视觉智能平台非上海oss,在搜索人脸的时候,能不能给一个c#代码片段?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
看下这个最佳实践,有c#的代码 https://help.aliyun.com/document_detail/478011.html?spm=a2c4g.11186623.0.0.50815c27XwmY6T,此回答整理自钉群“阿里云视觉智能开放平台咨询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://aip.baidubce.com/rest/2.0/face/v3/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<string, object> dict = new Dictionary<string, object>();
        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。
文件在本地或文件不在同一地域OSS,可以看下人脸搜索的最佳实践文档,有详细的C#调用代码。https://help.aliyun.com/document_detail/478011.htm