Updated on 2024-03-05 GMT+08:00

SDK API Example

SDK API Example

  1. Construct a service client.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    String ak = System.getenv("HUAWEICLOUD_SDK_AK");
    String sk = System.getenv("HUAWEICLOUD_SDK_SK");
    String projectId = "projectId";
    //Proxy information. If you need to use a proxy to access the service, add the proxy information here.
    ProxyHostInfo proxyHostInfo = new ProxyHostInfo("proxy",
    8080,
    "UserName",
    "Password"
    );
    //Service info
    String url = "https://face.ap-southeast-1.myhuaweicloud.com";
    String region = "ap-southeast-1";
    AuthInfo authInfo = new AuthInfo(url, region, ak, sk);
    FrsClient frsClient = new FrsClient(authInfo, projectId);
    //FrsClient frsClient = new FrsClient(authInfo, projectId, proxyHostInfo);//If you need to use a proxy to access the service, add the proxy information to the third parameter.
    
  2. Create a face set.
    1
    2
    3
    CreateExternalFields createExternalFields = new CreateExternalFields();
    createExternalFields.addField("testInteger", FieldType.INTEGER);
    CreateFaceSetResult createFaceSetResult = frsClient.getV2().getFaceSetService().createFaceSet(faceSetName, faceSetCapacity, createExternalFields);
    
  3. Add faces.
    1
    2
    3
    4
    5
    6
    7
    String imageId = "image1";
    File image = new File("data/face.jpg");
    byte[] fileData = FileUtils.readFileToByteArray(image);
    String imageBase64 = Base64.encodeBase64String(fileData);
    AddExternalFields addExternalFields = new AddExternalFields();
    addExternalFields.addField("testInteger", 123);
    AddFaceResult addFaceResult = frsClient.getV2().getFaceService().addFaceByBase64(faceSetName, imageId, imageBase64, addExternalFields);
    
  4. Invoke the Face Detection service.
    1
    2
    3
    String obsUrl = "/bucket/face.jpg";
    String attr = "1,2";
    DetectFaceResult detectFaceResult = frsClient.getV2().getDetectService().detectFaceByObsUrl(obsUrl, attr);
    

    In the preceding example, ak and sk indicate the access keys. To obtain the AK/SK, log in to the My Credentials page, choose Access Keys, and click Add Access Key.

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