人脸检测定位(DetectFace)是阿里云智能开放平台推出的人脸人体能力下的一个API接口,您可以调用该接口对图片中的人脸进行检测,并给出每张人脸的定位和关键点信息。

开通服务

调用人脸检测定位API前,您需要先开通人脸人体服务。在进行人脸检测时,需要先将图片上传至上海地域的OSS Bucket中生成URL链接,通过图片URL链接传入图片数据。关于如何开通OSS服务请参见开通OSS服务。下面为您介绍如何开通人脸人体服务。

  1. 登录阿里云视觉智能开放平台
  2. 在顶部菜单栏的能力广场中单击人脸人体
  3. 人脸人体页面中单击免费使用
  4. 确认开通地域后单击立即购买
  5. 确认订单步骤中勾选我已阅读并同意人脸人体服务协议后单击立即开通

    页面提示服务开通成功。

    说明
    • 当前平台还在公测阶段,所有服务均可以免费使用。
    • 开通了人脸人体服务后,该服务下的所有API均可以直接调用,不需要再单独开通。

生成URL

例如我们需要对如下图像进行人脸检测。人脸检测定位
  1. 登录OSS管理控制台
  2. 创建存储空间
    创建Bucket选择区域时,需要选择与人脸人体服务能力相同的区域华东2(上海)
    说明 如果您的OSS所在地域不是华东2(上海),那么请参见非上海地域OSS用户生成URL。
  3. 将上述示例图片上传到OSS服务。
    具体操作请参见上传文件
  4. 查看图片URL。
    在已上传图片列表中,单击详情查看并复制图片URL。
    https://viapi-test.oss-cn-shanghai.aliyuncs.com/demo-center/facebody/DetectFace.jpg

添加依赖包

请根据您的代码选择添加不同的依赖包。
  • Java
    在您的Java项目中添加人脸人体能力的POM依赖。
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.5.14</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-facebody -->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-facebody</artifactId>
        <version>1.2.2</version>
    </dependency>
  • Python
    打开命令提示符窗口,依次执行如下命令安装Python SDK。
    pip install aliyun-python-sdk-core
    pip install aliyun-python-sdk-facebody
  • Node.js
    使用NPM安装Node.js SDK。
    npm install @alicloud/pop-core -S
    npm install @alicloud/facebody20191230@1.2.1 --save
  • PHP
    使用composer工具安装PHP SDK。
    composer require alibabacloud/facebody-20191230
  • C#
    使用NuGet程序包安装.NET SDK。
    1. 如果您的Visual Studio没有安装NuGet,请先安装NuGet
    2. 在Visual Studio中新建或者打开已有的项目,选择工具 > NuGet包管理器 > 管理解决方案的NuGet程序包
    3. 搜索aliyun-net-sdk-core,在结果中找到aliyun-net-sdk-core,选择最新版本,单击安装

请求示例

