Help Center/ Image Recognition/ Getting Started/ Using the SDK to Call Image Recognition
Updated on 2022-03-08 GMT+08:00

Using the SDK to Call Image Recognition

This section provides an example of how to use a Java SDK to call Image Recognition. You can directly call APIs to use SDK functions. The procedure is as follows:

Step 1: Subscribing to a Service. Subscribe to your desired service on the Service List or Service Management page.

(Optional) Step 2: Enabling OBS Authorization. To use the data stored on OBS for Image Recognition, enable OBS authorization.

Step 3: Configuring the Environment. Obtain the SDK and sample project and import them to the development environment.

Step 4: Modifying the Configuration. Use AK/SK-based authentication.

Step 5: Calling a Service API. Call the service API. You can view the status code and error code at any time during the usage.

Step 1: Subscribing to a Service

To subscribe to Image Recognition, perform the following steps:

  1. Log in to HUAWEI CLOUD, apply for a HUAWEI CLOUD account, and complete real-name authentication.
  2. On the top of the HUAWEI CLOUD page, choose Products > AI > Image Recognition.
  3. On the product page, click Try Now and enter the username and password to access the Image Recognition management console.
  4. Subscribe to your desired service on the Service List or Service Management page.

    For example, to subscribe to Image Tagging on the Service Management page, select Image Tagging in the Commercial services area and click Subscribe.

    • You only need to subscribe to the service once.

  5. After being subscribed to, the service is displayed in My Services on the Service Management page. In this case, you can call related APIs to use the service.

(Optional) Step 2: Enabling OBS Authorization

If you want to use the data stored on OBS, enable OBS authorization. The procedure is as follows:

  1. The region and permissions of OBS must be consistent with those of Image Recognition.
  2. OBS authorization must be performed by an account, not by an IAM user.
  1. Log in to the management console and choose Service List > Image Recognition. In the navigation pane, choose Service Management .
  2. Enable OBS Authorization.

Step 3: Configuring the Environment

You can compile code to call Image Recognition APIs using the Image Recognition SDK. You need to configure the environment when using the SDK and calling APIs. The procedure is as follows:

  1. Obtain the Image Recognition SDK software packages and documents.
  2. For details about how to configure the environment, see Preparing a Java Development Environment.
  3. Import an SDK project on Eclipse.

    1. Open Eclipse and configure the correct JRE path in Windows > Preferences > Java > Installed JREs.
    2. Right-click Package Explorer on the left, and click Import. Choose Maven > Existing Maven Projects, and click Next. Then click Browse, and select the path where ais-image-java-sdk resides.
    3. Click Finish to import the SDK. After the SDK is imported, open the project. Figure 1 shows the project directory.
      Figure 1 Project directory

Step 4: Modifying the Configuration

Image Recognition supports token-based and AK/SK-based authentication. This section uses AK/SK-based authentication as an example.

  1. Obtain an AK/SK.

    The AK/SK is the access key. To obtain the AK/SK, log in to the My Credentials page, choose Access Keys in the left navigation pane, and click Create Access Key in the right pane.

  2. For AK/SK-based authentication, configure the AK/SK in the Java SDK.

    Change the values of AK and SK of the function in the ImageTaggingDemo.java file of the project to the obtained AK/SK. The sample code is as follows:
    // 1. Configure the basic information for accessing Image Tagging and generate a client connection object.
    AisAccess service = ServiceAccessBuilder.builder()		
            .ak("######")                       // your ak
            .sk("######")                       // your sk
            .region("ap-southeast-1")           // Configuration of Image Recognition in the CN-Hong Kong region
            .connectionTimeout(5000)            // Timeout limit for connecting to the target URL
            .connectionRequestTimeout(1000)     // Timeout limit for obtaining available connections from the connection pool
            .socketTimeout(20000)               // Timeout limit for obtaining server response data
            .build();

  3. Select a local image or use the default image of the sample project, and modify the image path (data/image-tagging-demo-1.jpg) in the ImageTaggingDemo.java file.

    //
    // 2. Construct the parameters required for accessing Image Tagging.
    //
    String uri = "/v1.0/image/tagging";
    byte[] fileData = FileUtils.readFileToByteArray(new File("data/image-tagging-demo-1.jpg"));
    String fileBase64Str = Base64.encodeBase64String(fileData);

Step 5: Calling a Service API

  1. Execute the ImageTaggingDemo.java file. If 200 is displayed on the console, the program is successfully executed. The recognization result is shown in Figure 2.

    Figure 2 Execution result

  2. View the number of API calls. To view the total number of API calls, go to the Image Tagging page under Service List, or click View Metric to go to the Cloud Eye management console, as shown in Figure 3.

    Figure 3 Viewing metrics