本筆記內(nèi)容為黑馬頭條項目的文本-圖片內(nèi)容審核接口部分
目錄
一、概述
二、準備工作
三、文本內(nèi)容審核接口
四、圖片審核接口
五、項目集成
一、概述
內(nèi)容安全是識別服務,支持對圖片、視頻、文本、語音等對象進行多樣化場景檢測,有效降低內(nèi)容違規(guī)風險。
目前很多平臺都支持內(nèi)容檢測,如阿里云、騰訊云、百度AI、網(wǎng)易云等國內(nèi)大型互聯(lián)網(wǎng)公司都對外提供了API。
按照性能和收費來看,黑馬頭條項目使用的就是阿里云的內(nèi)容安全接口,使用到了圖片和文本的審核。
阿里云收費標準:價格計算器 (aliyun.com)
二、準備工作
您在使用內(nèi)容檢測API之前,需要先注冊阿里云賬號,添加Access Key并簽約云盾內(nèi)容安全。 ?
操作步驟
1、前往阿里云官網(wǎng)注冊賬號。如果已有注冊賬號,請?zhí)^此步驟。
進入阿里云首頁后,如果沒有阿里云的賬戶需要先進行注冊,才可以進行登錄。由于注冊較為簡單,課程和講義不在進行體現(xiàn)(注冊可以使用多種方式,如淘寶賬號、支付寶賬號、微博賬號等...)。需要實名認證和活體認證。
2、打開云盾內(nèi)容安全產(chǎn)品試用頁面,單擊立即開通,正式開通服務。
注意 :現(xiàn)在這個服務需要企業(yè)認證才能使用?
所以我改用了阿里視覺智能開發(fā)平臺的審核功能視覺智能開放平臺-控制臺 (aliyun.com)
購買資源包,我第一次購買不用錢,免費送了1萬點
3、在AccessKey管理頁面管理您的AccessKeyID和AccessKeySecret。
管理自己的AccessKey,可以新建和刪除AccessKey
查看自己的AccessKey,AccessKey默認是隱藏的,第一次申請的時候可以保存AccessKey,點擊顯示,通過驗證手機號后也可以查看
三、文本內(nèi)容審核接口
文本垃圾內(nèi)容檢測: 文本內(nèi)容安全 (aliyun.com)
文本垃圾內(nèi)容Java SDK: Java (aliyun.com)
四、圖片審核接口
圖片垃圾內(nèi)容檢測:圖片內(nèi)容安全 (aliyun.com)
圖片垃圾內(nèi)容Java SDK:Java (aliyun.com)
五、項目集成
這里的代碼是使用智能開發(fā)平臺的審核功能
①:common模塊下面添加工具類,并添加到自動配置
pom依賴
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imageaudit20191230</artifactId>
<version>2.0.6</version>
</dependency>
aliyun包下的GreenImageScan.java和GreenTextScan.java
GreenImageScan代碼
package com.heima.common.aliyun;
import com.alibaba.fastjson.JSON;
import com.aliyun.imageaudit20191230.models.ScanImageRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.imageaudit20191230.models.ScanImageResponseBody;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.*;
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class GreenImageScan {
private String accessKeyId;
private String secret;
private String scenes;
public Map imageScan(List<String> imageList) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(secret);
// 訪問的域名
config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
com.aliyun.imageaudit20191230.Client client = new com.aliyun.imageaudit20191230.Client(config);
List<ScanImageRequest.ScanImageRequestTask> taskList = new ArrayList<>();
for (String img: imageList) {
ScanImageRequest.ScanImageRequestTask task = new ScanImageRequest.ScanImageRequestTask();
task.setImageURL(img);
task.setDataId(UUID.randomUUID().toString());
task.setImageTimeMillisecond(1L);
task.setInterval(1);
task.setMaxFrames(1);
taskList.add(task);
}
//場景
List<String> sceneList = new ArrayList<>();
sceneList.add(scenes);
sceneList.add("logo");
sceneList.add("porn");
com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
.setTask(taskList)
.setScene(sceneList);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
ScanImageResponse scanImageResponse = client.scanImageWithOptions(scanImageRequest, runtime);
Map<String, String> resultMap = new HashMap<>();
if (scanImageResponse.getStatusCode() == 200) {
List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> subResults = scanImageResponse.body.data.results.get(0).getSubResults();
ListIterator<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> listIterator = subResults.listIterator();
while (listIterator.hasNext()) {
ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults item = listIterator.next();
System.out.println("scene = [" + item.scene + "]");
System.out.println("suggestion = [" + item.suggestion + "]");
System.out.println("label = [" + item.label + "]");
if (!item.suggestion.equals("pass")) {
resultMap.put("suggestion", item.suggestion);
resultMap.put("label", item.label);
return resultMap;
}
}
resultMap.put("suggestion", "pass");
} else {
/* *
* 表明請求整體處理失敗,原因視具體的情況詳細分析
*/
System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scanImageResponse));
return null;
}
} catch (com.aliyun.tea.TeaException teaException) {
// 獲取整體報錯信息
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// 獲取單個字段
System.out.println(teaException.getCode());
}
return null;
/* Map<String, String> resultMap = new HashMap<>();
resultMap.put("suggestion", "pass");
return resultMap;*/
}
}
?GreenTextScan代碼
package com.heima.common.aliyun;
import com.aliyun.imageaudit20191230.*;
import com.aliyun.imageaudit20191230.models.ScanTextRequest;
import com.aliyun.imageaudit20191230.models.ScanTextResponse;
import com.aliyun.imageaudit20191230.models.ScanTextResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.*;
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class GreenTextScan {
private String accessKeyId;
private String secret;
public Map greeTextScan(String content) throws Exception {
/*
初始化配置對象com.aliyun.teaopenapi.models.Config
Config對象存放 AccessKeyId、AccessKeySecret、endpoint等配置
*/
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(secret);
// 訪問的域名
config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
Client client = new Client(config);
ScanTextRequest.ScanTextRequestTasks tasks = new ScanTextRequest.ScanTextRequestTasks()
.setContent(content); //審核內(nèi)容
ScanTextRequest.ScanTextRequestLabels labels = new ScanTextRequest.ScanTextRequestLabels()
.setLabel("abuse"); //設置審核類型
ScanTextRequest scanTextRequest = new ScanTextRequest()
.setLabels(java.util.Arrays.asList(
labels
))
.setTasks(java.util.Arrays.asList(
tasks
));
RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
Map<String, String> resultMap = new HashMap<>();
try {
// 復制代碼運行請自行打印API的返回值
ScanTextResponse response = client.scanTextWithOptions(scanTextRequest, runtime);
//System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
if (response.getStatusCode() == 200) {
ScanTextResponseBody.ScanTextResponseBodyDataElementsResults elementsResults = response.body.getData().elements.get(0).results.get(0);
if (!elementsResults.suggestion.equals("pass")) {
resultMap.put("suggestion", elementsResults.suggestion);
resultMap.put("label", elementsResults.label);
return resultMap;
}
resultMap.put("suggestion", "pass");
return resultMap;
} else {
return null;
}
} catch (TeaException error) {
// 獲取整體報錯信息
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// 獲取單個字段
System.out.println(error.getCode());
error.printStackTrace();
}
return null;
}
}
添加到自動配置中
②: accessKeyId和secret(需自己申請)
在heima-leadnews-wemedia中的nacos配置中心添加以下配置:
aliyun:
?accessKeyId: LTAI5tCWHCcfvqQzu8k2oKmX
?secret: auoKUFsghimbfVQHpy7gtRyBkoR4vc
#aliyun.scenes=porn,terrorism,ad,qrcode,live,logo
?scenes: terrorism
③:在自媒體微服務中測試類中注入審核文本和圖片的bean進行測試文章來源:http://www.zghlxwxcb.cn/news/detail-611010.html
package com.heima.wemedia;
import com.heima.common.aliyun.GreenImageScan;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.file.service.FileStorageService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@SpringBootTest(classes = WemediaApplication.class)
@RunWith(SpringRunner.class)
public class AliyunTest {
@Autowired
private GreenTextScan greenTextScan;
@Autowired
private GreenImageScan greenImageScan;
@Autowired
private FileStorageService fileStorageService;
@Test
public void testScanText() throws Exception {
Map map = greenTextScan.greeTextScan("王天剛去飯店吃飯后發(fā)現(xiàn)自己的車子被刮了,破口大罵是哪個傻逼干的?");
System.out.println(map);
}
@Test
public void testScanImage() throws Exception {
List<String> list=new ArrayList<>();
list.add("http://192.168.200.130:9000/leadnews/2021/04/26/ef3cbe458db249f7bd6fb4339e593e55.jpg");
Map map = greenImageScan.imageScan(list);
System.out.println(map);
}
}
結束!文章來源地址http://www.zghlxwxcb.cn/news/detail-611010.html
到了這里,關于【黑馬頭條之內(nèi)容安全第三方接口】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!