Help Center> Image Recognition> SDK Reference> Using Java SDK> Demo Project of Image Tagging
Updated on 2022-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) {
        //Enter your AK/SK.
            String ak = "<YOUR AK>"; 
            String sk = "<YOUR SK>";
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
            ImageClient client = ImageClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(ImageRegion.valueOf("cn-north-4"))
                    .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.
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
    SLF4J: Defaulting to no-operation (NOP) logger implementation 
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 
    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