Updated on 2025-07-02 GMT+08:00

Image Moderation (V3)

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

    Change the value of Image or Url in ImageDetectionReq in the sample code to configure the image to be reviewed. Then run the code.

    using System;
    using System.Collections.Generic;
    using HuaweiCloud.SDK.Core;
    using HuaweiCloud.SDK.Core.Auth;
    using HuaweiCloud.SDK.Moderation;
    using HuaweiCloud.SDK.Moderation.V3;
    using HuaweiCloud.SDK.Moderation.V3.Model;
     
    namespace CheckImageModerationSolution
    {
        class Program
        {
            static void Main(string[] args)
            {
           //Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
          //In this example, the AK and SK are stored in environment variables for identity authentication. Before running this example, configure environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
            string ak= Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK", EnvironmentVariableTarget.Machine);
            string sk= Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK", EnvironmentVariableTarget.Machine);
    
    
                const string projectId = "project_id";
                var config = HttpConfig.GetDefaultConfig();
                config.IgnoreSslVerification = true;
                var auth = new BasicCredentials(ak, sk, projectId);
     
                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 CheckImageModerationRequest
                {
                };
                List<string> listCategoriesbody = new List<string>();
                listCategoriesbody.Add("porn");
                req.Body = new ImageDetectionReq()
                {
                    Url = "http://www.xxxx.xxxx",
                    Categories = listCategoriesbody,
                    EventType = "head_image"
                };
     
                try
                {
                    var resp = client.CheckImageModeration(req);
                    var respStatusCode = resp.HttpStatusCode;
                    Console.WriteLine(respStatusCode);
                      Console.WriteLine(resp);
                }
                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 image moderation result is displayed on the console.
    200
     {"request_id": "69b424920668cb15e6d25eef879a023b", "result": {"suggestion": "block", "category": "image_text", "details": [{"suggestion": "block", "category": "image_text", "confidence": 1.0, "qr_location": {"top_left_x": 74, "top_left_y": 66, "bottom_right_x": 151, "bottom_right_y": 142}, "qr_content": "http://xxxxxx/xxxxx", "label": "qr_code"}, {"suggestion": "block", "category": "image_text", "confidence": 0.98779297, "label": "ad"}, {"suggestion": "block", "category": "image_text", "confidence": 1.0, "label": "qr_code"}], "ocr_text": "xxxxxxxxx"}}