This topic describes how to use Content Moderation SDK for Go to moderate videos for risky content.
Description
Content Moderation SDK for Go supports both synchronous and asynchronous video moderation.
If you use synchronous video moderation, you can submit only a sequence of frames captured from a video for moderation. For more information about the related parameters, see /green/video/syncscan.
(Recommended) If you use asynchronous video moderation, you can submit a video or a sequence of frames captured from the video for moderation. For more information about the related parameters, see /green/video/asyncscan and /green/video/results.
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 asynchronous video moderation tasks
Operation | Description | Supported region |
VideoAsyncScanRequest | Sends asynchronous requests to moderate videos for risky content across multiple moderation scenarios, including pornography, terrorist content, ad, undesirable scene, and logo detection. |
|
Sample code
Submit the URL of a video for video moderation
package main import ( "encoding/json" "fmt" "github.com/aliyun/alibaba-cloud-sdk-go/services/green" "strconv" ) func main() { /** * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ client, _err := green.NewClientWithAccessKey( "cn-shanghai", "We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables") if err != nil { fmt.Println(err.Error()) return } task := map[string]interface{}{"dataId": "ID of the video to be moderated", "url": "URL of the video to be moderated"} // scenes: the moderation scenarios. You can specify one or more moderation scenarios. // callback and seed: The two parameters are optional and are used to configure a callback notification. content, _ := json.Marshal( map[string]interface{}{ "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"}, "bizType": "Business scenario", "callback": "Callback URL", "seed": "Random string", }, ) request := green.CreateVideoAsyncScanRequest() request.SetContent(content) response, _err := client.VideoAsyncScan(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 a video live stream for video moderation
package main import ( "encoding/json" "fmt" "github.com/aliyun/alibaba-cloud-sdk-go/services/green" "strconv" ) func main() { /** * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ client, _err := green.NewClientWithAccessKey( "cn-shanghai", "We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables") if err != nil { fmt.Println(err.Error()) return } // Set the url parameter to the URL of your live stream. task := map[string]interface{}{"dataId": "ID of the video to be moderated", "url": "URL of the video to be moderated"} // scenes: the moderation scenarios. You can specify one or more moderation scenarios. // callback and seed: The two parameters are optional and are used to configure a callback notification. content, _ := json.Marshal( map[string]interface{}{ "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"}, "live": "true", "bizType": "Business scenario", "callback": "Callback URL", "seed": "Random string", }, ) request := green.CreateVideoAsyncScanRequest() request.SetContent(content) response, _err := client.VideoAsyncScan(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 a video live stream to moderate both the video images and the audio
package main import ( "encoding/json" "fmt" "github.com/aliyun/alibaba-cloud-sdk-go/services/green" "strconv" ) func main() { /** * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. * Common ways to obtain environment variables: * Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ client, _err := green.NewClientWithAccessKey( "cn-shanghai", "We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables") if err != nil { fmt.Println(err.Error()) return } // Set the url parameter to the URL of your live stream. task := map[string]interface{}{"dataId": "ID of the video to be moderated", "url": "URL of the video to be moderated"} // scenes: the moderation scenarios. You can specify one or more moderation scenarios. // callback and seed: The two parameters are optional and are used to configure a callback notification. content, _ := json.Marshal( map[string]interface{}{ "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"}, "live": "true", "audioScenes": [...]string{"antispam"}, "bizType": "Business scenario", "callback": "Callback URL", "seed": "Random string", }, ) request := green.CreateVideoAsyncScanRequest() request.SetContent(content) response, _err := client.VideoAsyncScan(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 video moderation
Operation | Description | Supported region |
VideoAsyncScanResultsRequest | Queries the results of asynchronous video moderation. Note Instead of calling this operation to poll the moderation results, we recommend that you set the callback parameter when you submit asynchronous video moderation tasks to receive the moderation results. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
"We recommend that you obtain the AccessKey secret of your RAM user from environment variables")
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal(
[...]string{"ID of the asynchronous video moderation task"},
)
request := green.CreateVideoAsyncScanResultsRequest()
request.SetContent(content)
response, _err := client.VideoAsyncScanResults(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 synchronous video moderation tasks
Operation | Description | Supported region |
VideoSyncScanRequest | Sends synchronous requests to moderate videos for risky content. Note You can submit only a sequence of frames that are captured from a video for video moderation. To submit other types of videos, we recommend that you use the VideoAsyncScanRequest operation. |
|
Sample code
In this example, a sequence of frames that are captured from a video are to be moderated.
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
"We recommend that you obtain the AccessKey secret of your RAM user from environment variables")
if err != nil {
fmt.Println(err.Error())
return
}
frame1 := map[string]interface{}{
"offset": "0", "url": "URL of the captured video frame 1 to be moderated",
}
frame2 := map[string]interface{}{
"offset": "5", "url": "URL of the captured video frame 2 to be moderated",
}
frame3 := map[string]interface{}{
"offset": "10", "url": "URL of the captured video frame 3 to be moderated",
}
// frames: the captured video frames to be moderated.
task := map[string]interface{}{"dataId": "ID of the video to be moderated", "frames": [...]map[string]interface{}{frame1, frame2, frame3}}
// scenes: the moderation scenarios. You can specify one or more moderation scenarios.
content, _ := json.Marshal(
map[string]interface{}{
"tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"},
"bizType": "Business scenario",
},
)
request := green.CreateVideoSyncScanRequest()
request.SetContent(content)
response, _err := client.VideoSyncScan(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 moderation results
If the moderation results do not meet your expectations, you can call the VideoFeedbackRequest operation to modify the results. Content Moderation adds the moderated video frames to the similar image blacklist or whitelist based on your feedback. When you submit a similar video frame for moderation, Content Moderation returns moderation results based on the label in your feedback.
For more information, see /green/video/feedback.
Operation | Description | Supported region |
VideoFeedbackRequest | Provides feedback on video moderation results and modifies the machine-assisted moderation results based on the feedback. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
* Common ways to obtain environment variables:
* Obtain the AccessKey ID of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of your RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
"We recommend that you obtain the AccessKey secret of your RAM user from environment variables")
if _err != nil {
fmt.Println(_err.Error())
return
}
frame1 := map[string]interface{}{"url": "URL of the captured and moderated video frame 1", "offset": "Offset of the captured and moderated video frame 1"}
frame2 := map[string]interface{}{"url": "URL of the captured and moderated video frame 2", "offset": "Offset of the captured and moderated video frame 2"}
frames := [...]map[string]interface{}{frame1, frame2}
// 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 video is normal. A value of block indicates that the moderated video contains violations.
content, _ := json.Marshal(
map[string]interface{}{
"taskId": "ID of the video moderation task", "dataId": "ID of the moderated video", "url": "URL of the moderated video", "frames": frames,
"suggestion": "block", "scenes": [...]string{"ad", "terrorism"}, "note": "Remarks",
},
)
request := green.CreateVideoFeedbackRequest()
request.SetContent(content)
response, err := client.VideoFeedback(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())
}