All Products
Search
Document Center

Content Moderation:Image moderation

Last Updated:Aug 07, 2023

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

Description

Content Moderation SDK for .NET 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 the related parameters, see /green/image/scan.

  • 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.

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

Prerequisites

The dependencies for Content Moderation SDK for .NET are installed. For more information about how to install the dependencies, see Installation.

Note

You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

(Recommended) Submit synchronous image moderation tasks

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

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 your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "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");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client 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", "ID of the image to be moderated");
            task1.Add("url", "URL of the image to be moderated");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes: the moderation scenarios. You can specify one or more moderation 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.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

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 your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "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");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client 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", "ID of the image to be moderated");
            task1.Add("url", "URL of the image to be moderated");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes: the moderation scenarios. You can specify one or more moderation 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.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

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 your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "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");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client 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 moderation 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 /green/image/feedback.

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

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 your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     Obtain the AccessKey secret of your RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "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");
            // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client 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 moderation scenarios. You can specify one or more moderation scenarios. 
            // suggestion: the moderation results 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. 
            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 moderated 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);
            }
        }
    }
}