阿里云人脸识别公测使用说明

作者:俏巴


概述


之前阿里云人脸识别只提供人脸检测,人脸属性及人脸对比三个API接口,关于这方面的介绍及使用细节,可以参考阿里云人脸识别使用流程简介,之前使用的服务地址为:dtplus-cn-shanghai.data.aliyuncs.com。目前新版本加入了1:N人脸查找的功能,新版本还处于公测阶段,服务地址:face.cn-shanghai.aliyuncs.com。下面主要介绍如何使用新版本的地址调用之前的三个API的功能。


使用流程


1、服务开通及1:N人脸识别使用
阿里云人脸识别 1:N 使用简明示例


2、接口调用Code示例


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 CommomDemo {

<span class="hljs-comment">//DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret</span>
public <span class="hljs-keyword">static</span> DefaultProfile profile = DefaultProfile.getProfile(<span class="hljs-string">"cn-shanghai"</span>, <span class="hljs-string">"********"</span>, <span class="hljs-string">"********"</span>);
public <span class="hljs-keyword">static</span> DefaultAcsClient client = <span class="hljs-keyword">new</span> DefaultAcsClient(profile);

public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(<span class="hljs-built_in">String</span>[] args)  {

    <span class="hljs-built_in">String</span> imageUrl_1 = <span class="hljs-string">"https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1559655604341&amp;di=3d6995f6dee1c4795d1827e754a00452&amp;imgtype=0&amp;src=http%3A%2F%2Fimg0.ph.126.net%2F90u9atgu46nnziAm1NMAGw%3D%3D%2F6631853916514183512.jpg"</span>;
    <span class="hljs-built_in">String</span> imageUrl_2 = <span class="hljs-string">"https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1559655604338&amp;di=ee3d8fb39f6e14a21852a4ac3f2c5a14&amp;imgtype=0&amp;src=http%3A%2F%2Fc4.haibao.cn%2Fimg%2F600_0_100_0%2F1473652712.0005%2F87c7805c10e60e9a6db94f86d6014de8.jpg"</span>;

    <span class="hljs-comment">// 人脸检测定位</span>
    DetectFace(imageUrl_1);
    <span class="hljs-comment">// 人脸属性识别</span>
    GetFaceAttribute(imageUrl_1);
    <span class="hljs-comment">// 人脸对比</span>
    VerifyFace(imageUrl_1,imageUrl_2);
}

<span class="hljs-comment">/**
 * DetectFace API 人脸检测定位
 *
 * @param imageUrl  检测人脸图片的URL
 */</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> DetectFace(<span class="hljs-built_in">String</span> imageUrl) {
    CommonRequest request = <span class="hljs-keyword">new</span> CommonRequest();
    request.setMethod(MethodType.POST);
    request.setDomain(<span class="hljs-string">"face.cn-shanghai.aliyuncs.com"</span>);
    request.setVersion(<span class="hljs-string">"2018-12-03"</span>);
    request.setAction(<span class="hljs-string">"DetectFace"</span>);
    request.putBodyParameter(<span class="hljs-string">"ImageUrl"</span>, imageUrl);

// request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA..."); //检测图片的内容,Base64编码

    CommonResponse response = <span class="hljs-literal">null</span>;
    <span class="hljs-keyword">try</span> {
        response = client.getCommonResponse(request);
    } <span class="hljs-keyword">catch</span> (ClientException e) {
        e.printStackTrace();
    }
    System.out.println(response.getData());
}

<span class="hljs-comment">/**
 * GetFaceAttribute API 人脸属性识别
 *
 * @param imageUrl  检测人脸图片的URL
 */</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> GetFaceAttribute(<span class="hljs-built_in">String</span> imageUrl) {
    CommonRequest request = <span class="hljs-keyword">new</span> CommonRequest();
    request.setMethod(MethodType.POST);
    request.setDomain(<span class="hljs-string">"face.cn-shanghai.aliyuncs.com"</span>);
    request.setVersion(<span class="hljs-string">"2018-12-03"</span>);
    request.setAction(<span class="hljs-string">"GetFaceAttribute"</span>);
    request.putBodyParameter(<span class="hljs-string">"ImageUrl"</span>, imageUrl);

// request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA..."); //检测图片的内容,Base64编码

    CommonResponse response = <span class="hljs-literal">null</span>;
    <span class="hljs-keyword">try</span> {
        response = client.getCommonResponse(request);
    } <span class="hljs-keyword">catch</span> (ClientException e) {
        e.printStackTrace();
    }
    System.out.println(response.getData());
}

<span class="hljs-comment">/**
 * VerifyFace API 人脸比对
 *
 * @param imageUrl_1 对比人脸图片1
 * @param imageUrl_2 对比人脸图片2
 */</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> VerifyFace(<span class="hljs-built_in">String</span> imageUrl_1, <span class="hljs-built_in">String</span> imageUrl_2) {
    CommonRequest request = <span class="hljs-keyword">new</span> CommonRequest();
    request.setMethod(MethodType.POST);
    request.setDomain(<span class="hljs-string">"face.cn-shanghai.aliyuncs.com"</span>);
    request.setVersion(<span class="hljs-string">"2018-12-03"</span>);
    request.setAction(<span class="hljs-string">"VerifyFace"</span>);
    request.putBodyParameter(<span class="hljs-string">"ImageUrl1"</span>, imageUrl_1);
    request.putBodyParameter(<span class="hljs-string">"ImageUrl2"</span>, imageUrl_2);

// request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA..."); //检测图片的内容,Base64编码

    CommonResponse response = <span class="hljs-literal">null</span>;
    <span class="hljs-keyword">try</span> {
        response = client.getCommonResponse(request);
    } <span class="hljs-keyword">catch</span> (ClientException e) {
        e.printStackTrace();
    }
    System.out.println(response.getData());
}

}


3、测试结果

{"Data":{"rectA":[679,539,1172,1664],"thresholds":[61.0,69.0,75.0],"rectB":[208,103,152,218],"request_id":"","errno":0,"confidence":70.725196838378906},"RequestId":"64320CB0-325A-4BDA-A127-EC3E686DE6F6","Success":true}

参考链接


阿里云人脸识别综述

上一篇:HTTP协议的介绍,Web服务器配置,虚拟主机的配置,如何用SSL实现HTTPS。


下一篇:Linux Kernel Development----系统调用