All Products
Search
Document Center

Content Moderation:Manage custom image libraries

Last Updated:Aug 07, 2023

This topic describes how to use Content Moderation SDK for .NET to manage custom image libraries.

Description

You can customize image samples based on your management requirements for personalized content. Then, you can use custom image libraries to intelligently detect pornographic content, terrorist content, and ad violations in images and videos. For more information about parameters, see CreateImageLib.

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.

Query custom image libraries

You can use the following code to query image libraries, including custom image libraries and feedback-based image libraries:

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DescribeImageLibRequest describeImageLibRequest = new DescribeImageLibRequest();
            describeImageLibRequest.ServiceModule = "open_api";
            try
            {
                // Query all image libraries, including custom image libraries and feedback-based image libraries. 
                DescribeImageLibResponse describeImageLibResponse = client.GetAcsResponse(describeImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(describeImageLibResponse.HttpResponse.Content));

                List<DescribeImageLibResponse.DescribeImageLib_ImageLib> allLibs = describeImageLibResponse.ImageLibList;
                List<DescribeImageLibResponse.DescribeImageLib_ImageLib> customImageLibs = new List<DescribeImageLibResponse.DescribeImageLib_ImageLib>();
                foreach (DescribeImageLibResponse.DescribeImageLib_ImageLib imageLib in allLibs)
                {
                    String source = imageLib.Source;
                    // List custom image libraries. 
                    if ("MANUAL".Equals(source))
                    {
                        customImageLibs.Add(imageLib);
                    }
                    // List feedback-based image libraries. 
                    if ("FEEDBACK".Equals(source))
                    {
                        customImageLibs.Add(imageLib);
                    }
                    Console.WriteLine(JsonConvert.SerializeObject(customImageLibs));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Create a custom image library

You can use the following code to create a custom image library:

Note

Set the parameters based on your business scenario.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            CreateImageLibRequest createImageLibRequest = new CreateImageLibRequest();
            createImageLibRequest.ServiceModule = "open_api";
            createImageLibRequest.Name = "Pornography detection blacklist";
            createImageLibRequest.Scene = "PORN";
            createImageLibRequest.Category = "BLACK";

            try
            {
                CreateImageLibResponse createImageLibResponse = client.GetAcsResponse(createImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(createImageLibResponse.HttpResponse.Content));

            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Modify a custom image library

You can use the following code to change the name of a custom image library and the moderation scenario to which the custom image library applies:

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            UpdateImageLibRequest updateImageLibRequest = new UpdateImageLibRequest();
            // The ID of the custom image library. 
            updateImageLibRequest.Id = ID of the custom image library;
            updateImageLibRequest.Name = "New name of the custom image library";
            updateImageLibRequest.BizTypes = JsonConvert.SerializeObject(new List<string> { "comment" });
            updateImageLibRequest.Category = "WHITE";
            updateImageLibRequest.Scene = "PORN";
            try
            {
                UpdateImageLibResponse updateImageLibResponse = client.GetAcsResponse(updateImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(updateImageLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Delete a custom image library

You can use the following code to delete a custom image library:

Important

If you delete a custom image library, all images in the library are also deleted.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteImageLibRequest deleteImageLibRequest = new DeleteImageLibRequest();
            // The ID of the custom image library. 
            deleteImageLibRequest.Id = ID of the custom image library;
            try
            {
                DeleteImageLibResponse deleteImageLibResponse = client.GetAcsResponse(deleteImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(deleteImageLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Query images in a custom image library

You can use the following code to query all images that have been added to a custom image library:

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DescribeImageFromLibRequest describeImageFromLibRequest = new DescribeImageFromLibRequest();
            describeImageFromLibRequest.ImageLibId = ID of the custom image library;
            describeImageFromLibRequest.PageSize = 20;
            describeImageFromLibRequest.CurrentPage = 1;
            try
            {
                DescribeImageFromLibResponse describeImageFromLibResponse = client.GetAcsResponse(describeImageFromLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(describeImageFromLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

Remove images from a custom image library

You can use the following code to remove multiple images from a custom image library:

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
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");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteImageFromLibRequest deleteImageFromLibRequest = new DeleteImageFromLibRequest();
            deleteImageFromLibRequest.Ids=JsonConvert.SerializeObject (new List<int> {IDs of the images to be removed});
            try
            {
                DeleteImageFromLibResponse deleteImageFromLibResponse = client.GetAcsResponse(deleteImageFromLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(deleteImageFromLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}