前段时间在做一个在线社交的项目,里面就有一个用户发布动态的功能,发布动态就需要审核,于是我就选择了华为云来对用户发布的动态进行审核,以下附上华为云的地址

华为云-内容审核

内容审核_内容检测 _内容风险检测_Moderation_华为云

在这里只讲一下对文本和图像的审核,所以在这里只需要订购文本和图像的服务即可

使用说明

附上快速入门文档的地址

成长地图_内容审核 Moderation_华为云

如果你是第一次使用华为云,就要先注册用户和进行实名认证,实名认证可以在右上角的用户里面可以找得到,很容易就可以认证成功,然后就是开通文本和图像内容审核(如图)

附上内容审核的地址:

https://console.huaweicloud.com/moderation/?region=cn-north-1#/moderation/services/textAntispam

账号管理

在华为云中,需要创建子账号进行获取token,才可以调用其他API,创建子账户如下步骤

上图的 账号名和密码是需要记下来的,一会要用到,然后右下角下一步

到这里就创建子用户完成了

抽取组件

一:首先要在pom文件导入hutool依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.4.3</version>
</dependency>

二:编写Template类

package com.afu.commons.templates;import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.afu.commons.properties.HuaWeiUGCProperties;import java.util.*;public class HuaWeiUGCTemplate {private HuaWeiUGCProperties properties; //后面的属性类public HuaWeiUGCTemplate(HuaWeiUGCProperties properties) {this.properties = properties;}public boolean textContentCheck(String textModeration) {String url = "https://moderation." + properties.getProject() + ".myhuaweicloud.com/v1.0/moderation/text";String reqBody = JSONUtil.createObj().set("categories", StrUtil.split(properties.getCagegoriesText(), ',')).set("items", JSONUtil.createArray().set(JSONUtil.createObj().set("text", textModeration).set("type", "content"))).toString();String resBody = HttpRequest.post(url).header("X-Auth-Token", this.getToken()).contentType("application/json;charset=utf8").setConnectionTimeout(3000).setReadTimeout(2000).body(reqBody).execute().body();JSONObject jsonObject = JSONUtil.parseObj(resBody);if (jsonObject.containsKey("result") && jsonObject.getJSONObject("result").containsKey("suggestion")) {String suggestion = jsonObject.getJSONObject("result").getStr("suggestion").toUpperCase();if("PASS".equals(suggestion)) {return true;}}return false;}public boolean imageContentCheck(String[] urls) {String url = "https://moderation." + properties.getProject() + ".myhuaweicloud.com/v1.0/moderation/image/batch";String reqBody = JSONUtil.createObj().set("categories", properties.getCagegoriesImage().split(",")).set("urls", urls).toString();String resBody = HttpRequest.post(url).header("X-Auth-Token", this.getToken()).contentType("application/json;charset=utf8").setConnectionTimeout(5000).setReadTimeout(3000).body(reqBody).execute().body();System.out.println("resBody=" + resBody);JSONObject jsonObject = JSONUtil.parseObj(resBody);if(jsonObject.containsKey("result")){//审核结果中如果出现一个block或review,整体结果就是不通过,如果全部为PASS就是通过if(StrUtil.contains(resBody, "\"suggestion\":\"block\"")){return false;}else if(StrUtil.contains(resBody, "\"suggestion\":\"review\"")){return false;}else{return true;}}//默认人工审核return false;}private String token;private long expire = 0L;// 获取tokenpublic String getToken() {Long now = System.currentTimeMillis();if(now > expire) {String url = "https://iam.myhuaweicloud.com/v3/auth/tokens";String reqBody = JSONUtil.createObj().set("auth", JSONUtil.createObj().set("identity", JSONUtil.createObj().set("methods", JSONUtil.createArray().set("password")).set("password", JSONUtil.createObj().set("user", JSONUtil.createObj().set("domain", JSONUtil.createObj().set("name", properties.getDomain())).set("name", properties.getUsername()).set("password", properties.getPassword())))).set("scope", JSONUtil.createObj().set("project", JSONUtil.createObj().set("name", properties.getProject())))).toString();token = HttpRequest.post(url).contentType("application/json;charset=utf8").setConnectionTimeout(3000).setReadTimeout(5000).body(reqBody).execute().header("X-Subject-Token");expire = System.currentTimeMillis() + 23 * 60 * 60 * 1000;}return token;}}

三:编写Properties类

package com.afu.commons.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "afu.huawei")//yml配置文件里的前缀
@Data
public class HuaWeiUGCProperties {private String username;private String password;private String project;private String domain;private String cagegoriesText;private String cagegoriesImage;
}

四:自动装载配置

@Configuration
@EnableConfigurationProperties({HuaWeiUGCProperties.class
})
public class AfuhuaAutoConfiguration {//将返回值生成Bean放到spring容器@Beanpublic HuaWeiUGCTemplate huaWeiUGCTemplate(HuaWeiUGCProperties properties) {return new HuaWeiUGCTemplate(properties);}
}

五.application.yml配置

Afu:huawei:username: 前面创建的子账户的名字password: 子账户的密码project: cn-east-3 # 区域IDdomain:  你的华为云账号# 图片检测内容 politics:是否涉及政治人物的检测,terrorism:是否包含涉政暴恐元素的检测,porn:是否包含涉黄内容元素的检测,ad:是否包含广告的检测(公测特性),all:包含politics、terrorism和porn三种场景的检测cagegoriesImage: politics,terrorism,porn# 文字检测内容 politics:涉政,porn:涉黄,ad:广告,abuse:辱骂,contraband:违禁品,flood:灌水cagegoriesText: politics,porn,ad,abuse,contraband,flood

