阿里云第三方文章垃圾内容审核
作者:互联网
一、导入maven依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.29</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ocr</artifactId>
<version>1.0.11</version>
</dependency>
<!-- 内容处理-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-imageaudit</artifactId>
<version>1.0.8</version>
</dependency>
<!-- 图像识别-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-imagerecog</artifactId>
<version>1.0.12</version>
</dependency>
二、根据官方文档案例测试代码:可根据自己的需求自行添加
package com.xiaoxu.admin.kafka.aliyun;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.RpcAcsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest.Task;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageResponse;
import com.aliyuncs.imageaudit.model.v20191230.ScanTextRequest;
import com.aliyuncs.imageaudit.model.v20191230.ScanTextResponse;
import com.aliyuncs.imagerecog.model.v20190930.RecognizeSceneRequest;
import com.aliyuncs.imagerecog.model.v20190930.RecognizeSceneResponse;
import com.aliyuncs.imagerecog.model.v20190930.TaggingImageRequest;
import com.aliyuncs.imagerecog.model.v20190930.TaggingImageResponse;
import com.aliyuncs.ocr.model.v20191230.*;
import com.aliyuncs.profile.DefaultProfile;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ImageTest {
static IAcsClient client = null;
public static void main(String[] args) throws Exception {
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai", //默认
"", //您的AccessKeyID
""); //您的AccessKeySecret
client = new DefaultAcsClient(profile);
// 内容审核
// testScanImage(); // 内容审核
testScanText();
System.out.println("--------------------------------------------------------------");
}
private static <R extends RpcAcsRequest<T>, T extends AcsResponse> T getAcsResponse(R req) throws Exception {
try {
return client.getAcsResponse(req);
} catch (ServerException e) {
// 服务端异常
System.out.println(String.format("ServerException: errCode=%s, errMsg=%s", e.getErrCode(), e.getErrMsg()));
throw e;
} catch (ClientException e) {
// 客户端错误
System.out.println(String.format("ClientException: errCode=%s, errMsg=%s", e.getErrCode(), e.getErrMsg()));
throw e;
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
throw e;
}
}
public static void testScanImage() throws Exception {
System.out.println("-------- 内容审核 --------------");
ScanImageRequest req = new ScanImageRequest();
List<String> scenes = new ArrayList<String>();
scenes.add("porn");
req.setScenes(scenes);
List<Task> tasks = new ArrayList<Task>();
com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest.Task task = new Task();
task.setDataId(UUID.randomUUID().toString());
task.setImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png");
tasks.add(task);
req.setTasks(tasks);
ScanImageResponse resp = getAcsResponse(req);
printResponse(req.getSysActionName(), resp.getRequestId(), resp);
}
public static void testScanText() throws Exception {
System.out.println("-------- 文章内容审核 --------------");
ScanTextRequest req = new ScanTextRequest();
List<ScanTextRequest.Labels> labelss = new ArrayList<>();
ScanTextRequest.Labels label1 = new ScanTextRequest.Labels();
label1.setLabel("politics");
labelss.add(label1);
ScanTextRequest.Labels label2 = new ScanTextRequest.Labels();
label2.setLabel("abuse");
labelss.add(label2);
ScanTextRequest.Labels label3 = new ScanTextRequest.Labels();
label3.setLabel("terrorism");
labelss.add(label3);
ScanTextRequest.Labels label4 = new ScanTextRequest.Labels();
label4.setLabel("porn");
labelss.add(label4);
ScanTextRequest.Labels label5 = new ScanTextRequest.Labels();
label5.setLabel("ad");
labelss.add(label5);
req.setLabelss(labelss);
List<ScanTextRequest.Tasks> tasks = new ArrayList<>();
ScanTextRequest.Tasks task1 = new ScanTextRequest.Tasks();
task1.setContent("本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。联系weixin 123456");
tasks.add(task1);
req.setTaskss(tasks);
ScanTextResponse response = getAcsResponse(req);
System.out.println(JSON.toJSONString(response));
}
//通用图像打标
public static void testTaggingImage() throws Exception {
System.out.println("-------- 通用图像打标 --------------");
TaggingImageRequest req = new TaggingImageRequest();
// 注意:下面的链接换成自有的oss链接
req.setImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/xxx.png");
TaggingImageResponse resp = getAcsResponse(req);
printResponse(req.getSysActionName(), resp.getRequestId(), resp);
}
public static void printResponse(String actionName, String requestId, AcsResponse data) {
// System.out.println(String.format("actionName=%s, requestId=%s", actionName, requestId));
System.out.println( String.format("data=%s",JSON.toJSONString(data)) );
}
}
三、测试结果
标签:aliyuncs,req,ScanTextRequest,阿里,垃圾,new,import,com,第三方 来源: https://blog.csdn.net/qq_44798321/article/details/123612418