Help Center> Image Recognition> SDK Reference> Using the Go SDK> Demo Project of Image Tagging
Updated on 2022-12-06 GMT+08:00

Demo Project of Image Tagging

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

  • Replace the AK/SK in the sample code with the actual AK/SK. You can use either the url or image parameter in the initialized ImageTaggingReq to configure image information. This section uses url as an example.
  • endpoint indicates the regions and endpoints for HUAWEI CLOUD services. For details, see Regions and Endpoints.
  1. The sample code for calling Image Tagging is as follows:
     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
    package main
    
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        image "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2/region"
    )
    
    func main() {
     //Enter your AK/SK.
        ak := "<YOUR AK>"
        sk := "<YOUR SK>"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := image.NewImageClient(
            image.ImageClientBuilder().
                WithRegion(region.ValueOf("cn-north-1")).
                WithCredential(auth).
                Build())
    
        request := &model.RunImageTaggingRequest{}
        limitImageTaggingReq:= int32(50)
        thresholdImageTaggingReq:= float32(95)
        languageImageTaggingReq:= "zh"
        urlImageTaggingReq:= "https://XXX.jpg"  //Replace it with an image URL that can be accessed from the Internet.
        request.Body = &model.ImageTaggingReq{
            Limit: &limitImageTaggingReq,
            Threshold: &thresholdImageTaggingReq,
            Language: &languageImageTaggingReq,
            Url: &urlImageTaggingReq,
        }
        response, err := client.RunImageTagging(request)
        if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }}
    
  2. If the recognition result is displayed on the console, the execution is successful. For details about related parameters, see Image Tagging APIs.
    1
    2
    RunImageTaggingResponse {"result": {"tags": [{"confidence": "98.01", "type": "Tree", "tag": "Tree", "i18n_tag": {"zh": "Chinese characters for tree", "en": "Tree"}, "i18n_type": {"zh": "Chinese characters for tree", "en": "Tree"}, "instances": []}]}} 
    Process exiting with code: 0