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

查询人脸

功能介绍

查询指定人脸库中人脸信息。

前提条件:

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

调试

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

URI

GET /v2/{project_id}/face-sets/{face_set_name}/faces?offset=xxx&limit=xxx

或者

GET /v2/{project_id}/face-sets/{face_set_name}/faces?face_id={face_id}

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

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

face_set_name

String

人脸库名称。

表2 Query参数

参数

是否必选

参数类型

描述

offset

否,与face_id二选一

Integer

查询的起始位置,取值范围为非负整数,默认为0。

limit

否,与face_id二选一

Integer

单次查询总量,默认为5。offset+limit值不能超过10000。

face_id

否,与offset二选一

String

人脸ID。

offset+limit值不能超过10000。人脸识别系统非存储系统,暂时不支持数据遍历操作,用户可以自行保存face_id,根据face_id查询导入的人脸信息。

请求参数

表3 请求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,正常使用FRS服务,账单的企业项目会被分类为“未归集”。

响应参数

状态码:200

表4 响应Body参数

参数

参数类型

描述

face_set_id

String

人脸库ID,随机生成的包含八个字符的字符串。 调用失败时无此字段。

face_set_name

String

人脸库名称。 调用失败时无此字段。

faces

Array of FaceSetFace objects

人脸库当中的人脸结构,详见FaceSetFace。 调用失败时无此字段。

表5 FaceSetFace

参数

参数类型

描述

bounding_box

BoundingBox object

人脸在图像中的位置。 BoundingBox结构见BoundingBox

external_fields

Object

用户添加的额外字段。

external_image_id

String

人脸所在的外部图片ID。

face_id

String

人脸ID,由系统内部生成的唯一ID。

表6 BoundingBox

参数

参数类型

描述

width

Integer

矩形框宽度。

top_left_y

Integer

矩形框左上角纵坐标。

top_left_x

Integer

矩形框左上角横坐标。

height

Integer

矩形框高度。

状态码: 400

表7 响应Body参数

参数

参数类型

描述

error_code

String

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

error_msg

String

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

请求示例

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

  • 在人脸库中查询某一人脸特征信息
    GET https://{endpoint}/v2/{project_id}/face-sets/showFaceSet/faces?offset=0&limit=1
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDT...
  • 使用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?offset=0&limit=1".format(
        endpoint=endpoint, project_id=project_id, face_set_name=face_set_name)
    response = requests.get(url, headers=headers, 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.HttpGet;
    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.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 GetFaceByFaceId {
        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 doGet(String url, String token, CloseableHttpClient client) {
            HttpGet get = new HttpGet(url);
            get.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            //time unit is milliseconds
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build();
            get.setConfig(requestConfig);
            get.setHeader("X-Auth-Token", token);
            HttpResponse response = null;
            String result = "";
            try {
                response = client.execute(get);
                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和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 = doGet(url, token, client);
            System.out.println(result);
        }
    }

响应示例

状态码:200

成功响应样例
{
  "face_set_id": "T785tx1N",
  "face_set_name": "showFaceSet",
  "faces": [
    {
      "bounding_box": {
        "width": 63,
        "top_left_y": 100,
        "top_left_x": 221,
        "height": 63
      },
      "external_image_id": "aIzRAa58",
      "face_id": "cFydu4d2",
      "external_fields": {
        "number": 122,
        "id": "home",
        "timestamp": 12
      }
    }
  ]
}

状态码:400

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

状态码

状态码请参见状态码

错误码

错误码请参见错误码

分享:

    相关文档

    相关产品