4.6 人脸资源管理
4.6.5 批量删除人脸
} 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和face_id需要替换成实际信息。
String url = "https://{{endpoint}}/v2/{{project_id}}/face-sets/{{face_set_name}}/faces?
face_id={{face_id}}";
String token = "对应region的token";
String result = doDelete(url, token, client);
System.out.println(result);
} }
响应示例
状态码:200 成功响应样例
{ "face_number": 1, "face_set_id": "T785tx1N", "face_set_name": "showFaceSet"
}
状态码:400 失败响应样例
{ "error_code": "FRS.0402",
"error_msg": "External id is not exist, can not delete face"
}
4.6.5 批量删除人脸
功能介绍
自定义筛选条件,批量删除人脸库中的符合指定条件的多张人脸。
前提条件:
请确保您已开通人脸识别服务,具体操作方法请参见申请服务。
说明
application/json请求的body中,请使用标准Json格式。
调试
您可以在API Explorer中调试该接口。
URI
DELETE /v2/{project_id}/face-sets/{face_set_name}/faces/batch
表4-85 路径参数
参数 是否必选 参数类型 描述
project_id 是 String 项目ID,获取方法请参见获取项
目ID/账号名/AK/SK。
face_set_nam
e 是 String 人脸库名称。
请求参数
表4-86 请求 Header 参数
参数 是否必选 参数类型 描述
X-Auth-Token 是 String 用户Token。
Token认证就是在调用API的时 候将Token加到请求消息头,从 而通过身份认证,获得操作API 的权限,响应消息头中X-Subject-Token的值即为Token。
表4-87 请求 Body 参数
参数名 参数类型 是否必选 说明
filter String 是 过滤条件,具体参见filter语法。
响应参数
状态码:200
表4-88 响应 Body 参数
参数 参数类型 描述
face_number Integer 删除的人脸数量。 调用失败时无此字段。
face_set_id String 人脸库ID。 调用失败时无此字段。
face_set_nam
e String 人脸库名称。 调用失败时无此字段。
状态码: 400
表4-89 响应 Body 参数
参数 参数类型 描述
error_code String 调用失败时的错误码,具体请参考错误码。 调用 成功时无此字段。
error_msg String 调用失败时的错误信息。 调用成功时无此字段。
请求示例
● Python3语言请求代码示例(其他语言参照下列示例编写或使用FRS SDK)
# 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/batch".format(
endpoint=endpoint, project_id=project_id, face_set_name=face_set_name) body = {"filter" : "age:[20 TO 30]"}
response = requests.delete(url, headers=headers, json=body, verify=False) print(response.text)
● Java语言请求代码示例(其他语言参照下列示例编写或使用FRS SDK)
import com.huawei.trace.http.apache.httpclient.TraceApacheHttpClientBuilder;
import java.nio.charset.StandardCharsets;
* 使用前需已配置HttpClient jar包。jar包可通过下载SDK获取 */
public class DeleteFaceBath {
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;
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 doDelete(String url, String jsonStr, String token, CloseableHttpClient client) { HttpDeleteWithBody delete = new HttpDeleteWithBody(url);
delete.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
StringEntity entity = new StringEntity(jsonStr, ContentType.APPLICATION_JSON);
delete.setEntity(entity);
delete.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
//time unit is milliseconds RequestConfig requestConfig =
RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build();
delete.setConfig(requestConfig);
delete.setHeader("X-Auth-Token", token);
HttpResponse response = null;
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/batch";
String jsonStr = "{\"filter\" : \"age:[20 TO 30]\"}";
String token = "对应region的token";
String result = doDelete(url, jsonStr, token, client);
System.out.println(result);
} }
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { public static final String METHOD_NAME = "DELETE";
@Override
public String getMethod() { return METHOD_NAME;
}
public HttpDeleteWithBody(final String uri) { super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) { super();
setURI(uri);
}
public HttpDeleteWithBody() { super();
} }
响应示例
状态码:200 成功响应样例
{ "face_number": 1, "face_set_id": "T785tx1N", "face_set_name": "showFaceSet"
}
状态码:400 失败响应样例
{ "error_code": "FRS.0407",
"error_msg": "All the data not suitable, no data to be deleted."
}
状态码
状态码请参见状态码。
错误码
错误码请参见错误码。