开发者社区 > 视觉智能 > 正文

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

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

展开
收起
cuicuicuic 2023-03-28 15:22:00 854 0
3 条回答
写回答
取消 提交回答
  • 看下这个最佳实践,有c#的代码 https://help.aliyun.com/document_detail/478011.html?spm=a2c4g.11186623.0.0.50815c27XwmY6T,此回答整理自钉群“阿里云视觉智能开放平台咨询1群”

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

    当然可以,以下是使用视觉智能平台进行人脸搜索的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
    赞同 展开评论 打赏
  • 文件在本地或文件不在同一地域OSS,可以看下人脸搜索的最佳实践文档,有详细的C#调用代码。https://help.aliyun.com/document_detail/478011.htm

    2023-03-28 18:02:19
    赞同 展开评论 打赏

为开发者提供高易用、普惠的视觉API服务,帮助企业快速建立视觉智能技术应用能力的综合性视觉AI能力平台。适用于数字营销、互联网娱乐、安防、手机应用、泛金融身份认证等行业。

相关产品

  • 视觉智能开放平台
  • 热门讨论

    热门文章

    相关电子书

    更多
    OSS运维进阶实战手册 立即下载
    《OSS运维基础实战手册》 立即下载
    OSS运维基础实战手册 立即下载