更新时间:2024-04-28 GMT+08:00
分享

创建人脸库

功能介绍

创建用于存储人脸特征的人脸库。您最多可以创建10个人脸库,每个人脸库最大容量为10万个人脸特征。如有更大规格的需求请联系客服。

前提条件:

请确保您已开通人脸搜索服务

  • 默认情况下,一个人脸库最大可支持10万个人脸特征,一个用户最多可创建10个人脸库,最多可支持10*10万(100万)个人脸特征。
  • 如您的需求超出100万个人脸特征,可通过工单或者服务热线( 4000-955-988或950808 转1)与我们联系,咨询具体解决方案。

调试

您可以在API Explorer中调试该接口,支持自动认证鉴权。API Explorer可以自动生成SDK代码示例,并提供SDK代码示例调试功能。

URI

POST /v2/{project_id}/face-sets

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,获取方法请参见获取项目ID/账号名/AK/SK

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户Token。

用于获取操作API的权限。获取方法请参见认证鉴权

Content-Type

String

发送的实体的MIME类型,参数值为“application/json”。

Enterprise-Project-Id

String

企业项目ID。FRS支持通过企业项目管理(EPS)对不同用户组和用户的资源使用,进行分账,当前仅支持按需计费模式。

获取方法:进入“企业项目管理”页面,单击企业项目名称,在企业项目详情页获取Enterprise-Project-Id(企业项目ID)。

说明:

创建企业项目后,在传参时,有以下三类场景。

  • 携带正确的ID,正常使用FRS服务,账单的企业项目会被分类到企业ID对应的企业项目中。
  • 携带格式正确但不存在的ID,正常使用FRS服务,账单的企业项目会显示对应不存在的企业项目ID。
  • 不携带ID或格式错误ID(包含特殊字符等),正常使用FRS服务,账单的企业项目会被分类到"default"中。
表3 请求Body参数

参数

是否必选

参数类型

描述

external_fields

Map<String,TypeInfo>

用户自定义数据,自定义字段不能以vector、bounding_box、external_image_id、face_id、create_time、_id、_all、_source等字段命名,这些字段为内置字段,自定义字段避免使用。

Json字符串不校验重复性,自定义字段的key值长度范围为[1,36],string类型的value长度范围为[1,256],具体参见自定义字段

如果在后续操作中,使用该自定义字段向人脸库中添加人脸,需要在创建人脸库时定义该字段。

face_set_name

String

人脸库的名称,字符串长度1-64。

建议人脸库的名称不要以下划线(_)开头,否则云监控服务会无法采集人脸数量。

face_set_capacity

Integer

人脸库最大的容量,填写1万整数倍的数字,例如:30000。

默认为100000,最大值为100000,可通过创建新的人脸库进行扩容,每个用户可使用10个人脸库,每个人脸库容量为10万个人脸特征。如需扩容单个人脸库规模,请联系华为云客服确认扩容规模与价格。

表4 TypeInfo

参数

是否必选

参数类型

描述

type

String

数据类型支持string、integer、double、long等。

响应参数

状态码:200

表5 响应Body参数

参数

参数类型

描述

face_set_info

FaceSetInfo object

人脸库信息,详见FaceSetInfo。 调用失败时无此字段。

表6 FaceSetInfo

参数

参数类型

描述

face_number

Integer

人脸库中已有的人脸特征的数量。

external_fields

Object

用户的自定义字段。

face_set_id

String

人脸库ID,随机生成的包含八个字符的字符串。

face_set_name

String

人脸库名称。

create_date

String

创建时间。

face_set_capacity

Integer

人脸库最大的容量。创建人脸库时,请求参数如果不设置face_set_capacity参数,默认每个人脸库最大容量为10万个人脸特征。

状态码: 400

表7 响应Body参数

参数

参数类型

描述

error_code

String

调用失败时的错误码,具体请参考错误码。 调用成功时无此字段。

error_msg

String

调用失败时的错误信息。 调用成功时无此字段。

请求示例

X-Auth-Token值获取方法请参见快速入门

  • 创建人脸库,并设置为可存储10万个人脸特征
    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"
        }
      }
    
    }
  • 使用Python3语言创建人脸库,并设置为可存储10万个人脸特征
    # -*- coding:utf-8 -*-
    
    import requests
    import base64
    
    endpoint = '开通服务所在region的人脸识别服务域名'
    project_id = '开通服务所在region的用户项目ID'
    token = '用户获取得到的实际token值'
    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) 
    
  • 使用Java语言创建人脸库,并设置为可存储10万个人脸特征
    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;
    
    /**
     * 此demo仅供测试使用,强烈建议使用SDK
     * 使用前需已配置HttpClient jar包。jar包可通过下载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();
            // endpoint和project_id需要替换成实际信息。
            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 = "对应region的token";
            String result = doPost(url, jsonStr, token, client);
            System.out.println(result);
        }
    }

响应示例

状态码: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"
      }
    }
  }

}

状态码:400

失败响应样例
{
  "error_code": "FRS.0002",
  "error_msg": "The authentication token is abnormal."
}

状态码

状态码请参见状态码

错误码

错误码请参见错误码

分享:

    相关文档

    相关产品