This topic describes how to use Content Moderation SDK for .NET to moderate videos for risky content.
Description
The video moderation API supports two detection modes: 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.
This SDK supports only public video URLs. It does not support local files or binary data.
The maximum length for a public HTTP or HTTPS URL is 2048 characters.
Prerequisites
The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.
You must use the required .NET 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 an online video for video moderation
using System; using Newtonsoft.Json; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Green.Model.V20180509; using System.Collections.Generic; namespace csharp_sdk_sample { class Program { static void Main(string[] args) { /** * Common ways to get environment variables: * Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ DefaultProfile profile = DefaultProfile.GetProfile( "cn-shanghai", "Get the AccessKey ID of the RAM user from an environment variable", "Get the AccessKey secret of the RAM user from an environment variable"); // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated connections. DefaultAcsClient client = new DefaultAcsClient(profile); VideoAsyncScanRequest request = new VideoAsyncScanRequest(); request.AcceptFormat = FormatType.JSON; request.ContentType = FormatType.JSON; request.Method = MethodType.POST; request.Encoding = "UTF-8"; Dictionary<string, object> task1 = new Dictionary<string, object>(); task1.Add("dataId", "Data ID for detection"); task1.Add("url", "URL of the video to be detected"); // scenes: The detection scenarios. You can specify multiple scenarios. // callback and seed are optional parameters used for callback notifications. Dictionary<string, object> httpBody = new Dictionary<string, object>(); httpBody.Add("scenes", new List<string> { "porn", "terrorism" }); httpBody.Add("bizType", "Business scenario"); httpBody.Add("callback", "Webhook address"); httpBody.Add("seed", "Random string"); httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 }); request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON); try { VideoAsyncScanResponse response = client.GetAcsResponse(request); if (response.HttpResponse.Status != 200) { Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status); } Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content)); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } } } }Submit a video live stream for video moderation
using System; using Newtonsoft.Json; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Green.Model.V20180509; using System.Collections.Generic; namespace csharp_sdk_sample { class Program { static void Main(string[] args) { /** * Common ways to get environment variables: * Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ DefaultProfile profile = DefaultProfile.GetProfile( "cn-shanghai", "Get the AccessKey ID of the RAM user from an environment variable", "Get the AccessKey secret of the RAM user from an environment variable"); // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated connections. DefaultAcsClient client = new DefaultAcsClient(profile); VideoAsyncScanRequest request = new VideoAsyncScanRequest(); request.AcceptFormat = FormatType.JSON; request.ContentType = FormatType.JSON; request.Method = MethodType.POST; request.Encoding = "UTF-8"; Dictionary<string, object> task1 = new Dictionary<string, object>(); task1.Add("dataId", "Data ID for detection"); // Set url to the streaming URL. task1.Add("url", "URL of the video to be detected"); // scenes: The detection scenarios. You can specify multiple scenarios. // callback and seed are optional parameters used for callback notifications. Dictionary<string, object> httpBody = new Dictionary<string, object>(); httpBody.Add("scenes", new List<string> { "porn", "terrorism" }); httpBody.Add("live", "true"); httpBody.Add("bizType", "Business scenario"); httpBody.Add("callback", "Webhook address"); httpBody.Add("seed", "Random string"); httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 }); request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON); try { VideoAsyncScanResponse response = client.GetAcsResponse(request); if (response.HttpResponse.Status != 200) { Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status); } Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content)); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } } } }Submitting video and audio for comprehensive detection
using System; using Newtonsoft.Json; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Green.Model.V20180509; using System.Collections.Generic; namespace csharp_sdk_sample { class Program { static void Main(string[] args) { /** * Common ways to get environment variables: * Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"); * Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ DefaultProfile profile = DefaultProfile.GetProfile( "cn-shanghai", "Get the AccessKey ID of the RAM user from an environment variable", "Get the AccessKey secret of the RAM user from an environment variable"); // Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated connections. DefaultAcsClient client = new DefaultAcsClient(profile); VideoAsyncScanRequest request = new VideoAsyncScanRequest(); request.AcceptFormat = FormatType.JSON; request.ContentType = FormatType.JSON; request.Method = MethodType.POST; request.Encoding = "UTF-8"; Dictionary<string, object> task1 = new Dictionary<string, object>(); task1.Add("dataId", "Data ID for detection"); // Set url to the streaming URL. task1.Add("url", "URL of the video to be detected"); // scenes: The detection scenarios. You can specify multiple scenarios. // callback and seed are optional parameters used for callback notifications. Dictionary<string, object> httpBody = new Dictionary<string, object>(); httpBody.Add("scenes", new List<string> { "porn", "terrorism" }); httpBody.Add("live", "true"); httpBody.Add("audioScenes", new List<string> { "antispam" }); httpBody.Add("bizType", "Business scenario"); httpBody.Add("callback", "Webhook address"); httpBody.Add("seed", "Random string"); httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 }); request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON); try { VideoAsyncScanResponse response = client.GetAcsResponse(request); if (response.HttpResponse.Status != 200) { Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status); } Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content)); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } } } }
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:
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* Common ways to get environment variables:
* Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Get the AccessKey ID of the RAM user from an environment variable",
"Get the AccessKey secret of the RAM user from an environment variable");
// Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
VideoAsyncScanResultsRequest request = new VideoAsyncScanResultsRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
List<string> taskIdList = new List<string> { "Asynchronous video detection task ID" };
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(taskIdList)), "utf-8", FormatType.JSON);
try
{
VideoAsyncScanResultsResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}Video snapshot synchronization detection
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.
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* Common ways to get environment variables:
* Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Get the AccessKey ID of the RAM user from an environment variable",
"Get the AccessKey secret of the RAM user from an environment variable");
// Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
VideoSyncScanRequest request = new VideoSyncScanRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
Dictionary<string, object> frame1 = new Dictionary<string, object>();
frame1.Add("offset", "0");
frame1.Add("url", "URL of your video snapshot 1");
Dictionary<string, object> frame2 = new Dictionary<string, object>();
frame2.Add("offset", "5");
frame2.Add("url", "URL of your video snapshot 2");
Dictionary<string, object> frame3 = new Dictionary<string, object>();
frame3.Add("offset", "10");
frame3.Add("url", "URL of your video snapshot 3");
Dictionary<string, object> task1 = new Dictionary<string, object>();
task1.Add("dataId", "Data ID for detection");
task1.Add("frames", new List<Dictionary<string, object>> { frame1, frame2, frame3 });
Dictionary<string, object> httpBody = new Dictionary<string, object>();
// scenes: The detection scenarios. You can specify multiple scenarios.
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("bizType", "Business scenario");
httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
VideoSyncScanResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}Feedback on video detection 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 | You can submit feedback from your manual review to correct the algorithm's video detection results. |
|
Sample code:
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
/**
* Common ways to get environment variables:
* Get the AccessKey ID of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
"Get the AccessKey ID of the RAM user from an environment variable",
"Get the AccessKey secret of the RAM user from an environment variable");
// Note: Reuse the instantiated client as much as possible to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
VideoFeedbackRequest request = new VideoFeedbackRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
Dictionary<string, object> frame1 = new Dictionary<string, object>();
frame1.Add("offset", "0");
frame1.Add("url", "URL of your video snapshot 1");
Dictionary<string, object> frame2 = new Dictionary<string, object>();
frame2.Add("offset", "0");
frame2.Add("url", "URL of your video snapshot 2");
List<Dictionary<string, object>> frames = new List<Dictionary<string, object>> { frame1, frame2 };
// scenes: The detection scenarios. You can specify multiple scenarios.
// suggestion: The expected detection result. pass: normal. block: violation.
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("suggestion", "block");
httpBody.Add("taskId", "Video moderation task ID");
httpBody.Add("dataId", "Data ID for detection");
httpBody.Add("url", "Video URL");
httpBody.Add("frames", frames);
httpBody.Add("note", "Remarks");
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
VideoFeedbackResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
}
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}