Updated on 2024-12-05 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:
    using System;
    using System.Collections.Generic;
    using HuaweiCloud.SDK.Core;
    using HuaweiCloud.SDK.Core.Auth;
    using HuaweiCloud.SDK.Image;
    using HuaweiCloud.SDK.Image.V2;
    using HuaweiCloud.SDK.Image.V2.Model;
    using Newtonsoft.Json;
    
    namespace RunImageTaggingSolution
    {
        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 accessKey= Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK", EnvironmentVariableTarget.Machine);
            string secretKey= Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK", EnvironmentVariableTarget.Machine);
    
                var config = HttpConfig.GetDefaultConfig();
                config.IgnoreSslVerification = true;
                var auth = new BasicCredentials(ak, sk);
    
                var client = ImageClient.NewBuilder()
                        .WithCredential(auth)
                        .WithRegion(ImageRegion.ValueOf("cn-north-1"))  //Replace cn-north-1 with the region where you enable the service. For details, see Regions and Endpoints.
                        .WithHttpConfig(config)
                        .Build();
    
                var req = new RunImageTaggingRequest
                {
                };
                req.Body = new ImageTaggingReq()
                {
                    Limit = 50,
                    Threshold = 95,
                    Language = "zh",
                    Url = "https://XXX.jpg"    //Replace it with an image URL that can be accessed from the Internet.
                };
    
                try
                {
                    var resp = client.RunImageTagging(req);
                    var respStatusCode = resp.HttpStatusCode;
                    Console.WriteLine(respStatusCode);
                    Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
                }
                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. Execute the sample code. If 200 is displayed on the console, the code is successfully executed. For details about related parameters, see Image Tagging APIs.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    200
    { 
         "result": { 
             "tags": [ 
                 { 
                     "confidence": "98.01",  
                    "type": "Tree",
                    "tag": "Tree", 
                     "i18n_tag": { 
                        "zh": "Chinese character for tree",
                         "en": "Tree" 
                     },  
                     "i18n_type": { 
                        "zh": "Chinese character for tree",
                         "en": "Tree" 
                     },  
                     "instances": [ ] 
                 }  
             ] 
         } 
     }