SDK API Example

SDK API Example for V1

  1. Construct a service client.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ak = "ak"
    sk = "sk"
    project_id = "projectId"
    end_point = "https://face.cn-north-4.myhuaweicloud.com"
    proxy = {"http": "http://127.0.0.1:1234",
    "https": "http://127.0.0.1:1234"}
    auth_info = AuthInfo(ak=ak, sk=sk, end_point=end_point)
    frs_client = FrsClient(auth_info=auth_info, project_id=project_id)
    # frs_client = FrsClient(auth_info=auth_info, project_id=project_id, proxies=proxy) #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
    fss = frs_client.get_face_set_service()
    external_fields = {"timestamp": {"type": "long"}, "id": {"type": "string"}, "number": {"type": "integer"}}
    ret = fss.create_face_set("faceSetName", 10000, external_fields)
    
  3. Add faces.
    1
    2
    3
    fs = frsClient.get_face_service()
    external_fields = {"timestamp": 12,"id": "home"}
    res = fs.add_face_by_obs_url("faceSetName", "/obs/image.jpg", "externalImageId", external_fields)
    
  4. Detect faces.
    1
    2
    ds = frs.get_detect_service()
    res = ds.detect_face_by_file("imagePath", "0,1,2,3")
    

SDK API Example for V2

  1. Construct a service client.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ak = "ak"
    sk = "sk"
    project_id = "projectId"
    end_point = "https://face.cn-north-4.myhuaweicloud.com"
    proxy = {"http": "http://127.0.0.1:1234",
    "https": "http://127.0.0.1:1234"}
    auth_info = AuthInfo(ak=ak, sk=sk, end_point=end_point)
    frs_client = FrsClient(auth_info=auth_info, project_id=project_id)
    # frs_client = FrsClient(auth_info=auth_info, project_id=project_id, proxies=proxy) #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
    fss = frs_client.get_v2().get_face_set_service()
    external_fields = {"timestamp":{"type" : "long"},"id":{"type":"string"},"number":{"type":"integer"}}
    ret = fss.create_face_set("faceSetName", 10000, external_fields)
    
  3. Add faces.
    1
    2
    3
    fs = frsClient.get_v2().get_face_service()
    external_fields = {"timestamp": 12,"id": "home"}
    res = fs.add_face_by_obs_url("faceSetName", "/obs/image.jpg", "externalImageId", external_fields)
    
  4. Detect faces.
    1
    2
    ds = frs.get_v2().get_detect_service()
    res = ds.detect_face_by_file("imagePath", "1,2")