人脸属性识别(RecognizeFace)是阿里云智能开放平台推出的人脸人体能力下的一个API接口,您可以识别检测人脸的性别、年龄、表情、眼镜四种属性,并返回人脸的1024维深度学习特征。
背景信息
人脸人体识别技术基于阿里云深度学习算法,结合图像或视频的人脸检测、分析、比对以及人体检测等技术,为您提供人脸人体的检测定位、人脸属性识别和人脸比对等能力。可以为开发者和企业提供高性能的在线API服务,应用于人脸AR、人脸识别和认证、大规模人脸检索、照片管理等各种场景。
开通服务
调用人脸属性识别API前,您需要先开通人脸人体服务。在进行人脸识别时,需要先将图片上传至上海地域的OSS Bucket中生成URL链接,通过图片URL链接传入图片数据。关于如何开通OSS服务请参见开通OSS服务。下面为您介绍如何开通人脸人体服务。
生成URL
例如我们需要对下图进行人脸识别检测。

添加依赖包
请根据您的代码选择添加不同的依赖包。
- 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。
- 如果您的Visual Studio没有安装NuGet,请先安装NuGet。
- 在Visual Studio中新建或者打开已有的项目,选择 。
- 搜索aliyun-net-sdk-core,在结果中找到aliyun-net-sdk-core,选择最新版本,单击安装。
代码示例
请求示例如下:
- Java请求示例
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 RecognizeFace { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>"); IAcsClient client = new DefaultAcsClient(profile); RecognizeFaceRequest request = new RecognizeFaceRequest(); request.setRegionId("cn-shanghai"); request.setImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E4%BA%BA%E8%84%B8%E5%B1%9E%E6%80%A7%E8%AF%86%E5%88%AB.jpg"); try { RecognizeFaceResponse 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请求示例
#!/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.RecognizeFaceRequest import RecognizeFaceRequest client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai') request = RecognizeFaceRequest() request.set_accept_format('json') request.set_ImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E4%BA%BA%E8%84%B8%E5%B1%9E%E6%80%A7%E8%AF%86%E5%88%AB.jpg") response = client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8'))
- Node.js请求示例
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": "http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E4%BA%BA%E8%84%B8%E5%B1%9E%E6%80%A7%E8%AF%86%E5%88%AB.jpg" } var requestOption = { method: 'POST' }; client.request('RecognizeFace', params, requestOption).then((result) => { console.log(JSON.stringify(result)); }, (ex) => { console.log(ex); })
- PHP请求示例
<?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('RecognizeFace') ->method('POST') ->host('facebody.cn-shanghai.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "cn-shanghai", 'ImageURL' => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E4%BA%BA%E8%84%B8%E5%B1%9E%E6%80%A7%E8%AF%86%E5%88%AB.jpg", ], ]) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; }
- .NET请求示例
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 = "RecognizeFace"; // request.Protocol = ProtocolType.HTTP; request.AddQueryParameters("ImageURL", "http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E4%BA%BA%E8%84%B8%E5%B1%9E%E6%80%A7%E8%AF%86%E5%88%AB.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": "B0760ABB-F70C-4B00-B9FF-4BD553B4233E",
"Data": {
"FaceRectangles": [
215,
57,
172,
229
],
"DenseFeatureLength": 1024,
"PoseList": [
"-15.702389717102051",
"8.4830875396728516",
"0.080579757690429688"
],
"Pupils": [
"243.02786254882812",
"149.0238037109375",
"7.3701672554016113",
"315.86138916015625",
"140.451416015625",
"7.3701672554016113"
],
"GenderList": [
1
],
"DenseFeatures": [
"-0.037511065602302551",
"0.022202443331480026",
"0.079755261540412903",
"-0.007559098768979311",
"0.032428193837404251",
"-0.031667508184909821",
"-0.017420554533600807",
"-0.000207205128390342",
"-0.014593920670449734",
"-0.0037230239249765873",
"0.013444140553474426",
"0.082390092313289642",
"0.013446067459881306",
"-0.0037146448157727718",
"-0.0077681485563516617",
"0.054059881716966629",
"0.030584953725337982",
"0.051545839756727219",
"0.025875747203826904",
"-0.0015580562176182866",
"...",
"0.0072302431799471378",
"-0.0027828323654830456",
"-0.021698996424674988",
"-0.022436553612351418",
"0.019861612468957901"
],
"FaceProbabilityList": [
1
],
"LandmarkCount": 105,
"AgeList": [
55
],
"Glasses": [
1
],
"Landmarks": [
"213.98641967773438",
"145.24307250976562",
263.90673828125,
"135.84013366699219",
"237.13218688964844",
"127.43693542480469",
"238.47280883789062",
"137.8927001953125",
"218.65261840820312",
"136.04519653320312",
"227.24664306640625",
"130.36431884765625",
247.36865234375,
"126.25999450683594",
"257.45843505859375",
"128.07203674316406",
"222.02139282226562",
"142.34713745117188",
"230.11050415039062",
"139.60614013671875",
"246.92530822753906",
"136.674072265625",
"255.44786071777344",
"136.97525024414062",
"291.1983642578125",
"134.10868835449219",
"344.8785400390625",
"138.02426147460938",
"318.74273681640625",
"...",
"243.86373901367188",
"265.70501708984375",
"324.709716796875",
"277.518798828125"
],
"Expressions": [
0
],
"FaceCount": 1
}
}
从返回结果中得到该图片的人脸属性信息结果如下:
- 人脸个数:1
- 性别:男性
- 年龄:55
- 人脸表情:中性
- 是否戴眼镜:有眼镜
在文档使用中是否遇到以下问题
更多建议
匿名提交