Add faces

更新时间:
复制 MD 格式

Use the .NET SDK to add face images to a person in your Content Moderation face library.

Prerequisites

Before you begin, make sure you have:

  • Installed the required .NET dependencies using the exact .NET version specified in Install .NET dependencies — using a different version causes API calls to fail

  • A personId for an existing person in your face library

  • The AI Guardrails API endpoint for your region. See Endpoints

Parameters

ParameterRequiredTypeDescription
personIdYesStringThe ID of the custom person in your face library.
urlsNoArray of stringsThe URLs of the face images to add. A person can have a maximum of 20 faces.

For the full parameter reference, see API operation for adding faces.

Add faces to a person

The following example adds two face images to an existing person. All requests use the AI Guardrails API endpoint and POST the request body as JSON.

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",
                    "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                    "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
            DefaultAcsClient client = new DefaultAcsClient(profile);

            AddFacesRequest request = new AddFacesRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            /**
            * personId: the ID of the custom person. This parameter is required.
            * urls: the URLs of face images.
            */
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("personId", "Person ID");
            httpBody.Add("urls", new List<string> { "URL of face image 1", "URL of face image 2" });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                AddFacesResponse 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);
            }
        }
    }
}

Replace the placeholder values before running the code:

PlaceholderDescriptionExample
Person IDThe personId of the target person in your face libraryperson_001
URL of face image 1, URL of face image 2Publicly accessible URLs of the face images to addhttps://example.com/face1.jpg

Store your AccessKey ID and AccessKey secret in environment variables (ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET) instead of hardcoding them in your source code.

What's next