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.

    package main
    
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        moderation "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3/region"
    )
    
    func main() {
       // 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.
       ak := os.Getenv("HUAWEICLOUD_SDK_AK")
       sk := os.Getenv("HUAWEICLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := moderation.NewModerationClient(
            moderation.ModerationClientBuilder().
                WithRegion(region.ValueOf("xxx")). //Replace xxx with the service region, for example, ap-southeast-3.
                WithCredential(auth).
                Build())
    
        request := &model.CheckImageModerationRequest{}
        var listCategoriesbody = []string{
            "porn",
        }
        urlImageDetectionReq:= "http://www.xxxx.xxxx"
        request.Body = &model.ImageDetectionReq{
            Url: &urlImageDetectionReq,
            Categories: listCategoriesbody,
            EventType: "head_image",
        }
        response, err := client.CheckImageModeration(request)
        if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
  2. The code is successfully executed if the image moderation result is displayed on the console.
    CheckImageModerationResponse 
    {"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"}}