本示例介绍如何使用Alibaba Cloud SDK for Java帮助您检测图片中是否含有风险内容。
背景信息
- 图片审核支持同步检测和异步检测两种方式。同步检测实时返回检测结果;异步检测需要您轮询结果或者通过callback回调通知获取检测结果。
- 图片支持传入互联网图片URL、本地图片文件路径以及二进制图片文件流。
前提条件
在使用本教程之前,请确保已完成以下操作:
- 使用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 java.util.Arrays; import java.util.Date; import java.util.UUID; public class Demo { public static void main(String[] args) { DefaultProfile 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(); // 设置要检测的场景, 计费是按照该处传递的场景进行 // 一次请求中可以同时检测多张图片,每张图片可以同时检测多个风险场景,计费按照场景计算 // 例如:检测2张图片,场景传递porn、terrorism,计费会按照2张图片鉴黄,2张图片暴恐检测计算 // porn: porn表示色情场景检测 httpBody.put("scenes", Arrays.asList("porn")); // 设置待检测图片, 一张图片一个task // 多张图片同时检测时,处理的时间由最后一个处理完的图片决定 // 通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高 // 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task JSONObject task = new JSONObject(); task.put("dataId", UUID.randomUUID().toString()); // 设置图片链接 task.put("url", "https://www.baidu.com/img/bd_logo1.png"); task.put("time", new Date()); httpBody.put("tasks", Arrays.asList(task)); request.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON); // 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置 // 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout异常 request.setSysConnectTimeout(3000); request.setSysReadTimeout(10000); CommonResponse httpResponse = null; 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 org.apache.commons.codec.binary.StringUtils; import org.apache.commons.io.FileUtils; import java.io.File; import java.util.Arrays; import java.util.Date; import java.util.UUID; public class Demo { public static void main(String[] args) { DefaultProfile 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(); // 设置要检测的场景, 计费是按照该处传递的场景进行 // 一次请求中可以同时检测多张图片,每张图片可以同时检测多个风险场景,计费按照场景计算 // 例如:检测2张图片,场景传递porn、terrorism,计费会按照2张图片鉴黄,2张图片暴恐检测计算 // porn: porn表示色情场景检测 httpBody.put("scenes", Arrays.asList("porn")); // 如果您要检测的文件存于本地服务器上,可以通过下述代码片生成url // 再将返回的url作为图片地址传递到服务端进行检测 String url = null; byte[] imageBytes = null; ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false); try{ imageBytes = FileUtils.readFileToByteArray(new File("/Users.jpg")); //上传到服务端 url = clientUploader.uploadBytes(imageBytes); }catch (Exception e){ e.printStackTrace(); } // 设置待检测图片, 一张图片一个task // 多张图片同时检测时,处理的时间由最后一个处理完的图片决定 // 通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高 // 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task JSONObject task = new JSONObject(); task.put("dataId", UUID.randomUUID().toString()); //设置图片链接为上传后的url task.put("url", url); task.put("time", new Date()); httpBody.put("tasks", Arrays.asList(task)); request.setHttpContent(StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON); // 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置 // 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout异常 request.setSysConnectTimeout(3000); request.setSysReadTimeout(10000); CommonResponse httpResponse = null; 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,
"dataId":"34061ac3-5f0f-4d19-b163-c05ea013cbc5",
"extras":{},
"results":[
{
"rate":100.0,
"suggestion":"pass",
"label":"normal",
"scene":"porn"
}
],
"taskId":"img6Ma******Eg-1rG71w",
"url":"http://aligre*****hanghai"
}
],
"requestId":"212963FE-FBFF-40DC-B86D-A8E192738DC6"
}
在文档使用中是否遇到以下问题
更多建议
匿名提交