This topic describes how to use Content Moderation SDK for Go to moderate videos for risky content.
Description
The Video Moderation API provides two detection methods: synchronous and asynchronous.
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.
The SDK accepts only public video URLs. It does not accept local files or binary data.
Internet HTTP and HTTPS URLs up to 2048 characters in length are supported.
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: Reuse the instantiated client to improve detection performance and avoid repeated connections. * Common ways to get environment variables: * Get the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Get the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ client, _err := green.NewClientWithAccessKey( "cn-shanghai", "Get your RAM user's AccessKey ID from an environment variable", "Get your RAM user's AccessKey secret from an environment variable") if err != nil { fmt.Println(err.Error()) return } task := map[string]interface{}{"dataId": "Data ID", "url": "URL of the video to moderate"} // scenes: The detection scenarios. You can specify multiple scenarios. // The callback and seed parameters are optional and are used for webhook notifications. content, _ := json.Marshal( map[string]interface{}{ "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"}, "bizType": "Business scenario", "callback": "Webhook address", "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: Reuse the instantiated client to improve detection performance and avoid repeated connections. * Common ways to get environment variables: * Get the AccessKey ID of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Get the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ client, _err := green.NewClientWithAccessKey( "cn-shanghai", "Get your RAM user's AccessKey ID from an environment variable", "Get your RAM user's AccessKey secret from an environment variable") if err != nil { fmt.Println(err.Error()) return } // Set the url parameter to the streaming URL. task := map[string]interface{}{"dataId": "Data ID", "url": "URL of the video to moderate"} // scenes: The detection scenarios. You can specify multiple scenarios. // The callback and seed parameters are optional and are used for webhook notifications. content, _ := json.Marshal( map[string]interface{}{ "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"}, "live": "true", "bizType": "Business scenario", "callback": "Webhook address", "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: Reuse the instantiated client to improve detection performance and avoid repeated connections. * Common ways to get environment variables: * Get the AccessKey ID of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * Get the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ client, _err := green.NewClientWithAccessKey( "cn-shanghai", "Get your RAM user's AccessKey ID from an environment variable", "Get your RAM user's AccessKey secret from an environment variable") if err != nil { fmt.Println(err.Error()) return } // Set the url parameter to the streaming URL. task := map[string]interface{}{"dataId": "Data ID", "url": "URL of the video to moderate"} // scenes: The detection scenarios. You can specify multiple scenarios. // The callback and seed parameters are optional and are used for webhook notifications. content, _ := json.Marshal( map[string]interface{}{ "tasks": [...]map[string]interface{}{task}, "scenes": [...]string{"porn", "terrorism"}, "live": "true", "audioScenes": [...]string{"antispam"}, "bizType": "Business scenario", "callback": "Webhook address", "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: Reuse the instantiated client to improve detection performance and avoid repeated connections.
* Common ways to get environment variables:
* Get the AccessKey ID of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Get the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Get your RAM user's AccessKey ID from an environment variable",
"Get your RAM user's AccessKey secret from an environment variable")
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal(
[...]string{"Task 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())
}Video snapshot synchronization check
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: Reuse the instantiated client to improve detection performance and avoid repeated connections.
* Common ways to get environment variables:
* Get the AccessKey ID of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Get the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Get your RAM user's AccessKey ID from an environment variable",
"Get your RAM user's AccessKey secret from an environment variable")
if err != nil {
fmt.Println(err.Error())
return
}
frame1 := map[string]interface{}{
"offset": "0", "url": "URL of your video snapshot 1",
}
frame2 := map[string]interface{}{
"offset": "5", "url": "URL of your video snapshot 2",
}
frame3 := map[string]interface{}{
"offset": "10", "url": "URL of your video snapshot 3",
}
// frames: The snapshot information.
task := map[string]interface{}{"dataId": "Data ID", "frames": [...]map[string]interface{}{frame1, frame2, frame3}}
// scenes: The detection scenarios. You can specify multiple 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 Detection Result Feedback.
Interface | 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: Reuse the instantiated client to improve detection performance and avoid repeated connections.
* Common ways to get environment variables:
* Get the AccessKey ID of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Get the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Get your RAM user's AccessKey ID from an environment variable",
"Get your RAM user's AccessKey secret from an environment variable")
if _err != nil {
fmt.Println(err.Error())
return
}
frame1 := map[string]interface{}{"url": "URL of the moderated video snapshot 1", "offset": "UNIX timestamp for offset 1"}
frame2 := map[string]interface{}{"url": "URL of the moderated video snapshot 2", "offset": "UNIX timestamp for offset 2"}
frames := [...]map[string]interface{}{frame1, frame2}
// scenes: The detection scenarios. You can specify multiple scenarios.
// suggestion: The expected detection result. pass: The content is normal. block: The content contains violations.
content, _ := json.Marshal(
map[string]interface{}{
"taskId": "Video moderation task ID", "dataId": "Data ID", "url": "Video URL", "frames": frames,
"suggestion": "block", "scenes": [...]string{"ad", "terrorism"}, "note": "Notes",
},
)
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())
}