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.
Usage notes
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.
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: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"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");
if err != nil {
fmt.Println(err.Error())
return
}
// Specify the URL of face image 2 to be compared.
extras := map[string]interface{}{"faceUrl": "URL of face image 2 to be compared"}
// Specify the URL of face image 1 to be compared.
task1 := map[string]interface{}{"dataId": "dataIdxxx", "url": "URL of face image 1 to be compared", "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())
}