Help Center/ Face Recognition Service/ Getting Started/ Using an SDK for Face Detection
Updated on 2024-09-30 GMT+08:00

Using an SDK for Face Detection

FRS SDKs provide a range of RESTful APIs to simplify development.

This section provides an example of how to use the Java SDK to call the Face Detection API. You can directly call APIs to use SDK functions.

To call an FRS API in an SDK, there are four steps:

Step 1: Subscribe to the Subservice

Step 2: Configure the Environment

Step 3: Modify the Configuration

Step 4: Call the Service API

Preparations

You have registered an account with HUAWEI CLOUD. Your account is not in arrears or frozen.

Step 1: Subscribe to the Subservice

  1. Log in to the FRS console.
  2. Click Authorization in the navigation pane on the left. Click Agree on the displayed page to authorize FRS to access data stored on OBS.
  3. On the displayed Subscription page, select and subscribe to your desired APIs.

    In this example, subscribe to the Face Detection API.

Step 2: Configure the Environment

  1. Download the FRS Java SDK.

    In the huaweicloud-sdk-java-frs directory, choose Code > Download ZIP to download frs-sdk-demo.

  2. Prepare the Java development environment.
  3. Import the FRS Java SDK into a project.
    1. Copy the downloaded frs-sdk-demo file to the Eclipse project folder.
    2. Open the project in Eclipse, right-click the project, and choose Properties.
    3. In the displayed dialog box, click Java Build Path. In the Libraries tab, click Add JARs to add the JAR file in the frs-sdk-demo folder.

Step 3: Modify the Configuration

In this demo, the AK/SK is used for authentication.

  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. Use the AK/SK for authentication.

    Change the values of AK and SK of the Main function of frs-sdk-demo to the obtained AK/SK.

    Figure 1 Configuring the AK/SK
  3. Modify the frs-sdk-demo configuration file.
    Figure 2 frs-sdk-demo file
    Table 1 Parameters in the Main function

    Parameter

    Description

    Value

    ak

    Access key ID

    Log in to the My Credentials page and choose Access Keys > Add Access Key to obtain it.

    sk

    Secret access key)

    Log in to the My Credentials page and choose Access Keys > Add Access Key to obtain it.

    endpoint

    Endpoint

    Select the endpoint of the region where the service is enabled.

    region

    Region where the service locates

    Select the region where the service is enabled.

    projectId

    Project ID

    Project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

    • The demo contains example calls of all FRS APIs. If you need the Face Detection API only, comment out or delete other APIs in the Main.java file.
    • Change the image path in detectFaceByObsUrl to the OBS bucket path of the image.

Step 4: Send API Calling Requests

Execute the Main.java file. If status code 200 is displayed on the IDE console, the program is successfully executed.

The face detection result is returned in JSON format.

{
   "faces": [
     {
       "bounding_box": {
         "width": 174,
         "top_left_y": 37,
         "top_left_x": 22,
         "height": 174
       }
     }
   ] 
}