java阿里云图片检测同/异步sdk调用详解
java阿里云图片检测同/异步sdk调用
sdk调用流程:注册阿里云账号→创建RAM子账号→子账号授权→复制子账号的AccessKey ID&Access Key Secret→sdk&api调用
阿里云RAM子账号创建、授权、api文档详情查看
注意:调用阿里云接口的时候最容易出错的俩个地方
1.AccessKey ID&Access Key Secret
因为阿里云是主张接口调用都是给子账号权限然后用子账号的AccessKey ID&Access Key Secret来调用接口,所有我们复制这俩个参数的时候很容易复制成我们主账号的这俩参数去了,切记一定要复制子账号的这俩参数!!
2.权限添加
很多时候我们所有步骤都作对了就是连不通的原因,就是子账号的权限没给,如我现在要调文本反垃圾的接口但是我没给子账号这个权限是无论如何都调不通的切记!!
阿里云不仅提供了api还提供了sdk,有了sdk我们就方便多了,阿里云帮我们封装了所有的方法我们只需要复制粘贴就能用这些接口了,但是前提得注意我上面说的一些注意事项切记嗷java阿里云sdk地址
1.同步图片检测:同步检测图片中的违规内容,实时返回检测结果。
支持检测的场景包括:图片智能鉴黄、图片暴恐涉政识别、图片广告识别、图片二维码识别、图片不良场景识别、图片logo识别。
阿里云图片同步检测接口详情及请求、返回参数详情查看
1):同步图片检测代码如下:
/**
* 图片同步检测
*
* @throws Exception
*/
@org.junit.Test
public void aliyunImageSyncCheck() throws Exception {
//请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile(ConfigAliyun.regionId, ConfigAliyun.accessKeyId, ConfigAliyun.accessKeySecret);
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(ConfigAliyun.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());
//https://img.alicdn.com/tfs/TB1Xk_qvwmTBuNjy1XbXXaMrVXa-550-407.jpg
task.put("url", "https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg");
task.put("time", new Date());
tasks.add(task);
JSONObject data = new JSONObject();
/**
* porn: 色情
* terrorism: 暴恐
* qrcode: 二维码
* ad: 图片广告
* ocr: 文字识别
*/
data.put("scenes", Arrays.asList("porn", "terrorism"));
data.put("tasks", tasks);
imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageSyncScanRequest.setConnectTimeout(3000);
imageSyncScanRequest.setReadTimeout(10000);
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("scene = [" + scene + "]");
System.out.println("suggestion = [" + 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();
}
}
2):同步图片检测结果如下:
{
"msg":"OK",
"code":200,
"data":[
{
"msg":"OK",
"code":200,
"dataId":"3d583265-d9cd-4159-83d8-68e8a2616ddf",
"extras":{
},
"results":[
{
"rate":99.700005,
"suggestion":"block",
"label":"porn",
"scene":"porn"
},
{
"rate":99.99,
"suggestion":"pass",
"label":"normal",
"scene":"terrorism"
}
],
"taskId":"imgitaNeo8XlI5fNtKt93rzT-1r9AuE",
"url":"https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg"
}
],
"requestId":"0C0A9B1C-BB89-47B8-8AB8-7FE7BA65D31D"
}
scene = [porn]
suggestion = [block]
scene = [terrorism]
suggestion = [pass]
2.异步图片检测:提交图片异步检测任务,检测图片违规或识别图片中的不良信息。
支持检测的场景包括:图片智能鉴黄、图片暴恐涉政识别、图片广告识别、图片二维码识别、图片不良场景识别、图片logo识别。
异步检测任务不会实时返回检测结果,您需要通过轮询的方式或者callback的方式获取检测结果。。
阿里云图片异步检测接口详情及请求、返回参数详情查看
1):异步图片检测代码如下:
@org.junit.Test
public void aliyunImagecheck()throws Exception{
//图片url
String srcUrl = "http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg";
System.out.println("srcUrl:"+srcUrl);
//获取图片异步检测taskId
String taskId = aliyunImageASyncCheck(srcUrl);
System.out.println("taskId:"+taskId);
//根据taskId查询图片异步检测结果
String imgCheckResult = aliyunImageAsyncResultCheck(taskId);
System.out.println("imgCheckResult:"+imgCheckResult);
}
/**
* 图片异步检测
* @throws Exception
*/
public String aliyunImageASyncCheck(String srcUrl) throws Exception {
String result = "";
//请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanRequest imageAsyncScanRequest = new ImageAsyncScanRequest();
imageAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageAsyncScanRequest.setEncoding("utf-8");
imageAsyncScanRequest.setRegionId(ConfigAliyun.regionId);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("dataId", UUID.randomUUID().toString());
//"http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg"
task1.put("url", srcUrl);
task1.put("time", new Date());
tasks.add(task1);
JSONObject data = new JSONObject();
/**
* porn: 色情
* terrorism: 暴恐
* qrcode: 二维码
* ad: 图片广告
* ocr: 文字识别
*/
data.put("scenes", Arrays.asList("porn", "ocr", "qrcode", "sface"));
data.put("tasks", tasks);
imageAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageAsyncScanRequest.setConnectTimeout(3000);
imageAsyncScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncScanRequest);
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")) {
String taskId = ((JSONObject) taskResult).getString("taskId");
// 将taskId 保存下来,间隔一段时间来轮询结果, 参照ImageAsyncScanResultsRequest
System.out.println("args = [" + taskId + "]");
result = taskId;
} 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();
}
return result;
}
/**
* 图片异步检测结果查询
* @throws Exception
*/
public static String aliyunImageAsyncResultCheck(String taskId)throws Exception{
String result = "";
//请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanResultsRequest imageAsyncScanResultsRequest = new ImageAsyncScanResultsRequest();
imageAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageAsyncScanResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageAsyncScanResultsRequest.setEncoding("utf-8");
imageAsyncScanResultsRequest.setRegionId(ConfigAliyun.regionId);
List<String> taskIds = new ArrayList<String>();
taskIds.add(taskId);
imageAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageAsyncScanResultsRequest.setConnectTimeout(3000);
imageAsyncScanResultsRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncScanResultsRequest);
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");
result = suggestion;
//根据scene和suggetion做相关的处理
//do something
}
}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();
}
return result;
}
2):异步图片检测结果如下:
srcUrl:http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg
{
"msg":"OK",
"code":200,
"data":[
{
"msg":"OK",
"code":200,
"dataId":"a0285d1c-d423-4fd3-a9ed-3005725b88e4",
"taskId":"img3QGf$fM4AzB4UUroFJI2GF-1r9AKR",
"url":"http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg"
}
],
"requestId":"DF984416-28D6-4088-B4BE-7552481DCB4E"
}
args = [img3QGf$fM4AzB4UUroFJI2GF-1r9AKR]
taskId:img3QGf$fM4AzB4UUroFJI2GF-1r9AKR
imgCheckResult:review
本文来自互联网用户投稿,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。该文观点仅代表作者本人,不代表本站立场。
如若内容造成侵权/违法违规/事实不符,请联系站长邮箱:luowen@yiyuen.com 进行投诉反馈,一经查实,立即删除!
如若转载,请注明出处:http://kaifa.yiyen.com/kaifa/article/d9abb5390dfc4a8aa5fd0e56cdecaa64
评论