阿里云人脸识别 1:N 使用简明示例

简介: 基于图像或视频输入进行检测,与注册库比对,实现1:N的人脸识别。适用于人脸登录、VIP人脸识别、人脸通关等无需刷卡验证的场景。目前人脸识别1:N功能暂时还处于公测阶段,开通后可以免费测试试用。下面主要演示1:N 服务的开通及Java程序的调用测试。

概述

基于图像或视频输入进行检测,与注册库比对,实现1:N的人脸识别。适用于人脸登录、VIP人脸识别、人脸通关等无需刷卡验证的场景。目前人脸识别1:N功能已经正式商业化。下面主要演示1:N 服务的开通及Java程序的调用测试。

服务开通

1、复制粘贴地址到浏览器打开链接:https://common-buy.aliyun.com/?commodityCode=face_pre#/open 开通服务(注意复制粘贴操作,不要直接点击,地址跳转可能会存在错误)。
_

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 = "JavaDemo1";
        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%3A%2F%2Fimg0.ph.126.net%2F90u9atgu46nnziAm1NMAGw%3D%3D%2F6631853916514183512.jpg";
        String imageUrl_2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559655604338&di=ee3d8fb39f6e14a21852a4ac3f2c5a14&imgtype=0&src=http%3A%2F%2Fc4.haibao.cn%2Fimg%2F600_0_100_0%2F1473652712.0005%2F87c7805c10e60e9a6db94f86d6014de8.jpg";
        String recognizeFaceImageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559655604335&di=7b540d703955aed6d235752589aee34a&imgtype=0&src=http%3A%2F%2Fphotocdn.sohu.com%2F20140317%2FImg396736687.jpg";

//        //添加入两张人脸
        AddFace(groupName,person,image_1,imageUrl_1);
        AddFace(groupName,person,image_2,imageUrl_2);

        //列举Group
        ListGroup();

        //列举Faces
        ListFace(groupName);

        //人脸查询
        RecognizeFace(recognizeFaceImageUrl, groupName);

        //删除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);
//        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, 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("RecognizeFace");
        request.putBodyParameter("Group", groupName);
        request.putBodyParameter("ImageUrl", recognizeFaceImageUrl);
//        request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA...");  //检测图片的内容,Base64编码
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
    }
}

3、测试结果

{"Data":"ok","RequestId":"5AA86872-4513-4C36-B1BA-4E7F14EAD252","Success":true}
{"Data":"ok","RequestId":"AB71CDD8-7F88-4E69-AB46-11B7A838BBA7","Success":true}
{"Data":["default","default1","defaultPython","defaultPythonDemo1","defaultPythonDemo2","defaultjs","defaultjsdemo","defaultPHPDemo","defaultPHPDemo1","JavaDemo","JavaDemo1"],"RequestId":"B5B79323-E936-4770-872D-A0ED5EE20F99","Success":true}
{"Data":{"mark":0,"list":[{"person":"LiuYifei","image":"photo2"},{"person":"LiuYifei","image":"photo1"}]},"RequestId":"89D8A329-8046-4F4E-9F5C-8FF6A8204E4F","Success":true}
{"Data":[{"person":"LiuYifei","score":0.65704226,"image":"photo2","rect":[146,131,132,179]}],"RequestId":"A506BC8D-F144-470D-A51A-6628338FD0BF","Success":true}
{"Data":"ok","RequestId":"1E1BF366-1558-4C1B-A48F-2F5F5DD736C9","Success":true}
{"Data":{"mark":0,"list":[{"person":"LiuYifei","image":"photo2"}]},"RequestId":"61EEDF7B-3D24-4EA3-A977-3C6CE10246FE","Success":true}
相关文章
|
Java API 计算机视觉
阿里云新版人脸识别Java使用示例教程
之前阿里云人脸识别只提供人脸检测,人脸属性及人脸对比三个API接口,关于这方面的介绍及使用细节,可以参考阿里云人脸识别使用流程简介,之前使用的服务地址为:dtplus-cn-shanghai.data.aliyuncs.com。目前新版本加入了1:N人脸查找的功能,新版本还处于公测阶段,服务地址:face.cn-shanghai.aliyuncs.com。下面主要介绍如何使用新版本的地址调用之前的三个API的功能。
2971 0
|
Unix Linux Windows
如何调整服务器系统时间
如何调整服务器系统时间
1692 0
|
网络协议 网络架构
【计算机网络】OSI、TCP/IP、五层模型
【计算机网络】OSI、TCP/IP、五层模型
|
3月前
|
人工智能 自然语言处理 运维
保姆级教程:2026年阿里云轻量服务器快速部署OpenClaw(Clawdbot)步骤
OpenClaw(原Clawdbot/Moltbot)作为阿里云生态下的开源AI自动化代理工具,凭借“自然语言交互+自动化任务执行+插件化扩展”的核心特性,已成为个人办公、中小企业协作提效的核心工具。2026年阿里云轻量应用服务器针对OpenClaw推出专属预装镜像,将原本需要数小时的环境配置、依赖安装流程简化为“一键部署”,即使是零基础的新手,也能在30分钟内完成从服务器购买到功能验证的全流程。本文将以阿里云轻量应用服务器为核心载体,详细拆解OpenClaw的部署、配置、功能验证与运维全流程,包含实操代码命令与避坑技巧,确保新手零门槛上手。
3909 3
|
弹性计算 固态存储 大数据
2025阿里云服务器租赁价格表一年、1个月和1小时收费标准(200M峰值带宽)
阿里云服务器价格优惠,2025年最新租用费用表显示,轻量应用服务器2核2G配置一年仅需68元(秒杀38元),带200M峰值带宽。云服务器ECS方面,99元/年的2核2G经济型和199元/年的2核4G企业专享型备受青睐。4核16G游戏服务器70元/月,8核32G则160元/月。GPU服务器也有大幅折扣,如T4显卡的gn6i最低配置4核15G一个月1878.40元。续费享有长期折扣,1年7.5折,3年4.5折等。公网带宽和系统盘按需计费,ESSD云盘性能优越,价格透明。详情见官网。
|
Java 定位技术
109.【Java最全腾讯地图接口】(一)
109.【Java最全腾讯地图接口】
848 1
|
Prometheus 监控 Java
如何全面监控所有的 Spring Boot 微服务
如何全面监控所有的 Spring Boot 微服务
689 3
使用BASE64方式进行人脸检测
【8月更文挑战第2天】使用BASE64方式进行人脸检测。
286 3

热门文章

最新文章