Help Center/ Image Recognition/ SDK Reference/ Using Java SDK/ Demo Project of Image Tagging
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 the withUrl or withImage method of the ImageTaggingReq class to configure image information (either the image or url parameter). 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:
    package com.huaweicloud.sdk.test;
     
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.image.v2.region.ImageRegion;
    import com.huaweicloud.sdk.image.v2.*;
    import com.huaweicloud.sdk.image.v2.model.*;
                               
    public class RunImageTaggingSolution {
     
        public 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 ak = System.getenv("HUAWEICLOUD_SDK_AK");
            String sk = System.getenv("HUAWEICLOUD_SDK_SK");
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
            ImageClient client = ImageClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(ImageRegion.valueOf("XXXX"))   //Replace XXXX with the region where you enable the service. For details, see Regions and Endpoints.
                    .build();
            RunImageTaggingRequest request = new RunImageTaggingRequest();
            ImageTaggingReq body = new ImageTaggingReq();
            body.withLimit(50);
            body.withThreshold(95f);
            body.withLanguage("zh");
            body.withUrl("https://XXX.jpg");  //Replace it with an image URL that can be accessed from the Internet.
            request.withBody(body);
            try {
                RunImageTaggingResponse response = client.runImageTagging(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
  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.
    class RunImageTaggingResponse {
        result: class ImageTaggingResponseResult {
            tags: [class ImageTaggingItemBody {
                confidence: 98.00
                type: Tree
                tag: Tree
                i18nTag: class ImageTaggingItemBodyI18nTag {
                    zh: Chinese characters for tree
                    en: Tree
                }
                i18nType: class ImageTaggingItemBodyI18nType {
                    zh: Chinese characters for tree
                    en: Tree
                }
                instances: []
            }]
        }
    }
    
    Process finished with exit code 0