Facial Image Library Create
Function
This API is used to create a facial image library for storing facial features. You can create a maximum of 10 facial image libraries. Each library can contain a maximum of 100,000 facial features. If you need higher specifications, contact customer service.
Prerequisites:
- Ensure that you have enabled FRS. For detailed operations, see Applying for FRS.
Use standard JSON format in the body of the application/json request.
URI
POST /v2/{project_id}/face-sets
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| project_id | Yes | String | Project ID. For details about how to obtain the project ID, see Obtaining the Project ID, Account Name, and AK/SK. |
Request Message
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| X-Auth-Token | Yes | String | User token. During API authentication using a token, the token is added to requests to obtain permissions for calling the API. The value of X-Subject-Token in the response header is the obtained token. |
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| face_set_name | String | Yes | Name of the facial image library. Do not start the name of a facial image library with an underscore (_). Otherwise, the Cloud Eye service cannot collect the number of faces. |
| face_set_capacity | Integer | No | Maximum capacity of a facial image library. The value is an integer multiple of 10,000, for example, 30,000. The default value is 100000 and the maximum value is also 100000. You can create a new facial image library to expand the capacity. Each user can use 10 facial image libraries. Each library can house a maximum of 100,000 facial features. If you want to expand the capacity of a facial image library, contact HUAWEI CLOUD customer service to confirm the expansion scale and price. |
| external_fields | JSON | No | Custom data. Custom fields cannot be named vector, bounding_box, external_image_id, face_id, create_time, _id, _all or _source, which are built-in fields. The uniqueness of JSON character strings is not verified. The key value of a custom field contains 1 to 36 characters, and the value of the string type contains 1 to 256 characters. For details, see Customized Fields. If you want to use custom fields to add faces to the face library in subsequent operations, you need to define this field when creating the face library. |
Response Message
Status code: 200
| Parameter | Type | Description |
|---|---|---|
| face_set_info | Object | Facial image library information. For details, see FaceSetInfo. This parameter is not included when the API fails to be called. |
| error_code | String | Error code returned after the API fails to be called. For details, see Error Codes. This parameter is not included when the API is successfully called. |
| error_msg | String | Error message returned after the API fails to be called. This parameter is not included when the API is successfully called. |
Example Requests
For details about how to obtain the value of X-Auth-Token, see Authentication.
- Example request
POST https://{endpoint}/v2/{project_id}/face-sets Request Header: Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDT... Request Body: { "face_set_name": "test", "face_set_capacity": 100000, "external_fields" : { "timestamp" : { "type" : "long" }, "id" : { "type" : "string" }, "number" : { "type" : "integer" } } } - Sample code for a Python 3 request (For codes in other languages, refer to the following sample or use the FRS SDK.)
# -*- coding:utf-8 -*- import requests import base64 endpoint = 'Endpoint of the region where the service is enabled' project_id = 'Project ID of the region where the service is enabled' token = 'Actual token value obtained by the user' headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} url = "https://{endpoint}/v2/{project_id}/face-sets".format(endpoint=endpoint,project_id=project_id) body = { "face_set_name": "test", "face_set_capacity": 100000, "external_fields" : { "timestamp" : { "type" : "long" }, "id" : { "type" : "string" }, "number" : { "type" : "integer" } } } response = requests.post(url, headers=headers, json=body, verify=False) print(response.text)
- Sample code for a Java request (For codes in other languages, refer to the following sample or use the FRS SDK.)
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.HttpPost; 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; /** * This demo is used only for tests. You are advised to use the SDK. * Before using this demo, configure the HttpClient JAR package. Obtain this package by downloading the SDK. */ public class CreateFaceSet { 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 doPost(String url, String jsonStr, String token, CloseableHttpClient client) { HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity(jsonStr, ContentType.APPLICATION_JSON); post.setEntity(entity); post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); //time unit is milliseconds RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build(); post.setConfig(requestConfig); post.setHeader("X-Auth-Token", token); HttpResponse response = null; String result = ""; try { response = client.execute(post); 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(); // Replace endpoint and project_id with the actual ones. String url = "https://{{endpoint}}/v2/{{project_id}}/face-sets"; String jsonStr = "{ \"face_set_name\": \"test\", \"face_set_capacity\": 100000, \"external_fields\" : { \"timestamp\" : { \"type\" : \"long\" }, \"id\" : { \"type\" : \"string\" }, \"number\" : { \"type\" : \"integer\" }}}"; String token = "Token of the corresponding region"; String result = doPost(url, jsonStr, token, client); System.out.println(result); } }
Example Responses
Status code: 200
{
"face_set_info": {
"face_number": 0,
"face_set_id": "WYYOFIGb",
"face_set_name": "test",
"create_date": "2018-05-28 02:19:00",
"face_set_capacity": 10000,
"external_fields": {
"timestamp" : {
"type" : "long"
},
"id" : {
"type" : "string"
},
"number" : {
"type" : "integer"
}
}
}
} Status code: 400
{
"error_code": "FRS.0002",
"error_msg": "The authentication token is abnormal."
} Status Code
For details about the status code, see Status Codes.
Error Code
For details about the error code, see Error Codes.
Last Article: Facial Image Library Management
Next Article: Query of All Facial Image Libraries
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.