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

Image Moderation (Batch) (V2)

  1. This section describes how to use the Image Moderation (Batch) 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
    package main
    
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        moderation "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v2"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v2/region"
    )
    
    func main() {
           //Enter your AK/SK.
        ak := "<YOUR AK>"
        sk := "<YOUR SK>"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := moderation.NewModerationClient(
            moderation.ModerationClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
    
        request := &model.RunImageBatchModerationRequest{}
        var listCategoriesbody = []model.ImageBatchModerationReqCategories{
            model.GetImageBatchModerationReqCategoriesEnum().PORN,
        }
        var listUrlsbody = []string{
            "https://XXX.jpg",  //Replace it with an image URL that can be accessed from the Internet.
        }
        thresholdImageBatchModerationReq:= float64(0)
        request.Body = &model.ImageBatchModerationReq{
            Threshold: &thresholdImageBatchModerationReq,
            Categories: &listCategoriesbody,
            Urls: listUrlsbody,
        }
        response, err := client.RunImageBatchModeration(request)
        if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }}
    
  2. If the moderation result is displayed on the console, the code is successfully executed.
    RunImageBatchModerationResponse {"result":[{"url":"https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/terrorism.jpg","suggestion":"pass","detail":{"porn":[{"confidence":0.9883,"label":"normal"},{"confidence":0.0039,"label":"porn"},{"confidence":0.0078,"label":"sexy"}]},"category_suggestions":{
    ,"porn":"pass"}}]}
    Process exiting with code: 0