概述
人脸人体识别技术是基于阿里云深度学习算法,结合图像或视频的人脸检测、分析、比对以及人体检测等技术,为您提供人脸人体的检测定位、人脸属性识别和人脸比对等能力。可以为开发者和企业提供高性能的在线API服务,应用于人脸AR、人脸识别和认证、大规模人脸检索、照片管理等各种场景。
Step By Step
1、服务开通,参考链接:阿里云视觉智能开放平台使用简明教程
2、目前主要提供三个方面的能力:人脸识别、人体识别、人脸对比,能力介绍
3、其中人脸识别、人体识别 目前主要是单个API的调用,不涉及多个API的协同调用,使用相对简单(实例仅选择一个API演示)。人脸对比涉及到人脸库、样本、图片等的操作,需要多个API的协同操作:
4、Code Sample
- 4.1 pom.xml
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.52</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.0.8</version>
</dependency>
</dependencies>
4.2 Java Code
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 com.aliyuncs.facebody.model.v20191230.*;
public class Demo1 {
// 阿里云常见参数获取位置 https://yq.aliyun.com/articles/693979
private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<access key>", "<access secret>");
private static IAcsClient client = new DefaultAcsClient(profile);
public static void main(String[] args) {
// 人脸识别 -- 人脸属性识别
String recognizeFaceURL = "http://vigen-invi.oss-cn-shanghai.aliyuncs.com/demo/test.jpg";
recognizeFace(recognizeFaceURL);
// 人体识别 -- 人体计算
String detectBodyCountURL = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/facebody/RecognizeBodyCount/ddd2512910b84bc3ffeba5ac44b30d81.jpg";
detectBodyCount(detectBodyCountURL);
// 人脸对比
String dbName = "tarogroup";
Integer Offset = 0;
String wangfei1 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/face1.jpg";
String wangfei2 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/face2.jpg";
String jingtian1 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/jingtian1.jpeg";
String jingtian2 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/jingtian2.jpeg";
compareFace(wangfei1,jingtian1);// 人脸对比
// 1:N 人脸查找
// deleteFaceDb(dbName);
// listFaceEntities(dbName, Offset);
// deleteFaceEntity(dbName,"2");
// listFaceDbs();
// createFaceDbRequest(dbName);
// listFaceDbs();
String entityId1 = "wangfei";
String entityId2 = "jingtian";
// 创建人脸样本
// addFaceEntity(dbName, entityId1);
// addFaceEntity(dbName, entityId2);
// 想人脸样本中加入人脸,每个样本人脸上限为5
// addFace(dbName,entityId1,wangfei1);
// addFace(dbName,entityId1,wangfei2);
//
// addFace(dbName,entityId2,jingtian1);
// addFace(dbName,entityId2,jingtian2);
//
// listFaceEntities(dbName, 0);
// 到人脸库中查找
searchFace(dbName,wangfei1,1);
}
/**
* 人脸属性识别
* @param imageURL 图片URL地址
*/
private static void recognizeFace(String imageURL)
{
RecognizeFaceRequest recognizeFaceRequest = new RecognizeFaceRequest();
recognizeFaceRequest.setImageURL(imageURL);
try {
RecognizeFaceResponse recognizeFaceResponse = client.getAcsResponse(recognizeFaceRequest);
System.out.println("人脸属性识别:");
System.out.println(new Gson().toJson(recognizeFaceResponse));
} 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());
}
}
/**
* 人体计数
* @param imageURL 图片URL地址
*/
private static void detectBodyCount(String imageURL)
{
DetectBodyCountRequest detectBodyCountRequest = new DetectBodyCountRequest();
detectBodyCountRequest.setImageURL(imageURL);
try {
DetectBodyCountResponse detectBodyCountResponse = client.getAcsResponse(detectBodyCountRequest);
System.out.println("人体计数:");
System.out.println(new Gson().toJson(detectBodyCountResponse));
} 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());
}
}
/**
* 创建人脸数据库
* @param dbName 数据库名称
*/
private static void createFaceDbRequest(String dbName)
{
CreateFaceDbRequest createFaceDbRequest = new CreateFaceDbRequest();
createFaceDbRequest.setName(dbName);
try {
CreateFaceDbResponse response = client.getAcsResponse(createFaceDbRequest);
System.out.println("创建人脸数据库:");
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());
}
}
/**
* 查看数据库列表
*/
private static void listFaceDbs()
{
ListFaceDbsRequest listFaceDbsRequest = new ListFaceDbsRequest();
try {
ListFaceDbsResponse response = client.getAcsResponse(listFaceDbsRequest);
System.out.println("查看数据库列表: ");
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());
}
}
/**
* 添加人脸样本
* @param dbName 数据库名称
* @param entityId 实体ID
*/
private static void addFaceEntity(String dbName, String entityId)
{
AddFaceEntityRequest addFaceEntityRequest = new AddFaceEntityRequest();
addFaceEntityRequest.setDbName(dbName);
addFaceEntityRequest.setEntityId(entityId);
try{
AddFaceEntityResponse addFaceEntityResponse = client.getAcsResponse(addFaceEntityRequest);
System.out.println("添加人脸样本:");
System.out.println(new Gson().toJson(addFaceEntityResponse));
} 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());
}
}
/**
* 查询人脸样本
* @param dbName 数据库名称
* @param entityId 实体ID
*/
private static void getFaceEntity(String dbName, String entityId)
{
GetFaceEntityRequest getFaceEntityRequest = new GetFaceEntityRequest();
getFaceEntityRequest.setDbName(dbName);
getFaceEntityRequest.setEntityId(entityId);
try{
GetFaceEntityResponse getFaceEntityResponse = client.getAcsResponse(getFaceEntityRequest);
System.out.println("查询人脸样本:");
System.out.println(new Gson().toJson(getFaceEntityResponse));
} 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());
}
}
/**
* 查询人脸样本列表
* @param dbName 数据库名称
* @param Offset 起始记录
*/
private static void listFaceEntities(String dbName, Integer Offset)
{
ListFaceEntitiesRequest listFaceEntitiesRequest = new ListFaceEntitiesRequest();
listFaceEntitiesRequest.setDbName(dbName);
listFaceEntitiesRequest.setOffset(Offset);
try{
ListFaceEntitiesResponse listFaceEntitiesResponse = client.getAcsResponse(listFaceEntitiesRequest);
System.out.println("查询人脸样本列表:");
System.out.println(new Gson().toJson(listFaceEntitiesResponse));
} 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());
}
}
/**
* 更新人脸样本
* @param dbName 数据库名称
* @param entityId 实体ID
* @param labels 标签名称
*/
private static void updateFaceEntity(String dbName, String entityId, String labels)
{
UpdateFaceEntityRequest updateFaceEntityRequest = new UpdateFaceEntityRequest();
updateFaceEntityRequest.setDbName(dbName);
updateFaceEntityRequest.setEntityId(entityId);
updateFaceEntityRequest.setLabels(labels);
try{
UpdateFaceEntityResponse updateFaceEntityResponse = client.getAcsResponse(updateFaceEntityRequest);
System.out.println("更新人脸样本:");
System.out.println(new Gson().toJson(updateFaceEntityResponse));
} 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());
}
}
/**
* 删除人脸样本
* @param dbName 数据库名称
* @param entityId 实体ID
*/
private static void deleteFaceEntity(String dbName, String entityId)
{
DeleteFaceEntityRequest deleteFaceEntityRequest = new DeleteFaceEntityRequest();
deleteFaceEntityRequest.setDbName(dbName);
deleteFaceEntityRequest.setEntityId(entityId);
try{
DeleteFaceEntityResponse deleteFaceEntityResponse = client.getAcsResponse(deleteFaceEntityRequest);
System.out.println("删除人脸样本:");
System.out.println(new Gson().toJson(deleteFaceEntityResponse));
} 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());
}
}
/**
* 添加人脸数据
* @param dbName 数据库名称
* @param entityId 实体ID
* @param imageUrl 人脸图片地址,必须是同Region的OSS的图片地址。人脸必须是正面无遮挡单人人脸。
*/
private static void addFace(String dbName, String entityId, String imageUrl)
{
AddFaceRequest addFaceRequest = new AddFaceRequest();
addFaceRequest.setDbName(dbName);
addFaceRequest.setEntityId(entityId);
addFaceRequest.setImageUrl(imageUrl);
try{
AddFaceResponse addFaceResponse = client.getAcsResponse(addFaceRequest);
System.out.println("添加人脸数据:");
System.out.println(new Gson().toJson(addFaceResponse));
} 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());
}
}
/**
* 搜索人脸
* @param dbName 数据库名称
* @param imageUrl 图片URL地址。必须是同Region的OSS地址。
*/
private static void searchFace(String dbName, String imageUrl, Integer limit)
{
SearchFaceRequest searchFaceRequest = new SearchFaceRequest();
searchFaceRequest.setDbName(dbName);
searchFaceRequest.setImageUrl(imageUrl);
searchFaceRequest.setLimit(limit);
try{
SearchFaceResponse searchFaceResponse = client.getAcsResponse(searchFaceRequest);
System.out.println("搜索人脸:");
System.out.println(new Gson().toJson(searchFaceResponse));
} 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());
}
}
/**
* 删除数据库
* @param dbName 数据库名称
*/
private static void deleteFace(String dbName, String faceId)
{
DeleteFaceRequest deleteFaceRequest = new DeleteFaceRequest();
deleteFaceRequest.setDbName(dbName);
deleteFaceRequest.setFaceId(faceId);
try{
DeleteFaceResponse deleteFaceResponse = client.getAcsResponse(deleteFaceRequest);
System.out.println("删除人脸:");
System.out.println(new Gson().toJson(deleteFaceResponse));
} 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());
}
}
/**
* 删除数据库
* @param dbName 数据库名称
*/
private static void deleteFaceDb(String dbName)
{
DeleteFaceDbRequest deleteFaceDbRequest = new DeleteFaceDbRequest();
deleteFaceDbRequest.setName(dbName);
try{
DeleteFaceDbResponse deleteFaceDbResponse = client.getAcsResponse(deleteFaceDbRequest);
System.out.println("删除数据库:");
System.out.println(new Gson().toJson(deleteFaceDbResponse));
} 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());
}
}
/**
* 人脸对比
* @param imageURLA 待对比图片URL地址A
* @param imageURLB 待对比图片URL地址B
*/
private static void compareFace(String imageURLA, String imageURLB)
{
CompareFaceRequest compareFaceRequest = new CompareFaceRequest();
compareFaceRequest.setImageURLA(imageURLA);
compareFaceRequest.setImageURLB(imageURLB);
try{
CompareFaceResponse compareFaceResponse = client.getAcsResponse(compareFaceRequest);
System.out.println("人脸对比:");
System.out.println(new Gson().toJson(compareFaceResponse));
} 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());
}
}
}