这里以腾讯云为例
一:需求分析
由于我最近在做的是新闻类型的微服务项目,由于互联网的发展,网络上面经常充斥着很多不可控的风险因素,如色情暴力、垃圾广告等,因此发布文章需要进行审核才能发布到App端。如果全部都要人工审核的话会很耗时间,这时候就可以借助第三方提供的服务来对内容进行审核。
二:功能介绍
1.文本内容安全
本内容安全(Text Moderation System,TMS)是一款文本内容智能识别服务,对用户上传的文本进行内容安全识别,能够做到识别准确率高、召回率高,多维度覆盖对内容识别的要求,并实时更新识别服务的识别标准和能力。
2.图片内容安全
图片内容安全(Image Moderation System,IMS)是一款采用前沿的图像识别算法,结合海量的违规图片数据进行训练建模,对用户上传的图片进行内容安全识别的安全服务,能够做到识别准确率高、召回率高,多维度覆盖对内容识别的要求,并不停地更新审核服务的识别标准和能力。
三:功能实现
1.前期工作
(1)开通服务并配置策略
要使用腾讯云的内容安全服务,首先需要注册一个腾讯与账号,并且开通相应的服务(文本内容安全&图片内容安全),然后可以在内容安全控制台创建自己的策略,具体配置教程见官方文档。
(2)导入坐标
我使用的是Java语言进行开发,所以选择接入Java SDK进行接入,导入以下坐标
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
<version>3.1.322</version>
</dependency>
可以设置镜像源以加快下载速度,编辑 Maven 的 settings.xml 配置文件,在 mirrors 段落增加镜像配置。
<mirror>
<id>tencent</id>
<name>tencent maven mirror</name>
<url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
(3)配置腾讯云 API 访问密钥
这两个服务都需要用到腾讯云API的访问密钥,因此使用在接入服务之前需要先申请自己的秘钥,申请地址点击这里。获取密钥之后,便可以将其配置到相应的微服务中,由于我使用了Nacos进行注册管理,这里可以将密钥信息放入Nacos配置中心中:
tencentcloud:
secretId:
secretKey:
2.代码实现
(1)文本内容安全
- 需要的参数:密钥信息、待检测文本(String类型)
- 注意事项:不能直接将文本扔进去检测,检测之前需要对文本进行Base64加密
- 文本内容大小支持:文本原文长度不能超过unicode编码长度10000个字符;
- 文本审核语言支持:目前支持中文、英文、阿拉伯数字的检测;
- 默认接口请求频率限制:1000次/秒,超过该频率限制则接口会报错
- 默认接口请求频率限制:1000次/秒。
- 返回类型:JSON
package com.demo.common.tencentcloud;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tms.v20201229.TmsClient;
import com.tencentcloudapi.tms.v20201229.models.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "tencentcloud")//报错是正常的
public class TextDetection {
private String secretId;
private String secretKey;
public JSONObject greenTextDetection(String text) throws TencentCloudSDKException {
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
Credential cred = new Credential(secretId, secretKey);
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("tms.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
TmsClient client = new TmsClient(cred, "ap-guangzhou", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
TextModerationRequest req = new TextModerationRequest();
//Base64加密
String encryptionText = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
//设置内容参数
req.setContent(encryptionText);
// 返回的resp是一个TextModerationResponse的实例,与请求对象对应
TextModerationResponse resp = client.TextModeration(req);
// 输出json格式的字符串回包
String result = TextModerationResponse.toJsonString(resp);
return JSON.parseObject(result);
}
}
(2)图片内容安全
- 接口调用说明:
- 需要的参数:密钥信息、待检测图片URL路径
- 图片文件大小支持:文件 < 5M
- 图片文件分辨率支持:建议分辨率大于256x256,否则可能会影响识别效果;
- 图片文件支持格式:PNG、JPG、JPEG、BMP、GIF、WEBP格式;
- 图片文件链接支持的传输协议:HTTP、HTTPS;
- 若传入图片文件的访问链接,则需要注意图片下载时间限制为3秒,为保障被检测图片的稳定性和可靠性,建议您使用腾讯云COS存储或者CDN缓存等;
- 默认接口请求频率限制:100次/秒,超过此调用频率则会报错。
- 返回类型:JSON
package com.demo.common.tencentcloud;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ims.v20201229.ImsClient;
import com.tencentcloudapi.ims.v20201229.models.*;
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "tencentcloud")//报错是正常的
public class ImageDetection {
private String secretId;
private String secretKey;
public JSONObject greenImageDetection(String imageUrl) throws TencentCloudSDKException {
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
Credential cred = new Credential(secretId, secretKey);
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ims.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
ImsClient client = new ImsClient(cred, "ap-guangzhou", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
ImageModerationRequest req = new ImageModerationRequest();
//设置图片url地址
req.setFileUrl(imageUrl);
// 返回的resp是一个ImageModerationResponse的实例,与请求对象对应
ImageModerationResponse resp = client.ImageModeration(req);
// 输出json格式的字符串回包
String result = ImageModerationResponse.toJsonString(resp);
return JSON.parseObject(result);
}
}
其他配置:
在resources/META-INF/spring.factories中配置两个类的所在路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.common.tencentcloudapi.ImageDetection,\
com.demo.common.tencentcloudapi.TextDetection
四:结果处理
1.返回参数
两个服务返回的参数基本相同,见下表:
2.功能测试
@SpringBootTest(classes = WemediaApplication.class)
@RunWith(SpringRunner.class)
public class wemediaDemo {
@Autowired
private TextDetection textDetection;
@Autowired
private ImageDetection imageDetection;
@Test
public void textTest() throws TencentCloudSDKException {
JSONObject result_json = textDetection.greenTextDetection("冰毒");
String result = (String) result_json.get("Suggestion");
System.out.println("文本"+result);
}
@Test
public void imageTest() throws TencentCloudSDKException {
//图片是存在minio中生成的url
JSONObject result_json = imageDetection.greenImageDetection("http://81.68.106.78:9000/leadnews/2022/08/02/c60d952f2473468da758db0338012814.jpg");
String result = (String) result_json.get("Suggestion");
System.out.println("图片"+result);
}
}
测试结果:
文本是违规的:
图片是正常图片所以显示pass通过: