This topic describes how to use the Go software development kit (SDK) to detect facial attributes in specified images that are accessible over the Internet. This feature supports only images.
Description
Facial attribute detection identifies various facial attributes in an image, such as face blur, angle, position, smile intensity, and the presence of glasses, a mask, or a hat. It also detects beards, bangs, and hair types. For more information about the parameters, see Facial attribute detection API.
You must use the endpoints of the Content Moderation API to call this SDK. For more information about the API endpoints, see Endpoints.
This SDK supports only image URLs. It does not support local files or binary data.
Supported URL types: Public HTTP or 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 facial attribute detection 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 and avoid repeated connections.
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of a 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 the AccessKey ID of your RAM user from an environment variable.",
"Obtain the AccessKey secret of your RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal(
map[string]interface{}{
"url": "The URL of the face image to detect.",
},
)
request := green.CreateDetectFaceRequest()
request.SetContent(content)
response, _err := client.DetectFace(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())
}