本文介绍使用Java SDK对给定的人脸图片进行自定义人脸检索的示例代码。
功能描述
自定义人脸检索根据您输入的待识别人脸图片(face),在指定人脸库(group)中查找并返回最相似的Top5个体(person),返回的Top5个体按照相似度从大到小排序。检索人脸时,必须指定要检索的分组信息(最多指定一个分组)。具体参数说明请参考自定义人脸检索API接口描述文档。
准备工作
在进行具体的服务调用之前,请参见以下步骤,完成准备工作:
- 创建阿里云AccessKeyId和AccessKeySecret。具体请参见创建AccessKey。
- 安装Java依赖。具体请参见安装Java依赖。
- 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
人脸图片检索(URL)代码示例
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
/**
* 人脸检索
* @author
* @date 2018/06/25
*/
public class FaceScanRequestSample {
public static void main(String[] args) throws Exception {
//请替换成您自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您自己的accessId", "您自己的accessSecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageSyncScanRequest.setEncoding("utf-8");
imageSyncScanRequest.setRegionId(regionId);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task = new LinkedHashMap<String, Object>();
task.put("dataId", UUID.randomUUID().toString());
task.put("url", "https://img.alicdn.com/tfs/xxx-550-407.jpg");
task.put("time", new Date());
Map<String, String> extras = new HashMap<String, String>();
extras.put("groupId", "java_sdk_test_group");
task.put("extras", extras);
tasks.add(task);
JSONObject data = new JSONObject();
/**
* sface-n: 人脸1-N
*/
data.put("scenes", Arrays.asList("sface-n"));
data.put("tasks", tasks);
imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageSyncScanRequest.setConnectTimeout(3000);
imageSyncScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageSyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if(200 == ((JSONObject)taskResult).getInteger("code")){
JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject)sceneResult).getString("scene");
String suggestion = ((JSONObject)sceneResult).getString("suggestion");
//根据scene和suggetion做相关的处理
//do something
System.out.println("args = [" + scene + "]");
System.out.println("args = [" + suggestion + "]");
}
}else{
System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
人脸图片检索(本地文件)代码示例
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.green.extension.uploader.ClientUploader;
/**
* 人脸检索
* @author
* @date 2018/06/25
*/
public class FaceScanRequestSample {
public static void main(String[] args) throws Exception {
//请替换成您自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您自己的accessId", "您自己的accessSecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageSyncScanRequest.setEncoding("utf-8");
imageSyncScanRequest.setRegionId(regionId);
ClientUploader uploader = ClientUploader.getImageClientUploader(client);
String url = null;
try{
url = uploader.uploadFile("d:/image.jpg");
}catch (Exception e){
e.printStackTrace();
}
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task = new LinkedHashMap<String, Object>();
task.put("dataId", UUID.randomUUID().toString());
task.put("url", url);
task.put("time", new Date());
Map<String, String> extras = new HashMap<String, String>();
extras.put("groupId", "java_sdk_test_group");
task.put("extras", extras);
tasks.add(task);
JSONObject data = new JSONObject();
/**
* sface-n: 人脸1-N
*/
data.put("scenes", Arrays.asList("sface-n"));
data.put("tasks", tasks);
imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageSyncScanRequest.setConnectTimeout(3000);
imageSyncScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageSyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if(200 == ((JSONObject)taskResult).getInteger("code")){
JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject)sceneResult).getString("scene");
String suggestion = ((JSONObject)sceneResult).getString("suggestion");
//根据scene和suggetion做相关的处理
//do something
System.out.println("args = [" + scene + "]");
System.out.println("args = [" + suggestion + "]");
}
}else{
System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
在文档使用中是否遇到以下问题
更多建议
匿名提交