This topic describes how to use Content Moderation SDK for Go to moderate images for risky content.

Background information

Content Moderation SDK for Go supports both synchronous and asynchronous image moderation.

  • If you use synchronous image moderation, the moderation results are returned in real time. For more information about parameters, see Synchronous moderation.
  • 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 parameters, see Asynchronous moderation.

You can submit the URL of an online image, the URL of a local image, or a binary image stream for image moderation.

Prerequisites

Go dependencies are installed. For more information, see Installation.
Note You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit synchronous image moderation tasks (Recommended)

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.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if err != nil {
      fmt.Println(err.Error())
      return
   }

   task1 := map[string]interface{}{"dataId": "ID of the image to be moderated", "url": "URL of the image to be moderated"}
   // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "tasks": task1, "scenes": [...]string{"porn", "terrorism"}, "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())
}

Submit asynchronous image moderation tasks

This section shows you how to use Content Moderation SDK for Go to submit asynchronous image moderation tasks. When you submit a request, you can configure a callback notification to receive the moderation results. Alternatively, you can call the ImageAsyncScanResultsRequest operation to poll the moderation results.

Same as synchronous image moderation, you can submit the URL of an online image, the URL of a local image, or a binary image stream for asynchronous image moderation. In this example, the URL of an online image is used.

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.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if err != nil {
      fmt.Println(err.Error())
      return
   }

   task1 := map[string]interface{}{"dataId": "ID of the image to be moderated", "url": "URL of the image to be moderated"}
   // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "tasks": [...]map[string]interface{}{task1}, "scenes": [...]string{"porn", "terrorism"},
         "bizType": "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.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if err != nil {
      fmt.Println(err.Error())
      return
   }

   content, _ := json.Marshal(
      [...]string{"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())
}

Give feedback on moderation results

If the results of image moderation are not as expected, you can call the ImageScanFeedbackRequest operation to modify the results. Content Moderation adds the moderated image to the similar image blacklist or whitelist based on your feedback. When you submit a similar image for moderation, Content Moderation returns moderation results based on the label in your feedback.

For more information, see Give feedback on moderation results.

Operation Description Supported region
ImageScanFeedbackRequest Provides feedback on image moderation results and modifies the machine-assisted moderation results based on the feedback.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, _err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
  // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
   // suggestion: the moderation result that you expect to return. A value of pass indicates that the moderated image is normal. A value of block indicates that the moderated image contains violations. 
   content, _ := json.Marshal(
      map[string]interface{}{
         "suggestion": "block", "scenes": [...]string{"porn", "terrorism"}, "url": "URL of the image to be moderated",
      },
   )

   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())
}