Add faces

更新时间:
复制 MD 格式

Use the Go SDK to add face images to a custom person in Content Moderation's face library. Each person supports up to 20 face images.

Prerequisites

Before you begin, make sure that you have:

  • The Go SDK installed using the required Go version. For details, see Installation.

  • A custom person ID (personId) for the target person.

  • Publicly accessible URLs for the face images to add.

Important

Use the Go version specified in the Installation topic. An unsupported version causes all API calls to fail.

Usage notes

  • Use the AI Guardrails API endpoints when calling this SDK. Generic endpoints are not supported.

  • A person can have a maximum of 20 face images.

  • Reuse the instantiated client across requests to improve performance and avoid repeated connection overhead.

  • For full parameter details, see API reference for adding faces.

Add faces

The following example initializes a client, sets the required parameters, and calls the AddFaces operation.

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)

func main() {
	// Initialize the client with your region and credentials.
	// Load the AccessKey ID and AccessKey secret from environment variables
	// to avoid hardcoding credentials in your code.
	client, err := green.NewClientWithAccessKey(
		"cn-shanghai",
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
	)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	// Build the request body.
	// personId is required. urls is the list of face image URLs to add.
	content, _ := json.Marshal(map[string]interface{}{
		"personId": "<your-person-id>",
		"urls":     []string{"<face-image-url-1>", "<face-image-url-2>"},
	})

	request := green.CreateAddFacesRequest()
	request.SetContent(content)

	response, err := client.AddFaces(request)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if response.GetHttpStatus() != 200 {
		fmt.Println("Request failed. HTTP status code: " + strconv.Itoa(response.GetHttpStatus()))
		return
	}

	fmt.Println(response.GetHttpContentString())
}

Replace the placeholders before running the code:

PlaceholderDescriptionExample
<your-person-id>The ID of the custom person to add faces toperson_001
<face-image-url-1>A publicly accessible URL of a face imagehttps://example.com/face1.jpg

Parameters

ParameterRequiredTypeDescription
personIdYesStringThe ID of the custom person.
urlsNoArray of stringsThe URLs of face images to add. Each URL must be publicly accessible.

What's next