Detect facial attributes in images using the Content Moderation software development kit (SDK) for .NET. The SDK analyzes a face in an image and returns properties such as blur, angle, position, smile intensity, hair type, and the presence of glasses, a face mask, a hat, a beard, or bangs.
This SDK accepts only public HTTP or HTTPS image URLs up to 2,048 characters. It does not support local files or binary data.
Prerequisites
Before you begin, make sure that you have:
Installed the Content Moderation SDK for .NET using the .NET version specified in Installation. Using a different .NET version causes API calls to fail.
An AccessKey ID and AccessKey secret for a RAM user with permissions to call the Content Moderation API.
Detectable attributes
The SDK returns the following facial attributes for each detected face:
| Attribute | Description |
|---|---|
| Blur | Blurriness of the face in the image |
| Angle | Head pose, including yaw, pitch, and roll |
| Position | Location of the face in the image (bounding box) |
| Smile intensity | Degree of smile detected on the face |
| Hair type | Hair style detected |
| Glasses | Whether glasses are present |
| Face mask | Whether a face mask is present |
| Hat | Whether a hat is present |
| Beard | Whether a beard is present |
| Bangs | Whether bangs are present |
For the full response schema and field-level details, see the Facial attribute detection API reference.
Detect facial attributes
The following example submits a facial attribute detection request for a public image URL.
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)
{
// Load credentials from environment variables.
// Do not hardcode credentials in your source code.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Reuse the client instance to improve performance and avoid 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";
// Set the URL of the image to analyze.
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("url", "<image-url>");
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("Request failed. Status: {0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Request failed: {0}", ex.Message);
}
}
}
}Replace <image-url> with a public HTTP or HTTPS URL no longer than 2,048 characters.
Request parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
url | Yes | String | Public HTTP or HTTPS URL of the image to analyze. Maximum length: 2,048 characters. |
Usage notes
Reuse the
DefaultAcsClientinstance across requests. Creating a new client for each request degrades performance and may exhaust connection limits.The SDK does not accept local file paths or binary image data — only public URLs.
What's next
Facial attribute detection API reference — full request and response schema with field definitions
Endpoints — select the endpoint for your region
Installation — SDK installation and .NET version requirements