作者:俏巴
概述
基於圖像或視頻輸入進行檢測,與註冊庫比對,實現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>
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>com.aliyun<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span></span>
<artifactId>aliyun-java-sdk-core<<span class="hljs-regexp">/artifactId>
<version>4.1.1</</span>version>
<span class="xml"><span class="hljs-tag"></<span class="hljs-name">dependency</span>></span></span></code></pre>
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 {
<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) throws ClientException {
<span class="hljs-built_in">String</span> groupName = <span class="hljs-string">"JavaDemo1"</span>;
<span class="hljs-built_in">String</span> person = <span class="hljs-string">"LiuYifei"</span>;
<span class="hljs-built_in">String</span> image_1 = <span class="hljs-string">"photo1"</span>;
<span class="hljs-built_in">String</span> image_2 = <span class="hljs-string">"photo2"</span>;
<span class="hljs-built_in">String</span> imageUrl_1 = <span class="hljs-string">"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"</span>;
<span class="hljs-built_in">String</span> imageUrl_2 = <span class="hljs-string">"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"</span>;
<span class="hljs-built_in">String</span> recognizeFaceImageUrl = <span class="hljs-string">"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"</span>;
// //添加入兩張人臉
AddFace(groupName,person,image_1,imageUrl_1);
AddFace(groupName,person,image_2,imageUrl_2);
<span class="hljs-comment">//列舉Group</span>
ListGroup();
<span class="hljs-comment">//列舉Faces</span>
ListFace(groupName);
<span class="hljs-comment">//人臉查詢</span>
RecognizeFace(recognizeFaceImageUrl, groupName);
<span class="hljs-comment">//刪除Face</span>
DeleteFace(groupName,person,image_1);
<span class="hljs-comment">//列舉Faces查詢刪除情況</span>
ListFace(groupName);
}
<span class="hljs-comment">/**
* AddFace接口用於向人臉庫中添加人臉
* @param groupName 添加人臉的分組
* @param person 添加人臉的姓名
* @param image 添加人臉的編號
* @param imageUrl 檢測圖片的URL
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> AddFace(<span class="hljs-built_in">String</span> groupName,<span class="hljs-built_in">String</span> person,<span class="hljs-built_in">String</span> image,<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">"AddFace"</span>);
request.putBodyParameter(<span class="hljs-string">"Group"</span>, groupName);
request.putBodyParameter(<span class="hljs-string">"Person"</span>, person);
request.putBodyParameter(<span class="hljs-string">"Image"</span>, image);
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">/**
* DeleteFace接口用於從人臉庫中刪除人臉
* @param groupName 添加人臉的分組
* @param person 添加人臉的姓名
* @param image 添加人臉的編號
* @throws ClientException
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> DeleteFace(<span class="hljs-built_in">String</span> groupName,<span class="hljs-built_in">String</span> person,<span class="hljs-built_in">String</span> image) throws ClientException {
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">"DeleteFace"</span>);
request.putBodyParameter(<span class="hljs-string">"Group"</span>, groupName);
request.putBodyParameter(<span class="hljs-string">"Person"</span>, person);
request.putBodyParameter(<span class="hljs-string">"Image"</span>, image);
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
}
<span class="hljs-comment">/**
* ListFace接口用於列舉註冊庫中的人臉
* @param groupName 需要查詢的庫
* @throws ClientException
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> ListFace(<span class="hljs-built_in">String</span> groupName) throws ClientException {
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">"ListFace"</span>);
request.putBodyParameter(<span class="hljs-string">"Group"</span>, groupName);
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
}
<span class="hljs-comment">/**
* ListGroup接口用於列舉人臉組
* @throws ClientException
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> ListGroup() throws ClientException {
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">"ListGroup"</span>);
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
}
<span class="hljs-comment">/**
* RecognizeFace接口用於查找註冊庫中的人臉
* @param recognizeFaceImageUrl 需要查詢的人類圖片URL
* @throws ClientException
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> RecognizeFace(<span class="hljs-built_in">String</span> recognizeFaceImageUrl, <span class="hljs-built_in">String</span> groupName) throws ClientException {
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">"RecognizeFace"</span>);
request.putBodyParameter(<span class="hljs-string">"Group"</span>, groupName);
request.putBodyParameter(<span class="hljs-string">"ImageUrl"</span>, 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}
參考鏈接
人臉查找1:N SDK調用說明
人臉查找1:N API調用說明