你好,视觉智能平台非上海oss,在搜索人脸的时候,能不能给一个c#代码片段?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

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

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

展开
收起
cuicuicuic 2023-03-28 15:22:00 784 0
3 条回答
写回答
取消 提交回答
  • 2000元阿里云代金券免费领取,2核4G云服务器仅698元/3年,新老用户都有优惠,立即抢购>>>

    看下这个最佳实践,有c#的代码 https://help.aliyun.com/document_detail/478011.html?spm=a2c4g.11186623.0.0.50815c27XwmY6T,此回答整理自钉群“阿里云视觉智能开放平台咨询1群”

    2023-03-29 23:33:32
    赞同 展开评论 打赏
  • 全栈JAVA领域创作者

    2000元阿里云代金券免费领取,2核4G云服务器仅698元/3年,新老用户都有优惠,立即抢购>>>

    当然可以,以下是使用视觉智能平台进行人脸搜索的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。

    2023-03-29 20:09:36
    赞同 展开评论 打赏
  • 2000元阿里云代金券免费领取,2核4G云服务器仅698元/3年,新老用户都有优惠,立即抢购>>>

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

    2023-03-28 18:02:19
    赞同 展开评论 打赏
来源圈子
更多
收录在圈子:
阿里云视觉智能开放平台视觉AI能力问题咨询,请通过钉钉群(23109592)加入阿里云视觉智能开放平台咨询群
相关文档: 视觉智能开放平台
问答排行榜
最热
最新
相关电子书
更多
OSS运维基础实战手册
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多
http://www.vxiaotou.com