概述
基于图像或视频输入进行检测, 与注册库比对, 实现 1:N 的人脸识别. 适用于人脸登录, VIP 人脸识别, 人脸通关等无需刷卡验证的场景. 目前人脸识别 1:N 功能暂时还处于公测阶段, 开通后可以免费测试试用. 下面主要演示 1:N 服务的开通及 Java 程序的调用测试.
服务开通
1, 点击开通链接开通服务.
2, 到费用中心零元支付.
3, 登陆控制台查看 (公测阶段管理控制台功能还不完善, 不过不影响程序的测试调用).
- Java Code Sample
- 1,pom.xml
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>aliyun-java-sdk-core</artifactId>
- <version>4.1.1</version>
- </dependency>
- 2,Code Sample
- import com.aliyuncs.CommonRequest;
- import com.aliyuncs.CommonResponse;
- import com.aliyuncs.DefaultAcsClient;
- import com.aliyuncs.exceptions.ClientException;
- import com.aliyuncs.http.MethodType;
- import com.aliyuncs.profile.DefaultProfile;
- public class Demo {
- //DefaultProfile.getProfile 的参数分别是地域, access_key_id, access_key_secret
- public static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "******", "******");
- public static DefaultAcsClient client = new DefaultAcsClient(profile);
- public static void main(String[] args) throws ClientException {
- String groupName = "default";
- String person = "LiuYifei";
- String image_1 = "photo1";
- String image_2 = "photo2";
- String imageUrl_1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559655604341&di=3d6995f6dee1c4795d1827e754a00452&imgtype=0&src=http://img0.ph.126.net/90u9atgu46nnziAm1NMAGw==/6631853916514183512.jpg";
- String imageUrl_2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559655604338&di=ee3d8fb39f6e14a21852a4ac3f2c5a14&imgtype=0&src=http://c4.haibao.cn/img/600_0_100_0/1473652712.0005/87c7805c10e60e9a6db94f86d6014de8.jpg";
- String recognizeFaceImageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559655604335&di=7b540d703955aed6d235752589aee34a&imgtype=0&src=http://photocdn.sohu.com/20140317/Img396736687.jpg";
- // 添加入两张人脸
- AddFace(groupName,person,image_1,imageUrl_1);
- AddFace(groupName,person,image_2,imageUrl_2);
- // 列举 Group
- ListGroup();
- // 列举 Faces
- ListFace(groupName);
- // 人脸查询
- RecognizeFace(recognizeFaceImageUrl);
- // 删除 Face
- DeleteFace(groupName,person,image_1);
- // 列举 Faces 查询删除情况
- ListFace(groupName);
- }
- /**
- * AddFace 接口用于向人脸库中添加人脸
- * @param groupName 添加人脸的分组
- * @param person 添加人脸的姓名
- * @param image 添加人脸的编号
- * @param imageUrl 检测图片的 URL
- */
- public static void AddFace(String groupName,String person,String image,String imageUrl)
- {
- CommonRequest request = new CommonRequest();
- request.setMethod(MethodType.POST);
- request.setDomain("face.cn-shanghai.aliyuncs.com");
- request.setVersion("2018-12-03");
- request.setAction("AddFace");
- request.putBodyParameter("Group", groupName); // 添加人脸的分组
- request.putBodyParameter("Person", person); // 添加人脸的姓名
- request.putBodyParameter("Image", image); // 添加人脸的编号
- request.putBodyParameter("ImageUrl",imageUrl); // 检测图片的 URL
- // request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA..."); // 检测图片的内容, Base64 编码
- CommonResponse response = null;
- try {
- response = client.getCommonResponse(request);
- } catch (ClientException e) {
- e.printStackTrace();
- }
- System.out.println(response.getData());
- }
- /**
- * DeleteFace 接口用于从人脸库中删除人脸
- * @param groupName 添加人脸的分组
- * @param person 添加人脸的姓名
- * @param image 添加人脸的编号
- * @throws ClientException
- */
- public static void DeleteFace(String groupName,String person,String image) throws ClientException {
- CommonRequest request = new CommonRequest();
- request.setMethod(MethodType.POST);
- request.setDomain("face.cn-shanghai.aliyuncs.com");
- request.setVersion("2018-12-03");
- request.setAction("DeleteFace");
- request.putBodyParameter("Group", groupName);
- request.putBodyParameter("Person", person);
- request.putBodyParameter("Image", image);
- CommonResponse response = client.getCommonResponse(request);
- System.out.println(response.getData());
- }
- /**
- * ListFace 接口用于列举注册库中的人脸
- * @param groupName 需要查询的库
- * @throws ClientException
- */
- public static void ListFace(String groupName) throws ClientException {
- CommonRequest request = new CommonRequest();
- request.setMethod(MethodType.POST);
- request.setDomain("face.cn-shanghai.aliyuncs.com");
- request.setVersion("2018-12-03");
- request.setAction("ListFace");
- request.putBodyParameter("Group", groupName); // 列举的人脸组
- CommonResponse response = client.getCommonResponse(request);
- System.out.println(response.getData());
- }
- /**
- * ListGroup 接口用于列举人脸组
- * @throws ClientException
- */
- public static void ListGroup() throws ClientException {
- CommonRequest request = new CommonRequest();
- request.setMethod(MethodType.POST);
- request.setDomain("face.cn-shanghai.aliyuncs.com");
- request.setVersion("2018-12-03");
- request.setAction("ListGroup");
- CommonResponse response = client.getCommonResponse(request);
- System.out.println(response.getData());
- }
- /**
- * RecognizeFace 接口用于查找注册库中的人脸
- * @param recognizeFaceImageUrl 需要查询的人类图片 URL
- * @throws ClientException
- */
- public static void RecognizeFace(String recognizeFaceImageUrl) throws ClientException {
- CommonRequest request = new CommonRequest();
- request.setMethod(MethodType.POST);
- request.setDomain("face.cn-shanghai.aliyuncs.com");
- request.setVersion("2018-12-03");
- request.setAction("RecognizeFace");
- request.putBodyParameter("Group", "default"); // 待识别的人脸分组
- request.putBodyParameter("ImageUrl", recognizeFaceImageUrl); // 检测图片的 URL
- // request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA..."); // 检测图片的内容, Base64 编码
- CommonResponse response = client.getCommonResponse(request);
- System.out.println(response.getData());
- }
- }
3, 测试结果
- {
- "Data":"ok","RequestId":"9180EBDF-836E-4C6F-A9A7-FEC32CEB202A","Success":true
- }
- {
- "Data":"ok","RequestId":"B29A6752-A93D-4DEB-B07F-AEC727C7DB37","Success":true
- }
- {
- "Data":["default"],"RequestId":"8A6B9AF3-B914-4199-929B-2CF14C8E94F7","Success":true
- }
- {
- "Data":{
- "mark":0,"list":[{
- "person":"LiuYifei","image":"photo1"
- },{
- "person":"LiuYifei","image":"photo2"
- }]
- },"RequestId":"91D3285D-D2B6-4600-B0C6-6A1649452FDF","Success":true
- }
- {
- "Data":[{
- "person":"LiuYifei","score":0.66236943,"image":"photo2","rect":[146,131,132,179]
- }],"RequestId":"0484F0F4-CD52-42D3-A113-5C7466B24331","Success":true
- }
- {
- "Data":"ok","RequestId":"BD8BA622-50AD-466D-BDD8-AF3EA7B19E2A","Success":true
- }
- {
- "Data":{
- "mark":0,"list":[{
- "person":"LiuYifei","image":"photo2"
- }]
- },"RequestId":"45876486-C29A-4A84-A240-FB90FF3E03B3","Success":true
- }
参考链接
人脸查找 1:N SDK 调用说明
人脸查找 1:N API 调用说明
来源: https://yq.aliyun.com/articles/704556