可以自己做網(wǎng)站優(yōu)化嗎體驗(yàn)式營銷經(jīng)典案例
本筆記內(nèi)容為黑馬頭條項(xiàng)目的文本-圖片內(nèi)容審核接口部分
目錄
一、概述
二、準(zhǔn)備工作
三、文本內(nèi)容審核接口
四、圖片審核接口
五、項(xiàng)目集成
一、概述
內(nèi)容安全是識別服務(wù),支持對圖片、視頻、文本、語音等對象進(jìn)行多樣化場景檢測,有效降低內(nèi)容違規(guī)風(fēng)險。
目前很多平臺都支持內(nèi)容檢測,如阿里云、騰訊云、百度AI、網(wǎng)易云等國內(nèi)大型互聯(lián)網(wǎng)公司都對外提供了API。
按照性能和收費(fèi)來看,黑馬頭條項(xiàng)目使用的就是阿里云的內(nèi)容安全接口,使用到了圖片和文本的審核。
阿里云收費(fèi)標(biāo)準(zhǔn):價格計算器 (aliyun.com)
二、準(zhǔn)備工作
您在使用內(nèi)容檢測API之前,需要先注冊阿里云賬號,添加Access Key并簽約云盾內(nèi)容安全。 ?
操作步驟
1、前往阿里云官網(wǎng)注冊賬號。如果已有注冊賬號,請?zhí)^此步驟。
進(jìn)入阿里云首頁后,如果沒有阿里云的賬戶需要先進(jìn)行注冊,才可以進(jìn)行登錄。由于注冊較為簡單,課程和講義不在進(jìn)行體現(xiàn)(注冊可以使用多種方式,如淘寶賬號、支付寶賬號、微博賬號等...)。需要實(shí)名認(rèn)證和活體認(rèn)證。
2、打開云盾內(nèi)容安全產(chǎn)品試用頁面,單擊立即開通,正式開通服務(wù)。
注意 :現(xiàn)在這個服務(wù)需要企業(yè)認(rèn)證才能使用?
所以我改用了阿里視覺智能開發(fā)平臺的審核功能視覺智能開放平臺-控制臺 (aliyun.com)
購買資源包,我第一次購買不用錢,免費(fèi)送了1萬點(diǎn)
3、在AccessKey管理頁面管理您的AccessKeyID和AccessKeySecret。
管理自己的AccessKey,可以新建和刪除AccessKey
查看自己的AccessKey,AccessKey默認(rèn)是隱藏的,第一次申請的時候可以保存AccessKey,點(diǎn)擊顯示,通過驗(yàn)證手機(jī)號后也可以查看
三、文本內(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)
五、項(xiàng)目集成
這里的代碼是使用智能開發(fā)平臺的審核功能
①:common模塊下面添加工具類,并添加到自動配置
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 {/* ** 表明請求整體處理失敗,原因視具體的情況詳細(xì)分析*/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.ConfigConfig對象存放 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"); //設(shè)置審核類型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 {// 復(fù)制代碼運(yùn)行請自行打印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: LTAI5tCWHCcfvqQzu8k2oKmXsecret: auoKUFsghimbfVQHpy7gtRyBkoR4vc
#aliyun.scenes=porn,terrorism,ad,qrcode,live,logoscenes: terrorism
③:在自媒體微服務(wù)中測試類中注入審核文本和圖片的bean進(jìn)行測試
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 {@Autowiredprivate GreenTextScan greenTextScan;@Autowiredprivate GreenImageScan greenImageScan;@Autowiredprivate FileStorageService fileStorageService;@Testpublic void testScanText() throws Exception {Map map = greenTextScan.greeTextScan("王天剛?cè)ワ埖瓿燥埡蟀l(fā)現(xiàn)自己的車子被刮了,破口大罵是哪個傻逼干的?");System.out.println(map);}@Testpublic 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);}}
結(jié)束!