本示例介绍如何使用Alibaba Cloud SDK for Java调用图片同步检测任务进行人脸1:1比对。
前提条件
在使用本教程之前,请确保已完成以下操作:
- 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
- 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)。
<dependencies> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-green</artifactId> <version>3.5.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-oss --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.8.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.61</version> </dependency> </dependencies>
- 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
代码示例
- 传入URL示例代码:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.MethodType; import com.aliyuncs.http.ProtocolType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.Arrays; public class Test { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile( "<your-region-id>", // 您的可用区ID "<your-access-key-id>", // 您的AccessKey ID "<your-access-key-secret>"); // 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); // 指定请求方式 request.setSysMethod(MethodType.POST); // 指定请求地址 request.setSysDomain("green.cn-hangzhou.aliyuncs.com"); // 指定请求方法 request.setSysUriPattern("/green/image/scan"); // 指定请求版本 request.setSysVersion("2018-05-09"); // 支持HTTP和HTTPS request.setSysProtocol(ProtocolType.HTTPS); JSONObject httpBody = new JSONObject(); httpBody.put("scenes", Arrays.asList("sface-1")); JSONObject task = new JSONObject(); //设置待比对人脸图片1链接 task.put("url","http://pic1****1/5acd7c52f192e.jpg"); JSONObject extras = new JSONObject(); //设置待比对人脸图片2链接 extras.put("faceUrl", "https://******pf04191c17.jpg"); task.put("extras", extras); httpBody.put("tasks", Arrays.asList(task)); request.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON); // 请务必设置超时时间 request.setSysConnectTimeout(3000); request.setSysReadTimeout(10000); try { CommonResponse response = client.getCommonResponse(request); JSONObject dataJson = JSON.parseObject(response.getData()); System.out.println(JSON.toJSONString(dataJson, true)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }
- 传入本地文件示例代码:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.green.extension.uploader.ClientUploader; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.MethodType; import com.aliyuncs.http.ProtocolType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.Arrays; public class Test { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile( "<your-region-id>", // 您的可用区ID "<your-access-key-id>", // 您的AccessKey ID "<your-access-key-secret>"); // 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); // 指定请求方式 request.setSysMethod(MethodType.POST); // 指定请求地址 request.setSysDomain("green.cn-hangzhou.aliyuncs.com"); // 指定请求方法 request.setSysUriPattern("/green/image/scan"); // 指定请求版本 request.setSysVersion("2018-05-09"); // 支持HTTP和HTTPS request.setSysProtocol(ProtocolType.HTTPS); JSONObject httpBody = new JSONObject(); httpBody.put("scenes", Arrays.asList("sface-1")); JSONObject task = new JSONObject(); ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false); //设置待比对人脸图片1链接 String url1 = null; try{ url1 = clientUploader.uploadFile("C:/1.jpg"); }catch (Exception e){ e.printStackTrace(); } task.put("url",url1); // 传入待比较图片中的第二张图片 // 格式为{"faceUrl":"http://xxx.jpg"}。其中,faceUrl为第二张图片的URL JSONObject extras = new JSONObject(); //设置待比对人脸图片2链接 String url2 = null; try{ url2 = clientUploader.uploadFile("C:/2.jpg"); }catch (Exception e){ e.printStackTrace(); } extras.put("faceUrl", url2); task.put("extras", extras); httpBody.put("tasks", Arrays.asList(task)); request.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON); // 请务必设置超时时间 request.setSysConnectTimeout(3000); request.setSysReadTimeout(10000); try { CommonResponse response = client.getCommonResponse(request); JSONObject dataJson = JSON.parseObject(response.getData()); System.out.println(JSON.toJSONString(dataJson, true)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }
执行结果
以上代码示例正确的返回结果类似如下:
{
"msg":"OK",
"code":200,
"data":[
{
"msg":"OK",
"code":200,
"extras":{},
"results":[
{
"rate":59.0,
"suggestion":"review",
"label":"sface-1",
"scene":"sface-1"
}
],
"taskId":"img6G****m-1rGL3S",
"url":"http://pi*******d7c52f192e.jpg"
}
],
"requestId":"F2D1CBAE-1DB6-44E7-9580-D3B75758154E"
}
在文档使用中是否遇到以下问题
更多建议
匿名提交