This topic describes how to use Content Moderation SDK for Go to moderate images for risky content.
Description
Image Moderation provides two detection modes: synchronous and asynchronous.
Synchronous detection provides real-time results. For more information about the parameters, see Synchronous Detection.
If you use asynchronous image moderation, you must poll the moderation results or configure a callback notification to receive the moderation results. For more information about the related parameters, see /green/image/asyncscan and /green/image/results.
This SDK supports only image URLs. It does not support local files or binary data.
The supported URL types are Internet HTTP or HTTPS URLs that are up to 2,048 characters in length.
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.
(Recommended) Submit synchronous image moderation tasks
Operation | Description | Supported region |
ImageSyncScanRequest | Sends synchronous requests to moderate images for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, QR code, undesirable scene, and logo detection. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: Reuse the client instance to improve detection performance.
* This avoids establishing new connections for each request.
* To obtain environment variables, you can use the following methods:
* To obtain the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* To 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 a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
task1 := map[string]interface{}{"dataId": "The ID of the data to be detected", "url": "The URL of the image to be detected"}
// scenes: The detection scenarios. You can specify multiple scenarios.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": task1, "scenes": [...]string{"porn", "terrorism"}, "bizType": "The 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())
}Submit asynchronous image moderation tasks
You can use the Go SDK to detect threats in images. You can submit an asynchronous task and retrieve the results through a callback or by polling. The supported input content for asynchronous moderation is the same as that for synchronous moderation.
Operation | Description | Supported region |
ImageAsyncScanRequest | Sends asynchronous requests to moderate images for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, QR code, undesirable scene, and logo detection. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: Reuse the client instance to improve detection performance.
* This avoids establishing new connections for each request.
* To obtain environment variables, you can use the following methods:
* To obtain the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* To 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 a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
task1 := map[string]interface{}{"dataId": "The ID of the data to be detected", "url": "The URL of the image to be detected"}
// scenes: The detection scenarios. You can specify multiple scenarios.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": [...]map[string]interface{}{task1},
// The callback and seed parameters are used for webhook notifications. These parameters are optional.
"callback": "The webhook address",
"seed": "A random string",
"scenes": [...]string{"porn", "terrorism"},
"bizType": "The business scenario",
},
)
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()))
}
fmt.Println(response.GetHttpContentString())
}Query the results of asynchronous image moderation
Operation | Description | Supported region |
ImageAsyncScanResultsRequest | Queries asynchronous image moderation results. You can query the moderation results of multiple asynchronous image moderation tasks at a time. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: Reuse the client instance to improve detection performance.
* This avoids establishing new connections for each request.
* To obtain environment variables, you can use the following methods:
* To obtain the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* To 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 a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal(
[...]string{"The ID of the asynchronous image moderation task"},
)
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()))
}
fmt.Println(response.GetHttpContentString())
}Provide feedback on image moderation results
If an image moderation result does not meet your expectations, you can call the ImageScanFeedbackRequest operation to correct the result. Based on your feedback, the system adds the image to a blacklist or whitelist of similar images. When you submit similar content for moderation in the future, the service returns a result based on the label in your feedback. For more information about the operation, see Provide feedback on moderation results.
Interface | Description | Supported region |
ImageScanFeedbackRequest | Submit feedback on image detection results to help refine the algorithm. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: Reuse the client instance to improve detection performance.
* This avoids establishing new connections for each request.
* To obtain environment variables, you can use the following methods:
* To obtain the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* To 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 a RAM user from an environment variable.",
"Obtain the AccessKey secret of a RAM user from an environment variable.")
if _err != nil {
fmt.Println(err.Error())
return
}
// scenes: The detection scenarios. You can specify multiple scenarios.
// suggestion: The expected moderation result. pass: The image is normal. block: The image contains violations.
content, _ := json.Marshal(
map[string]interface{}{
"suggestion": "block", "scenes": [...]string{"porn", "terrorism"}, "url": "The URL of the image to be detected",
},
)
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()))
}
fmt.Println(response.GetHttpContentString())
}