Image moderation
Use the AI Guardrails SDK for Go to moderate images for risky content, including pornography, terrorist content, ads, QR codes, undesirable scenes, and logos.
How it works
Image Moderation supports two detection modes:
| Mode | How it works | When to use |
|---|---|---|
| Synchronous | Submits a moderation request and returns results immediately | You need a decision before processing or displaying the image (low-latency, real-time use cases) |
| Asynchronous | Submits a moderation task and returns a task ID; retrieve results later by polling or callback | You are processing images in bulk and can tolerate a delay in results |
Limitations
This SDK accepts only image URLs. Local files and binary data are not 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.
The following types of URLs are supported:
Prerequisites
Before you begin, make sure you have:
Installed Go dependencies. See Installation for the required Go version. Using an unsupported version causes subsequent operation calls to fail.
A RAM user AccessKey ID and AccessKey secret stored as environment variables:
ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET
Submit synchronous image moderation tasks (recommended)
ImageSyncScanRequest sends a synchronous request to moderate images and returns results immediately.
| Parameter | Description |
|---|---|
| Supported regions | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
| Supported scenarios | Pornography, terrorist content, ad, QR code, undesirable scene, logo detection |
For full parameter details, see Synchronous Detection.
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)
func main() {
// Reuse the client instance across requests to avoid connection overhead.
client, err := green.NewClientWithAccessKey(
"cn-shanghai",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
)
if err != nil {
fmt.Println(err.Error())
return
}
task := map[string]interface{}{
"dataId": "<data-id>", // A unique ID for tracking this moderation task
"url": "<image-url>", // The HTTP or HTTPS URL of the image to moderate
}
// scenes: one or more detection scenarios to run
content, _ := json.Marshal(map[string]interface{}{
"tasks": []interface{}{task},
"scenes": []string{"porn", "terrorism"},
"bizType": "<biz-type>", // Your business scenario identifier
})
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:
| Placeholder | Description | Example |
|---|---|---|
<data-id> | A unique identifier for tracking this moderation task | img-001 |
<image-url> | The HTTP or HTTPS URL of the image to moderate | https://example.com/photo.jpg |
<biz-type> | Your business scenario identifier | ecommerce-product |
Submit asynchronous image moderation tasks
ImageAsyncScanRequest submits an asynchronous moderation task. Retrieve results by polling or by configuring a callback notification. The supported input format is the same as synchronous moderation.
| Parameter | Description |
|---|---|
| Supported regions | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
| Supported scenarios | Pornography, terrorist content, ad, QR code, undesirable scene, logo detection |
For full parameter details, see /green/image/asyncscan and /green/image/results.
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)
func main() {
// Reuse the client instance across requests to avoid connection overhead.
client, err := green.NewClientWithAccessKey(
"cn-shanghai",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
)
if err != nil {
fmt.Println(err.Error())
return
}
task := map[string]interface{}{
"dataId": "<data-id>", // A unique ID for tracking this moderation task
"url": "<image-url>", // The HTTP or HTTPS URL of the image to moderate
}
// scenes: one or more detection scenarios to run
content, _ := json.Marshal(map[string]interface{}{
"tasks": []map[string]interface{}{task},
"scenes": []string{"porn", "terrorism"},
"bizType": "<biz-type>", // Your business scenario identifier
// callback and seed are optional. Include them to receive results via webhook.
"callback": "<callback-url>", // Your webhook URL for receiving results
"seed": "<seed>", // A random string for verifying webhook requests
})
request := green.CreateImageAsyncScanRequest()
request.SetContent(content)
response, err := client.ImageAsyncScan(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:
| Placeholder | Description | Example |
|---|---|---|
<data-id> | A unique identifier for tracking this moderation task | img-001 |
<image-url> | The HTTP or HTTPS URL of the image to moderate | https://example.com/photo.jpg |
<biz-type> | Your business scenario identifier | ecommerce-product |
<callback-url> | (Optional) Your webhook URL for receiving results | https://your-server.com/callback |
<seed> | (Optional) A random string for verifying webhook authenticity | abc123xyz |
Query asynchronous image moderation results
ImageAsyncScanResultsRequest retrieves the results of one or more asynchronous moderation tasks by task ID.
| Parameter | Description |
|---|---|
| Supported regions | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)
func main() {
// Reuse the client instance across requests to avoid connection overhead.
client, err := green.NewClientWithAccessKey(
"cn-shanghai",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
)
if err != nil {
fmt.Println(err.Error())
return
}
// Pass one or more task IDs returned by ImageAsyncScanRequest.
content, _ := json.Marshal([]string{"<task-id>"})
request := green.CreateImageAsyncScanResultsRequest()
request.SetContent(content)
response, err := client.ImageAsyncScanResults(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 <task-id> with the task ID returned by ImageAsyncScanRequest. Pass multiple IDs in the array to query multiple tasks at once.
Provide feedback on image moderation results
If a moderation result is incorrect, use ImageScanFeedbackRequest to correct it. The service adds the image to a blacklist or whitelist of similar images, so future moderation requests for similar content return results based on your feedback.
For more information, see Provide feedback on moderation results.
| Parameter | Description |
|---|---|
| Supported regions | cn-shanghai (China (Shanghai)), cn-beijing (China (Beijing)), cn-shenzhen (China (Shenzhen)), ap-southeast-1 (Singapore) |
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)
func main() {
// Reuse the client instance across requests to avoid connection overhead.
client, err := green.NewClientWithAccessKey(
"cn-shanghai",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
)
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal(map[string]interface{}{
"url": "<image-url>", // The HTTP or HTTPS URL of the image
"scenes": []string{"porn", "terrorism"},
// suggestion: the correct moderation result
// pass — the image is normal (add to whitelist)
// block — the image contains violations (add to blacklist)
"suggestion": "block",
})
request := green.CreateImageScanFeedbackRequest()
request.SetContent(content)
response, err := client.ImageScanFeedback(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 <image-url> with the URL of the image to submit feedback for. Set suggestion to pass if the image is normal, or block if it contains violations.