简介
Detecting Not-Suitable-For-Work (NSFW) content is a high demand task in computer vision. While there are many types of NSFW content, here we focus on the pornographic images and videos.
The Yahoo Open-NSFW model originally developed with the Caffe framework has been a favourite choice, but the work is now discontinued and Caffe is also becoming less popular. Please see the description on the Yahoo project page for the context, definitions, and model training details.
This Open-NSFW 2 project provides a Keras implementation of the Yahoo model, with references to its previous third-party TensorFlow 1 implementation. Note that Keras 3 is compatible with TensorFlow, JAX, and PyTorch. However, currently this model is only guaranteed to work with TensorFlow and JAX.
A simple API is provided for making predictions on images and videos.
相关技术
- TensorFlow
- fastapi
- loguru
- pillow
- requests
- nodejs
- express
- tfjs-node
- body-parser
- multiparty
- nsfwjs
python版本
usage(模型使用)
load image
1 | def load_image(image_path): |
predict image
1 | def predict(image_path): |
上述代码主要用于预测给定图像路径对应的图像的分类标签及其概率。
js版本
导入lib
1 | // 导入experss模块 |
通过接口接收数据
1 | app.post('/checkImg',async (req, res) => { |
图片数据归一化
1 | let imgTypeoObj = { |
废弃数据清空
1 | // 递归删除目录中的文件和子目录 |
内容检测
1 |
|
使用库
1 | { |
程序使用
1 | # install nodejs |
java版本
基础软件安装
1 | nvm install |
- pom.xml引入库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hserver-nsfw</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>hserver-parent</artifactId>
<groupId>cn.hserver</groupId>
<version>3.4.0</version>
</parent>
<dependencies>
<!-- 核心依赖-->
<dependency>
<artifactId>hserver</artifactId>
<groupId>cn.hserver</groupId>
</dependency>
<!-- web框架 -->
<dependency>
<artifactId>hserver-plugin-web</artifactId>
<groupId>cn.hserver</groupId>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-platform</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<!-- 打包jar -->
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 配置打包插件(设置主类,并打包成胖包) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<!-- 此处,要改成自己的程序入口(即 main 函数类) -->
<manifest>
<mainClass>net.hserver.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
实体类定义
1 |
|
Controller定义
1 | package net.hserver.controller; |
输入图片处理与模型调用
1 | package net.hserver.service; |
nsfw模型输出检测结果
1 | package net.hserver.service; |
测试结果
1 | 2023-04-21 15:54:54.312 DEBUG --- [server_business@1] c.h.p.web.handlers.DispatcherHandler [ 390] [c0a80107-lgq9ak0z-9143-1] : 地址:/check 方法:POST 耗时:1294/ms 来源:127.0.0.1 |
- result json
1
{'msg': '操作成功', 'score': {'drawings': 0.86819315, 'hentai': 0.13177933, 'neutral': 1.712211e-06, 'porn': 1.654509e-05, 'sexy': 9.231081e-06}, 'code': 200}