This topic describes how to use Content Moderation SDK for .NET to moderate images for risky content.
Description
Image Moderation supports synchronous and asynchronous detection.
Synchronous detection returns results in real time. For more information about the parameters, see Synchronous Detection API reference.
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 SDK supports public HTTP and HTTPS URLs with a maximum length of 2,048 characters.
Prerequisites
The .NET dependencies must be installed. For instructions, see Install .NET dependencies.
You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
(Recommended) Synchronous image detection
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
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 obtain environment variables:
* Obtain the AccessKey ID of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"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.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest request = new ImageSyncScanRequest();
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 image to be detected");
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
{
ImageSyncScanResponse 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 asynchronous image moderation tasks
This section describes how to use Content Moderation SDK for .NET 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 using 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. |
|
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 obtain environment variables:
* Obtain the AccessKey ID of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"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.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanRequest request = new ImageAsyncScanRequest();
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 image to be detected");
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
{
ImageAsyncScanResponse 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 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
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 obtain environment variables:
* Obtain the AccessKey ID of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"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.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanResultsRequest request = new ImageAsyncScanResultsRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
List<string> taskIdList = new List<string> { "ID of the asynchronous image detection task"};
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(taskIdList)), "utf-8", FormatType.JSON);
try
{
ImageAsyncScanResultsResponse 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);
}
}
}
}Provide feedback on image moderation results
If the results of image moderation do not meet your expectations, 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 Detection result feedback.
Interface | Description | Supported region |
ImageScanFeedbackRequest | You can submit feedback to manually correct the algorithm's image 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 obtain environment variables:
* Obtain the AccessKey ID of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.GetProfile(
"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.");
// Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
DefaultAcsClient client = new DefaultAcsClient(profile);
ImageScanFeedbackRequest request = new ImageScanFeedbackRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// 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("url", "URL of the image");
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
ImageScanFeedbackResponse 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);
}
}
}
}