Facial attribute detection

更新时间:
复制 MD 格式

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.

Note

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:

AttributeDescription
BlurBlurriness of the face in the image
AngleHead pose, including yaw, pitch, and roll
PositionLocation of the face in the image (bounding box)
Smile intensityDegree of smile detected on the face
Hair typeHair style detected
GlassesWhether glasses are present
Face maskWhether a face mask is present
HatWhether a hat is present
BeardWhether a beard is present
BangsWhether 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

ParameterRequiredTypeDescription
urlYesStringPublic HTTP or HTTPS URL of the image to analyze. Maximum length: 2,048 characters.

Usage notes

  • Reuse the DefaultAcsClient instance 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