上面yml文件里面的project:

domain:

最后附上测试类

package com.afu.manage.test;import com.afu.commons.templates.HuaWeiUGCTemplate;
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;@RunWith(SpringRunner.class)
@SpringBootTest
public class HuaWeiTest {@Autowiredprivate HuaWeiUGCTemplate template;@Testpublic void testToken() {System.out.println(template.getToken());}//测试文字@Testpublic void testText() {boolean check = template.textContentCheck("好好先生");System.out.println(check);}//测试图像,需要注意的是图像需要用的是阿里云oss上面的地址@Testpublic void testImages() {String[] urls = new String[]{"http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/9.jpg","http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/10.jpg"};boolean check = template.imageContentCheck(urls);System.out.println(check);}
}

使用华为云的准备工作就完成了,项目中需要用的时候直接注入并调用工具类HuaWeiUGCTemplate即可

SpringBoot项目中华为云 内容审核的使用(内附代码)相关推荐

  1. 华为云 内容审核API调用 前端 js uni-app

    文章目录 前言 一.华为云的内容审核api的调用需要哪些东西? 二.使用步骤 1.先注册一个华为云的账号 2.申请内容审核服务 3.[华为云内容审核API的官方文档](https://support. ...

  2. 华为云内容审核—性能更加狂野,价格更加腼腆

    关于华为云内容审核 华为云内容审核是基于图像.文本.视频的检测技术,可自动检测涉黄.广告.涉政涉暴.涉政敏感人物等内容,对用户上传的图片.文字.视频进行内容审核,并且可识别照片是否清晰及对表单图片扭曲 ...

  3. 使用c#接入华为云-内容审核

    背景 内容审核(Content Moderation),是基于图像.文本.音视频的检测技术,可自动检测涉黄.涉政涉暴.涉政敏感人物.图文违规等内容,对用户上传的图片.文字.音视频进行内容审核,以满足上 ...

  4. 最最最详细的springboot项目中集成微信扫码登入功能.步骤代码超级详细(OAuth2)

    说到登录注册,就会想到先要注册一个用户名,在进行登入,但是现在大多数的网站都集成了微信登入,不需要注册,给你一个二维码,微信一扫直接登录.这确实是十分便捷的.所以我们会尽量在项目中实现这一功能.减少用 ...

  5. springboot项目整合百度AI内容审核(文本,图片)

    毕设项目做得差不多了,但功能上基本都是本地完成的,除了有一个支付功能以及图片上传的优化,其他貌似没有用到云的东西,显得过于单调,由于是社区型项目,用户发送文本以及上传图片的频率是十分高的,于是就打算利 ...

  6. springboot项目整合阿里云oss的内容审核

    springboot项目整合阿里云 内容审核 第一 添加依赖 <dependency><groupId>com.aliyun</groupId><artifa ...

  7. 亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像)

    亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像) 大噶好,我们继续延续上一章,学习如何使用OSS存放用户头像代码示例; 在application.propert ...

  8. SpringBoot项目中集成第三方登录功能

    SpringBoot项目中集成第三方登录功能 引言 1 环境准备 2 代码实现 3 第三方平台认证申请 4 打包和部署项目 5 第三方平台登录认证测试 6 参考文章 引言 最近想把自己在公众号上介绍过 ...

  9. IDEA springboot项目中properties配置文件 {针对将对应GBK改为UTF-8并勾选转为ASCII后仍无效情况} 运行时中文乱码解决

    springboot项目中properties配置文件中,运行时中文乱码情况 file encoding里边进行设置,设为utf-8并勾选转为ascii,分别在setting.setting for ...

最新文章

  1. 任务管理器进程中多个chrome.exe的问题
  2. LeetCode Course Schedule II(拓扑排序)
  3. 《leetcode》first-missing-positive
  4. 关于JEECG 开源声明
  5. Linux 服务器安全加固 10条建议
  6. 解决在Mac上操作sourcetree反复要求输入密码的问题
  7. 6)Thymelead th:with 局部变量 与 属性优先级 和 Thymeleaf 注释
  8. 【电路设计】尖峰电压与浪涌电流
  9. 遍历QListWidget 所有item
  10. [转载]ubuntu samba Windows共享 你可能没有权限访问网络资源
  11. 【BX学习之打印机】 惠普5055(无线WiFi小型打印机双面复印扫描一体机)
  12. 机器学习“剧透”权游大结局:三傻最先领盒饭,龙妈、小恶魔笑到最后
  13. 刘泽云《计量经济学实验教程》笔记
  14. python网易云热歌榜歌曲信息爬取(iframe框架内数据爬取,src为空)
  15. 骑行从脚下,健康你我他之第一篇-----杭城骑行路线参考图
  16. Struts2(2)_什么是 struts2
  17. 在javaweb中将excel表格导入存放数据库
  18. 谷歌出品,数据集搜索引擎上线了!
  19. 网络信息安全之APT攻击
  20. 【项目实战】心脏病患者数据分析和建模

热门文章

  1. 用计算机绘制函数图像数学大师,什么手机app可以画函数图像 能画函数图像的app推荐...
  2. Flying-Saucer使用HTML或者FTL(Freemarker模板)生成PDF
  3. 基于K-Means聚类算法对NBA球员数据的聚类分析
  4. Linux 安装python 3.8(Linux 的版本为 Centos 7)
  5. 知识图谱中的实体定义
  6. [Kaggle]泰坦尼克号沉没预测
  7. 餐饮店如何做活动吸引人
  8. 【樂理】中國古典音樂樂理
  9. 小米电视5和5pro区别
  10. 用来用去,Python脚本打包 exe还是这款工具最棒