Custom face retrieval

更新时间:
复制 MD 格式

Use the Content Moderation SDK for .NET to search a person group for faces similar to a query image.

How it works

Submit a face retrieval task with a face image URL and a target person group. Content Moderation compares the query image against all persons in that group and returns up to 5 matches, sorted by similarity score in descending order. Each request can target one person group only.

Prerequisites

Before you begin, make sure you have:

  • Installed the Content Moderation SDK for .NET dependencies. See Installation for instructions and the required .NET version. Using a different .NET version causes operation calls to fail.

  • Obtained the Content Moderation API endpoints.

Submit a face retrieval task

The following example uses ImageSyncScanRequest to submit a synchronous face retrieval task. Set scenes to sface-n and specify the target person group via extras.groupId.

Store your AccessKey ID and AccessKey secret in environment variables before running the code.

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.
            // Storing credentials in environment variables keeps them out of your source code.
            string accessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
            string accessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

            DefaultProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                accessKeyId,
                accessKeySecret);

            // Reuse the client instance across requests to improve performance
            // and avoid repeated connection overhead.
            DefaultAcsClient client = new DefaultAcsClient(profile);

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

            // Specify the person group to search within.
            Dictionary<string, object> extras = new Dictionary<string, object>();
            extras.Add("groupId", "<person-group-id>");

            // Specify the image to use as the search query.
            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "<data-id>");
            task1.Add("url", "<face-image-url>");
            task1.Add("extras", extras);

            // Set scenes to "sface-n" for custom face retrieval.
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("scenes", new List<string> { "sface-n" });
            httpBody.Add("bizType", "<business-scenario>");
            httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });

            request.SetContent(
                System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
                "utf-8",
                FormatType.JSON);

            try
            {
                ImageSyncScanResponse 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 the following placeholders with actual values:

PlaceholderDescription
<person-group-id>ID of the person group to search
<data-id>Unique identifier for the image data
<face-image-url>URL of the face image to use as the query
<business-scenario>Your business scenario name

What's next