Use the Content Moderation Go SDK to compare two face images and get a similarity score. The SDK submits images by URL and supports synchronous and asynchronous detection modes.
How it works
Initialize a client with your AccessKey credentials and the target region.
Build a task payload with the URLs of the two face images and set the scene to
sface-1.Submit the task using the synchronous or asynchronous scan API.
Parse the response to get the similarity score.
Limitations
Input format: Image URLs only. Local files and binary data are not supported.
URL protocol: Public HTTP or HTTPS URLs only.
URL length: Maximum 2,048 characters per URL.
Prerequisites
Before you begin, ensure that you have:
Go dependencies installed. See Installation for the required Go version. Using a different version causes operation calls to fail.
Submit a face comparison task
The following example uses synchronous detection. For asynchronous detection — where you poll for results or configure a callback notification — see Synchronous moderation and Asynchronous moderation for the full parameter reference.
Reuse the client instance across requests to improve performance and avoid repeated connection overhead.
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
// Initialize the client. Reuse this instance across requests.
// Obtain credentials from environment variables to avoid hardcoding sensitive values:
// AccessKey ID: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
// AccessKey secret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Obtain your AccessKey ID from an environment variable.",
"Obtain your AccessKey secret from an environment variable.")
if _err != nil {
fmt.Println(_err.Error())
return
}
// extras carries the URL of face image 2.
extras := map[string]interface{}{"faceUrl": "<url-of-face-image-2>"}
// task1 carries the URL of face image 1.
task1 := map[string]interface{}{
"dataId": "dataIdxxx",
"url": "<url-of-face-image-1>",
"extras": extras,
}
// scenes: set to "sface-1" for face comparison.
// bizType: your business scenario identifier.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": task1,
"scenes": [...]string{"sface-1"},
"bizType": "<your-business-scenario>",
},
)
request := green.CreateImageSyncScanRequest()
request.SetContent(content)
response, _err := client.ImageSyncScan(request)
if _err != nil {
fmt.Println(_err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
return
}
fmt.Println(response.GetHttpContentString())
}Replace the following placeholders with actual values:
| Placeholder | Description | Example |
|---|---|---|
<url-of-face-image-1> | Public URL of the first face image | https://example.com/face1.jpg |
<url-of-face-image-2> | Public URL of the second face image | https://example.com/face2.jpg |
<your-business-scenario> | Your bizType identifier | default |
What's next
Review the full parameter reference for Synchronous moderation to understand all available request and response fields, including the similarity score returned in the response.
To receive results without polling, configure a callback notification using Asynchronous moderation.