在 SpringBoot 中整合 TensorFlow 进行 不健康图片(NSFW ) 识别

该博客介绍了如何通过引入open-nsfw-spring-boot-starter依赖来实现图片的不安全内容检测。在Spring Boot应用中,简单几步即可集成并使用该库,返回图片为不安全内容的概率。但需要注意,由于各种因素,其准确率无法保证100%,实际应用中可能需要结合人工审核等辅助手段。
摘要由CSDN通过智能技术生成

如果懒得看过程,直接看答案然后离开吧

https://github.com/ruibty/open-nsfw-spring-boot-starter

<!-- https://mvnrepository.com/artifact/com.ruibty.nsfw/open-nsfw-spring-boot-starter -->
<dependency>
    <groupId>com.ruibty.nsfw</groupId>
    <artifactId>open-nsfw-spring-boot-starter</artifactId>
    <version>1.0</version>
</dependency>

过程

应该说比把大象放冰箱统共分几步还要简单

  1. 引入上面提到的依赖
  2. 注入后使用,结果是不安全的概率
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private NsfwService nsfwService;

    // http://localhost:8080/upload/formData
    @PostMapping("formData")
    public ResponseEntity<BigDecimal> formData(
            @RequestParam("file") MultipartFile multipartFile
    ) throws IOException {
        byte[] bytes = multipartFile.getBytes();
        float prediction = nsfwService.getPrediction(bytes);
        BigDecimal result = new BigDecimal(String.valueOf(prediction));
        return ResponseEntity.ok(result);
    }

    // http://localhost:8080/upload/base64
    @PostMapping("base64")
    public ResponseEntity<BigDecimal> base64(
            @RequestBody String base64Str
    ) {
        // 去掉base64编码中的前缀 data:image/png;base64,
        if (base64Str.contains("base64,")) {
            base64Str = base64Str.substring(base64Str.indexOf("base64,") + 7);
        }
        byte[] bytes = Base64.decodeBase64(base64Str);
        float prediction = nsfwService.getPrediction(bytes);
        BigDecimal result = new BigDecimal(String.valueOf(prediction));
        return ResponseEntity.ok(result);
    }

}

备注

受限于各种因素,这类服务的判定准确性并不能达到百分百,必要场景需要结合人工审核用户举报等手段协同完成内容安全。

比如我从某站下载的图片,结果就不太理想。

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值