
# 更新人脸 - UpdateFace
#### 功能介绍
根据人脸ID（face_id）更新单张人脸信息。
**前提条件：**
请确保您已开通[人脸搜索服务](https://console.huaweicloud.com/frs/?locale=zh-cn#/frs/management/faceRecognized)。
![](https://support.huaweicloud.com/api-face/public_sys-resources/note_3.0-zh-cn.png)
application/json请求的body中，请使用标准Json格式。
#### 调试
您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=FRS&api=UpdateFace)中调试该接口，支持自动认证鉴权。API Explorer可以自动生成SDK代码示例，并提供SDK代码示例调试功能。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/usermanual-face/face_01_0011.html)。
#### URI
PUT /v2/{project_id}/face-sets/{face_set_name}/faces
表1路径参数 
| 参数            | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| project_id    | 是    | String | **参数解释：** 项目ID，获取方法请参见[获取项目ID/账号名/AK/SK](https://support.huaweicloud.com/api-face/face_02_0056.html)。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。 |
| face_set_name | 是    | String | **参数解释：** 人脸库名称。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。                                                                                 |
   
#### 请求参数
表2请求Header参数 
| 参数                    | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
|:---|:---|:---|:---|
| X-Auth-Token          | 是    | String | **参数解释：** 用户Token，用于获取操作API的权限。获取方法请参见[认证鉴权](https://support.huaweicloud.com/api-face/face_02_0004.html)。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| Content-Type          | 是    | String | **参数解释：** 发送的实体的MIME类型。 **约束限制：** 不涉及。 **取值范围：** 参数值为"application/json"。 **默认取值：** 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| Enterprise-Project-Id | 否    | String | **参数解释：** 企业项目ID。FRS支持通过企业项目管理（EPS）对不同用户组和用户的资源使用，进行分账，当前仅支持按需计费模式。 获取方法：进入"[企业项目管理](https://console.huaweicloud.com/eps/?#/eps/manager/projects)"页面，单击企业项目名称，在企业项目详情页获取Enterprise-Project-Id（企业项目ID）。 ![](https://support.huaweicloud.com/api-face/zh-cn_image_0000002603438819.png "点击放大") 说明： 创建企业项目后，在传参时，有以下三类场景。 - 携带正确的ID，正常使用FRS服务，账单的企业项目会被分类到企业ID对应的企业项目中。  - 携带格式正确但不存在的ID，正常使用FRS服务，账单的企业项目会显示对应不存在的企业项目ID。  - 不携带ID或格式错误ID（包含特殊字符等），正常使用FRS服务，账单的企业项目会被分类到"default"中。   **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。 |
   
表3请求Body参数 
| 参数名               | 参数类型   | 是否必选 | 说明                                                                                                                                                                                                             |
|:---|:---|:---|:---|
| face_id           | String | 是    | 人脸ID，由系统内部生成的唯一ID。                                                                                                                                                                                             |
| external_image_id | String | 否    | 用户指定的图片外部ID，与当前图像绑定。用户不设置该参数时，系统会自动生成一个。该ID长度范围为\[1,36\]，可以包含字母、数字、中划线或者下划线，不包含其他的特殊字符。 这里是待修改的参数，external_image_id和external_fields至少选一个。                                      |
| external_fields   | Object | 否    | 自定义字段的key值长度范围为\[1,36\]，string类型的value长度范围为\[1,256\]，具体参见[自定义字段](https://support.huaweicloud.com/api-face/face_02_0012.html)。 这里是待修改的参数，external_image_id和external_fields至少选一个。 |
   
#### 响应参数
**状态码： 200**
表4响应Body参数 
| 参数            | 参数类型    | 描述                  |
|:---|:---|:---|
| face_number   | Integer | 更新的人脸数量。 调用失败时无此字段。 |
| face_set_id   | String  | 人脸库ID。 调用失败时无此字段。   |
| face_set_name | String  | 人脸库名称。 调用失败时无此字段。   |
   
**状态码： 400**
表5响应Body参数 
| 参数         | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|
| error_code | String | **参数解释：** 调用失败时的错误码，具体请参考[错误码](https://support.huaweicloud.com/api-face/face_02_0045.html)。 调用成功时无此字段。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。 |
| error_msg  | String | **参数解释：** 调用失败时的错误信息。 调用成功时无此字段。 **约束限制：** 不涉及。 **取值范围：** 不涉及。 **默认取值：** 不涉及。                                                                         |
   
#### 请求示例
X-Auth-Token值获取方法请参见[快速入门](https://support.huaweicloud.com/qs-face/face_09_0003.html)。
- 更新人脸库中某一人脸的特征信息
  ```
  PUT https://{endpoint}/v2/{project_id}/face-sets/showFaceSet/faces
  Request Header:
  Content-Type: application/json
  X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDT...
  Request Body:
  {
    "face_id": "iexEBb6t", 
    "external_image_id": "imageID",
    "external_fields": {
       "timestamp": 12,
       "id": "300018629384756"
    }
  }
  ```
  
- 使用Python3语言更新人脸库中某一人脸的特征信息
  ```
  # -*- coding:utf-8 -*-
  import requests
  import base64
  endpoint = '开通服务所在region的人脸识别服务域名'
  project_id = '开通服务所在region的用户项目ID'
  token = '用户获取得到的实际token值'
  headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
  face_set_name = 'showFaceSet'
  url = "https://{endpoint}/v2/{project_id}/face-sets/{face_set_name}/faces".format(
      endpoint=endpoint, project_id=project_id, face_set_name=face_set_name)
  body = {
      "face_id": "iexEBb6t",
      "external_image_id": "imageID",
      "external_fields": {
          "timestamp": 12,
          "id": "300018629384756"
      }
  }
  response = requests.put(url, headers=headers, json=body, verify=False)
  print(response.text)
  ```
  

- 使用Java语言更新人脸库中某一人脸的特征信息
  ```
  import com.huawei.trace.http.apache.httpclient.TraceApacheHttpClientBuilder;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpHeaders;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.config.RequestConfig;
  import org.apache.http.client.methods.HttpPut;
  import org.apache.http.config.Registry;
  import org.apache.http.config.RegistryBuilder;
  import org.apache.http.conn.socket.ConnectionSocketFactory;
  import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  import org.apache.http.conn.ssl.NoopHostnameVerifier;
  import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  import org.apache.http.entity.ContentType;
  import org.apache.http.entity.StringEntity;
  import org.apache.http.impl.client.CloseableHttpClient;
  import org.apache.http.impl.client.HttpClientBuilder;
  import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  import org.apache.http.ssl.SSLContextBuilder;
  import org.apache.http.ssl.TrustStrategy;
  import org.apache.http.util.EntityUtils;
  import java.io.IOException;
  import java.nio.charset.StandardCharsets;
  import java.security.KeyManagementException;
  import java.security.KeyStoreException;
  import java.security.NoSuchAlgorithmException;
  import java.security.cert.CertificateException;
  import java.security.cert.X509Certificate;
  import java.util.concurrent.TimeUnit;
  import javax.net.ssl.HostnameVerifier;
  import javax.net.ssl.SSLContext;
  /**
   * 此demo仅供测试使用，强烈建议使用SDK
   * 使用前需已配置HttpClient jar包。jar包可通过下载SDK获取
   */
  public class UpdateFaceByFaceId {
      protected static HttpClientBuilder buildClient(HttpClientBuilder httpClientBuilder) {
          SSLContext sslContext = null;
          try {
              sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                  public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                      return true;
                  }
              }).build();
          } catch (NoSuchAlgorithmException e) {
              throw new RuntimeException(e.getMessage());
          } catch (KeyManagementException e) {
              throw new RuntimeException(e.getMessage());
          } catch (KeyStoreException e) {
              throw new RuntimeException(e.getMessage());
          }
          httpClientBuilder.setSSLContext(sslContext);
          httpClientBuilder.setConnectionTimeToLive(30, TimeUnit.SECONDS);
          HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
          SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
          Registry<ConnectionSocketFactory> socketFactoryRegistry
              = RegistryBuilder.<ConnectionSocketFactory>create().register("http",
              PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
          PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
          connMgr.setMaxTotal(200);
          connMgr.setDefaultMaxPerRoute(100);
          httpClientBuilder.setConnectionManager(connMgr);
          return httpClientBuilder;
      }
      public static String doPut(String url, String jsonStr, String token, CloseableHttpClient client) {
          HttpPut put = new HttpPut(url);
          put.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
          StringEntity entity = new StringEntity(jsonStr, ContentType.APPLICATION_JSON);
          put.setEntity(entity);
          put.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
          //time unit is milliseconds
          RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build();
          put.setConfig(requestConfig);
          put.setHeader("X-Auth-Token", token);
          HttpResponse response = null;
          String result = "";
          try {
              response = client.execute(put);
              HttpEntity responseBody = response.getEntity();
              if (responseBody == null) {
                  System.out.println("the response body is null.");
                  return result;
              } else {
                  byte[] byteStream = EntityUtils.toByteArray(responseBody);
                  System.arraycopy(byteStream, 0, new byte[byteStream.length], 0, byteStream.length);
                  result = new String(byteStream, StandardCharsets.UTF_8);
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
          return result;
      }
      public static void main(String[] args) {
          CloseableHttpClient client = buildClient(TraceApacheHttpClientBuilder.create()).build();
          // endpoint,project_id和face_set_name需要替换成实际信息。
          String url = "https://{{endpoint}}/v2/{{project_id}}/face-sets/{{face_set_name}}/faces";
          String token = "对应region的token";
          String jsonStr = "{ \"face_id\": \"iexEBb6t\", \"external_image_id\": \"imageID\", \"external_fields\": { \"timestamp\": 12, \"id\": \"300018629384756\" }}";
          String result = doPut(url, jsonStr,token, client);
          System.out.println(result);
      }
  }
  ```
  
 
#### 响应示例
**状态码：200**
成功响应样例
```
{
  "face_number": 1,
  "face_set_id": "T785tx1N",
  "face_set_name": "showFaceSet"
}
```
**状态码：400**
失败响应样例
```
{
  "error_code": "FRS.0303",
  "error_msg": "The face id is not exist, checkout your input."
}
```
#### 状态码
状态码请参见[状态码](https://support.huaweicloud.com/api-face/face_02_0082.html)。
#### 错误码
错误码请参见[错误码](https://support.huaweicloud.com/api-face/face_02_0045.html)。