请求代码示例如下:
  • Java请求示例
    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.facebody.model.v20191230.*;
    
    public class DetectFace {
    
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            DetectFaceRequest request = new DetectFaceRequest();
            request.setRegionId("cn-shanghai");
            request.setImageURL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/demo-center/facebody/DetectFace.jpg");
    
            try {
                DetectFaceResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }
  • Python请求示例
    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    #!/usr/bin/env python
    #coding=utf-8
    
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdkfacebody.request.v20191230.DetectFaceRequest import DetectFaceRequest
    
    client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai')
    
    request = DetectFaceRequest()
    request.set_accept_format('json')
    
    request.set_ImageURL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/demo-center/facebody/DetectFace.jpg")
    
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))                
  • Node.js请求示例
    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    const Core = require('@alicloud/pop-core');
    
    var client = new Core({
      accessKeyId: '<accessKeyId>',
      accessKeySecret: '<accessSecret>',
      endpoint: 'https://facebody.cn-shanghai.aliyuncs.com',
      apiVersion: '2019-12-30'
    });
    
    var params = {
      "RegionId": "cn-shanghai",
      "ImageURL": "https://viapi-test.oss-cn-shanghai.aliyuncs.com/demo-center/facebody/DetectFace.jpg"
    }
    
    var requestOption = {
      method: 'POST'
    };
    
    client.request('DetectFace', params, requestOption).then((result) => {
      console.log(JSON.stringify(result));
    }, (ex) => {
      console.log(ex);
    })
  • PHP请求示例
    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    <?php
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    
    // Download:https://github.com/aliyun/openapi-sdk-php
    // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
    
    AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                            ->regionId('cn-shanghai')
                            ->asDefaultClient();
    
    try {
        $result = AlibabaCloud::rpc()
                              ->product('facebody')
                              // ->scheme('https') // https | http
                              ->version('2019-12-30')
                              ->action('DetectFace')
                              ->method('POST')
                              ->host('facebody.cn-shanghai.aliyuncs.com')
                              ->options([
                                            'query' => [
                                              'RegionId' => "cn-shanghai",
                                              'ImageURL' => "https://viapi-test.oss-cn-shanghai.aliyuncs.com/demo-center/facebody/DetectFace.jpg",
                                            ],
                                        ])
                              ->request();
        print_r($result->toArray());
    } catch (ClientException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    } catch (ServerException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    }
  • .NET请求示例
    说明 请将如下代码示例中的<accessKeyId><accessSecret>替换为您的真实AK信息。如何获取AK请参见创建AccessKey
    using System;
    using System.Collections.Generic;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Core.Exceptions;
    using Aliyun.Acs.Core.Http;
    
    namespace CommonRequestDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
                DefaultAcsClient client = new DefaultAcsClient(profile);
                CommonRequest request = new CommonRequest();
                request.Method = MethodType.POST;
                request.Domain = "facebody.cn-shanghai.aliyuncs.com";
                request.Version = "2019-12-30";
                request.Action = "DetectFace";
                // request.Protocol = ProtocolType.HTTP;
                request.AddQueryParameters("ImageURL", "https://viapi-test.oss-cn-shanghai.aliyuncs.com/demo-center/facebody/DetectFace.jpg");
                try {
                    CommonResponse response = client.GetCommonResponse(request);
                    Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
                }
                catch (ServerException e)
                {
                    Console.WriteLine(e);
                }
                catch (ClientException e)
                {
                    Console.WriteLine(e);
                }
            }
        }
    }
                            

结果示例

该接口的请求结果示例如下。
{
    "RequestId": "81E8C954-A5A1-4361-A0A2-DCE722BD3E74",
    "Data": {
        "FaceProbabilityList": [
            1
        ],
        "LandmarkCount": 105,
        "FaceRectangles": [
            153,
            4,
            310,
            426
        ],
        "Landmarks": [
            "179.71014404296875",
            "170.73817443847656",
            "272.2652587890625",
            "157.37681579589844",
            "219.99136352539062",
            "138.14523315429688",
            "225.23085021972656",
            "158.95890808105469",
            "151.02610778808594",
            "373.52310180664062",
            "145.79440307617188",
            "229.51496887207031",
            ......
            "238.59555053710938",
            "183.47145080566406",
            "247.61610412597656",
            "182.42185974121094",
            "256.4852294921875",
            "180.46705627441406",
            "358.20834350585938",
            "178.58555603027344",
            "425.43515014648438",
            "137.3551025390625",
            "253.60928344726562",
            "473.96920776367188",
            "261.82354736328125",
            "218.69778442382812",
            "404.356689453125",
            "383.27932739257812",
            "406.52761840820312"
        ],
        "FaceCount": 1,
        "PoseList": [
            "0.86857914924621582",
            "-1.3923481702804565",
            "1.8193349838256836"
        ],
        "Pupils": [
            "232.2469482421875",
            "170.73431396484375",
            "17.970651626586914",
            "387.22463989257812",
            "173.99905395507812",
            "17.970651626586914"
        ]
    }
}