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.

    Use Url or Image to configure the image information (Url is used in the sample code) and run the code.

    const core = require('@huaweicloud/huaweicloud-sdk-core');
    const moderation = require("@huaweicloud/huaweicloud-sdk-moderation");
    // 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, HUAWEICLOUD_SDK_SK, and HUAWEICLOUD_SDK_PROJECT_ID.
    const ak = process.env.HUAWEICLOUD_SDK_AK;
    const sk = process.env.HUAWEICLOUD_SDK_SK;
    const projectId = process.env.HUAWEICLOUD_SDK_PROJECT_ID;
    
    const endpoint = "https://moderation.ap-southeast-3.myhuaweicloud.com"; //Replace ap-southeast-3 with the region where the service is located.
    const credentials = new core.BasicCredentials()
                         .withAk(ak)
                         .withSk(sk)
                         .withProjectId(project_id)
    const client = moderation.v3.ModerationClient.newBuilder()
                                .withCredential(credentials)
                                .withEndpoint(endpoint)
                                .build();
    const request = new moderation.v3.CheckImageModerationRequest();
    const body = new moderation.v3.ImageDetectionReq();
    const listbodyCategories = new Array();
    listbodyCategories.push("porn");
    body.withUrl("http://www.xxxx.xxxx");
    body.withCategories(listbodyCategories);
    body.withEventType("head_image");
    request.withBody(body);
    const result = client.checkImageModeration(request);
    result.then(result => {
        console.log("JSON.stringify(result)::" + JSON.stringify(result));
    }).catch(ex => {
        console.log("exception:" + JSON.stringify(ex));
    });
  2. The code is successfully executed if the image moderation result is displayed on the console.
    JSON.stringify(result)::{"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"}}