This topic describes how to call the face comparison SDK for Go to compare specified face images and detect the similarity between different face images.
Feature description
Face comparison supports synchronous detection and asynchronous detection.
If you use synchronous face comparison, the detection results are returned in real time. For more information about the related parameters, see Synchronous moderation.
If you use asynchronous face comparison, you must poll the detection results or configure a callback notification to receive the detection results. For more information about the related parameters, see Asynchronous moderation.
This SDK supports only image URLs. It does not support local files or binary data.
Supported URL types: Public HTTP/HTTPS URLs with a maximum length of 2,048 characters.
Prerequisites
Go dependencies are installed. For more information, see Installation.
You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
Submit a face comparison task
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: Reuse the instantiated client as much as possible to improve detection performance. This avoids repeated connections.
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of a RAM user: 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
}
// Set the URL of face image 2.
extras := map[string]interface{}{"faceUrl": "The URL of face image 2"}
// Set the URL of face image 1.
task1 := map[string]interface{}{"dataId": "dataIdxxx", "url": "The URL of face image 1", "extras": extras}
// scenes: the image detection scenario. Set the value to sface-1.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": task1, "scenes": [...]string{"sface-1"}, "bizType": "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()))
}
fmt.Println(response.GetHttpContentString())
}