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

Background information

You can customize image samples based on personalized content management requirements. 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 the topics in Manage custom image libraries.

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 versions of the dependencies described in the Installation topic. 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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            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 business 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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            // IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            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:
Notice 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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            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 are 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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            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);
            }
        }
    }
}

Delete 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)
        {
            // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            IClientProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                "Your AccessKey ID",
                "Your AccessKey secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteImageFromLibRequest deleteImageFromLibRequest = new DeleteImageFromLibRequest();
            deleteImageFromLibRequest.Ids=JsonConvert.SerializeObject (new List<int> { IDs of the images to be deleted });
            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);
            }
        }
    }
}