自建鉴黄服务
Jul 20, 2021
自建鉴黄服务
介绍
NSFW 的意思:
即不雅内容。
我们需要使用到一个 JS 库来去使用一个经过机械训练的,可以识别不雅图像的模型
库地址:https://github.com/infinitered/nsfwjs
不是简单的 npm install
之后即可识别内容了,识别内容的根本核心在于,是引用了一个经过训练的模型去识别,其识别效率、识别精准度,都在于识别模型。
识别模型训练开源库:https://github.com/alex000kim/nsfw_data_scraper
里面有数 10W+的黄图用于训练模型,号称可以达到93%
的识别精准度。
当然要从 0 开始训练模型,就需要耗费巨大的带宽流量和时间成本,而github
已经有优秀作者共享了经过训练出来的模型,我们只要下载并使用:https://github.com/gantman/nsfw_model
安装使用
拉取项目到本地:
$ gcl https://code.haxibiao.cn/web/nsfw.haxibiao.com.git
$ npm install
$ npm rebuild @tensorflow/tfjs-node build-addon-from-source
$ cp .env.prod .env
$ npm start
.env
文件中可以修改其监听的端口
然后使用 supervisor
挂起,常驻后台,配置模板。
[program:nsfw-nodejs-listen]
process_name=%(program_name)s_%(process_num)02d
directory=/data/www/nsfw.xxx.com
command=/root/.nvm/versions/node/v10.16.0/bin/npm start
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/data/www/nsfw.xxx.com/logs/run.log
stderr_logfile=/data/www/nsfw.xxx.com/logs/run.log
Nginx 配置模板:
server {
server_name nsfw.xxx.info
listen 80;
root /data/www/nsfw.xxx.com/;
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
# 如果请求的文件不存在,则代理到 Node.js
if ( !-f $request_filename ){
rewrite (.*) /index.js;
}
location = /index.js {
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# 代理服务
proxy_pass http://127.0.0.1:3307$request_uri;
# 忽略其他重写
proxy_redirect off;
}
}
完成。
测试
上传图片后,总共会返回 5 个维度的数值来鉴别该图片的尺度:
- 绘画(Drawing)—— 无害的艺术,或艺术绘画;
- 变态(Hentai)—— 色情艺术,不适合大多数工作环境;
- 中立(Neutral)—— 一般,无害的内容;
- 色情(Porn)—— 不雅的内容和行为,通常涉及生殖器;
- 性感(Sexy)—— 不合时宜的挑衅内容。
当porn
评分超过>=0.6
左右,就几乎是一张带有色情性质
的图片了。