This topic describes how to use the Go software development kit (SDK) to add images to an image search library.
Description
When you add similar image samples, you can set tags for the images. If a sample image is matched during a search, its tags are also returned. For more information about the parameters, see the AddSimilarityImage API documentation.
You must use the AI Guardrails endpoint to call the service using the SDK. For more information about API endpoints, see Endpoints.
-
This SDK supports only image URLs. It does not support local files or binary data.
-
The following types of URLs are supported:
-
Public HTTP and HTTPS URLs. The URLs cannot exceed 2,048 characters in length.
-
Alibaba Cloud Object Storage Service (OSS) URLs in the format of
oss://<bucket-name>.<endpoint>/<object-name>. You must grant AI Guardrails the required permissions to access the destination OSS bucket. The bucket and the AI Guardrails service must be in the same region. For more information, see Authorize AI Guardrails to access OSS buckets.
-
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.
Add similar graph sample task
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: To improve detection performance, reuse the client instance. This avoids repeated connection creation.
* Common methods 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",
"Get the AccessKey ID of a RAM user from an environment variable.",
"Get the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
task := map[string]interface{}{
"dataId": "Data ID",
"url": "Sample image URL",
"tag": [...]string{"Custom tag 1", "Custom tag 2", "Custom tag 3"},
}
content, _ := json.Marshal(
map[string]interface{}{
"library": "Name of the image search library", "tasks": [...]map[string]interface{}{task},
},
)
request := green.CreateAddSimilarityImageRequest()
request.SetContent(content)
response, _err := client.AddSimilarityImage(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())
}