Query information about face images

更新时间:
复制 MD 格式

Use the .NET SDK to retrieve face IDs and image paths for a specific person in your custom face library.

Usage notes

Specify a person ID and one or more face IDs to retrieve the face metadata stored for that person, including face IDs and the paths of the associated face images. For the full parameter reference, see API operation for querying information about face images.

Call this operation through the AI Guardrails API endpoints. For endpoint details, see Endpoint.

Prerequisites

Before you begin, ensure that you have:

  • Installed the .NET dependencies. See Install .NET dependencies for instructions.

  • Used the required .NET version specified in the Installation topic. Using a different version causes subsequent operation calls to fail.

Query face image information

The following example uses GetFacesRequest to query face metadata for a custom person.

Request parameters

ParameterTypeRequiredDescription
personIdStringYesThe ID of the custom person
faceIdsList\<String\>YesThe IDs of the faces to query
Store your AccessKey ID and AccessKey secret as environment variables rather than hardcoding them in your code. Use ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET as the variable names.
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);

            GetFacesRequest request = new GetFacesRequest();
            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.
            * faceIds: the IDs of faces. This parameter is required.
            */
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("personId", "Person ID");
            httpBody.Add("faceIds", new List<string> { "Face ID_1", "Face ID_2" });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                GetFacesResponse 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);
            }
        }
    }
}
Reuse the DefaultAcsClient instance across requests. Creating a new client for each request degrades moderation performance and increases connection overhead.

What's next