Updated on 2023-07-17 GMT+08:00

Image Moderation (Batch) (V2)

  1. This section describes how to use the Image Moderation SDK by AK/SK authentication.

    Replace the AK/SK in the sample code with the actual AK/SK. You can set Urls in ImageBatchModerationReq to configure the URLs of the images to be reviewed, and then execute the code.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    using System;
    using System.Collections.Generic;
    using HuaweiCloud.SDK.Core;
    using HuaweiCloud.SDK.Core.Auth;
    using HuaweiCloud.SDK.Moderation;
    using HuaweiCloud.SDK.Moderation.V2;
    using HuaweiCloud.SDK.Moderation.V2.Model;
    
    namespace RunImageBatchModerationSolution
    {
        class Program
        {
            static void Main(string[] args)
            {
           //Enter your AK/SK.
                const string ak = "<YOUR AK>";
                const string sk = "<YOUR SK>";
    
                var config = HttpConfig.GetDefaultConfig();
                config.IgnoreSslVerification = true;
                var auth = new BasicCredentials(ak, sk);
    
                var client = ModerationClient.NewBuilder()
                        .WithCredential(auth)
                        .WithRegion(ModerationRegion.ValueOf("xxx"))  //Replace xxx with the region where the service is located.
                        .WithHttpConfig(config)
                        .Build();
    
                var req = new RunImageBatchModerationRequest
                {
                };
                List<ImageBatchModerationReq.CategoriesEnum> listImageBatchModerationReqCategories = new List<ImageBatchModerationReq.CategoriesEnum>();
                listImageBatchModerationReqCategories.Add(ImageBatchModerationReq.CategoriesEnum.FromValue("porn"));
                List<String> listImageBatchModerationReqUrls = new List<String>();
                listImageBatchModerationReqUrls.Add("https://XXX.jpg");  //Replace it with an image URL that can be accessed from the Internet.
                req.Body = new ImageBatchModerationReq()
                {
                    Threshold = 0,
                    Categories = listImageBatchModerationReqCategories,
                    Urls = listImageBatchModerationReqUrls
                };
    
                try
                {
                    var resp = client.RunImageBatchModeration(req);
                    var respStatusCode = resp.HttpStatusCode;
                    Console.WriteLine(respStatusCode);
    	        Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
                }
                catch (RequestTimeoutException requestTimeoutException)
                {
                    Console.WriteLine(requestTimeoutException.ErrorMessage);
                }
                catch (ServiceResponseException clientRequestException)
                {
                    Console.WriteLine(clientRequestException.HttpStatusCode);
                    Console.WriteLine(clientRequestException.ErrorCode);
                    Console.WriteLine(clientRequestException.ErrorMsg);
                }
                catch (ConnectionException connectionException)
                {
                    Console.WriteLine(connectionException.ErrorMessage);
                }
            }
        }
    }
    
  2. If 200 is displayed on the console, the code is successfully executed. The batch Image Moderation result is displayed on the console.
    200
    {'result': [{'category_suggestions': {
                                          'porn': 'pass',
                                          },
                 'detail': {'ad': [{'confidence': 0.0, 'label': 'ad'},
                                   {'confidence': 1.0, 'label': 'normal'}],
                            'porn': [{'confidence': 0.9883, 'label': 'normal'},
                                     {'confidence': 0.0039, 'label': 'porn'},
                                     {'confidence': 0.0078, 'label': 'sexy'}],
                 'suggestion': 'pass',
                 'url': 'https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/terrorism.jpg'}]}