This topic describes how to use the .NET software development kit (SDK) to detect facial attributes in images from public URLs. This feature supports only images.
Features
Facial attribute detection identifies various properties of a face in an image, such as blur, angle, position, smile intensity, hair type, and the presence of glasses, a mask, a hat, a beard, or bangs. For more information about the parameters, see Facial attribute detection API reference.
You can call this SDK operation using the Content Moderation API endpoints. For more information about the API endpoints, see Endpoints.
This SDK supports only image URLs. It does not support local files or binary data.
Supported URL types: public HTTP or HTTPS URLs that are no more than 2,048 characters in length.
Prerequisites
The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.
You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
Submit a facial attribute detection task
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)
{
/**
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Obtain the AccessKey ID of your RAM user from an environment variable",
"Obtain the AccessKey secret of your RAM user from an environment variable");
// Note: Reuse the instantiated client to improve detection performance and avoid creating repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
DetectFaceRequest request = new DetectFaceRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("url", "The URL of the face image that you want to detect");
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
DetectFaceResponse 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);
}
}
}
}