本文介绍了如何使用.NET SDK人脸比对接口,对指定的人脸图片进行属性检测。人脸属性检测仅支持以图片作为检测媒介。
功能描述
人脸比对支持同步检测和异步检测两种方式。
前提条件
提交人脸比对任务
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* 常见获取环境变量方式:
* 获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* 获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"建议从环境变量中获取RAM用户AccessKey ID",
"建议从环境变量中获取RAM用户AccessKey Secret");
// 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest request = new ImageSyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// 设置待比对人脸图片链接地址2。
Dictionary<string, object> extras = new Dictionary<string, object>();
extras.Add("faceUrl", "待比对人脸图片链接地址2");
// 设置待比对人脸图片链接地址1。
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "检测数据ID");
task1.Add("url", "待比对人脸图片链接地址1");
task1.Add("extras", extras);
// scenes:检测场景,指定图片检测的应用场景,取值:sface-1。
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "sface-1" });
httpBody.Add("bizType", "业务场景");
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
ImageSyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}
文档内容是否对您有帮助?