python NSFW Model 图片识别鉴黄 后面更新视频检测

Posted 远方的李子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python NSFW Model 图片识别鉴黄 后面更新视频检测相关的知识,希望对你有一定的参考价值。

基于 NSFW Model 图片识别鉴黄 后面更新视频检测

识别效果


推荐SFW 0.98 色情 NSFW 0.015

推荐SFW 0.00 色情 NSFW 0.99 为色情图片

中文github代码下载 - 原文地址

环境条件

所有代码都应该与Python 3.6and兼容Tensorflow 1.x(用 1.12 测试)。模型实现可以在 中找到model.py

作者python环境

Python 3.6.13 :: Anaconda, Inc.

pip list

absl-py==1.1.0
astor==0.8.1
cached-property==1.5.2
certifi==2021.5.30
cycler==0.11.0
dataclasses==0.8
decorator==4.4.2
gast==0.5.3
grpcio==1.46.3
h5py==3.1.0
imageio==2.15.0
importlib-metadata==4.8.3
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.2
kiwisolver==1.3.1
Markdown==3.3.7
matplotlib==3.3.4
networkx==2.5.1
numpy==1.16.2
Pillow==8.4.0
protobuf==4.21.0
pyparsing==3.0.9
python-dateutil==2.8.2
PyWavelets==1.1.1
scikit-image==0.17.2
scipy==1.5.4
six==1.16.0
tensorboard==1.12.2
tensorflow==1.12.0
termcolor==1.1.0
tifffile==2020.9.3
typing_extensions==4.1.1
Werkzeug==2.0.3
wincertstore==0.2
zipp==3.6.0

安装依赖

方法 1.
pip install tensorflow== 1.12
pip installnumpy==1.16.2
pip install scikit-image==0.17.2

方法 2.
pip install -r requirements.txt

使用方法

这里注意一下只支持.jpg类型图片

python classify_nsfw.py -m data/open_nsfw-weights.npy data/test.jpg	
SFW score推荐比例  , NSFW score 不推荐比例
Results for 'test.jpg'
	SFW score:	0.9355766177177429 
	NSFW score:	0.06442338228225708 

视频或者图片批量检测

python python http_nsfw.py	

POST请求 http://127.0.0.1:2200/?token=aid.sd5@ns85
///图片格式///

    "temp_path":"C:/Users/lijie/Desktop/新-短视频/视频分割检测/66/",
    "type":"image",
    "valve":0.8,
    "src":[
        "http://aliyuncs.com/image/2022-06/16/I1nfUPElFH6oz8ux.jpeg",
        "http://aliyuncs.com/image/2022-06/16/OGCVPdDnV1wLdTn9.png"
    ]

//视频格式//

    "temp_path":"C:/Users/lijie/Desktop/新-短视频/视频分割检测/66/",
    "type":"video",
    "valve":0.8,
    "src":"http://aliyuncs.com/image/2022-06/16/OGCVPd.mp4"


返回信息 

    "isViolation": false, //是否涩情
    "tNsfw": "0.07921311259269714",
    "tsfw": "0.9207869172096252"


##http_nsfw.py 代码

#!/usr/bin/env python
import sys
import argparse
import tensorflow as tf

from model import OpenNsfwModel, InputType
from image_utils import create_tensorflow_image_loader
from image_utils import create_yahoo_image_loader
from pathlib import Path
import subprocess
import os
import numpy as np

from flask import request
from flask import jsonify
from flask import Flask
import json
from flask import jsonify
import urllib.request


class main(object):

    def __init__(self):
        self.model =''
        self.sess =''

    def test(self):
        print(666)

    def model_load(self, weights=""):
        
        model = OpenNsfwModel()#创建加载NSF模型
        input_type = InputType['TENSOR'] #模型类型 [InputType.TENSOR.name.lower(), InputType.BASE64_JPEG.name.lower()])
        model.build(weights_path=weights, input_type=input_type)
        self.model = model;
        print('模型加载OK')

        self.sess =tf.Session() #创建对象
        self.sess.run(tf.global_variables_initializer())

    def distinguish(self,input_file):

        
        fn_load_image = None
        fn_load_image = create_yahoo_image_loader() #创建雅虎图片加载器
        image = fn_load_image(input_file)
        predictions = \\
            self.sess.run(self.model.predictions,
                     feed_dict=self.model.input: image) #识别
        # print("Results for ''".format(args.input_file))
        # print("\\tSFW score:\\t\\n\\tNSFW score:\\t".format(*predictions[0]))
        return "tSFW":predictions[0][0],
                "tNSFW":predictions[0][1]



# if __name__ == "__main__":

def save_image(video_path, image_dir, fps):
    #文件夹创建
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    #-vf scale=320:240
    cmd_str = f'ffmpeg -i video_path  -f image2 -r fps image_dir/%d.jpeg -loglevel quiet '
    subprocess.run(cmd_str, encoding="utf-8" , shell=True)


app = Flask(__name__)

@app.route('/', methods=['POST'])
def distinguish():
    token = request.values.get("token")
    if token != 'aid.sd5@ns85': #token
        return '403'

    params = json.loads(request.data)

    try:
        # 不能确定正确执行的代码
        params = json.loads(request.data) # 将json字符串转为dict
    except:
        return '405 参数错误'

    distinguish = '';
    is_violation = False;
    #视频识别
    if params['type'] == 'video':
        #视频进行抽针处理
        save_image(params['src'],params['temp_path'],1)#保存帧数1
        # os.listdir()方法获取文件夹名字,返回数组
        file_name_list = os.listdir(params['temp_path'])
        for index,img in enumerate(file_name_list):
            img_str = params['temp_path'] + img
            distinguish =  Model.distinguish(img_str)
            if distinguish['tNSFW'] > params['valve']:
                is_violation = True
                break
        returnData =  
            "tsfw":f"distinguish['tSFW']",
            "tNsfw":f"distinguish['tNSFW']",
            "isViolation":is_violation
        
        return jsonify(returnData);
    #图片识别
    if params['type'] == 'image':
         #文件夹创建
        if not os.path.exists(params['temp_path']):
            os.makedirs(params['temp_path'])

        n = 0#用来计数或命名
        for img in params['src']:
            n += 1
            img_url = params['temp_path']+"%s.jpeg" % n
            urllib.request.urlretrieve(img, img_url)#保存图片到本地
            distinguish = Model.distinguish(img_url)

            if distinguish['tNSFW'] > params['valve']:
                is_violation = True
                break

        returnData =  
            "tsfw":f"distinguish['tSFW']",
            "tNsfw":f"distinguish['tNSFW']",
            "isViolation":is_violation
        

        return jsonify(returnData);


    return "Ok"

if __name__ == '__main__':
    Model =  main()
    Model.model_load("data/open_nsfw-weights.npy")
    app.run(host='0.0.0.0', port=2200, debug=False)

    


出现错误

caffe: TypeError: _open() got an unexpected keyword argument ‘as_grey‘
 “as_grey” 实际应当修改为 “as_gray”,原因是 scikit-image 的 0.17.2 版本修改了参数名称

ValueError: Object arrays cannot be loaded when allow_pickle=False
自Numpy 1.16.3版本发行之后,函数 numpy.load() 和 numpy.lib.format.read_array() 采用allow_pickle关键字,现在默认为False以响
pip install numpy=1.16.2

Note: Currently only jpeg images are supported.

classify_nsfw.py accepts some optional parameters you may want to play around with:

usage: classify_nsfw.py [-h] -m MODEL_WEIGHTS [-l yahoo,tensorflow]
                        [-t tensor,base64_jpeg]
                        input_jpeg_file

positional arguments:
  input_file            Path to the input image. Only jpeg images are
                        supported.

optional arguments:
  -h, --help            show this help message and exit
  -m MODEL_WEIGHTS, --model_weights MODEL_WEIGHTS
                        Path to trained model weights file
  -l yahoo,tensorflow, --image_loader yahoo,tensorflow
                        image loading mechanism
  -i tensor,base64_jpeg, --input_type tensor,base64_jpeg
                        input type

-l/–image-loader

The classification tool supports two different image loading mechanisms.

  • yahoo (default) replicates yahoo’s original image loading and preprocessing. Use this option if you want the same
    results as with the original implementation
  • tensorflow is an image loader which uses tensorflow exclusively (no dependencies on PIL, skimage, etc.). Tries
    to replicate the image loading mechanism used by the original caffe implementation, differs a bit though due to
    different jpeg and resizing implementations.
    See this issue for details.

Note: Classification results may vary depending on the selected image loader!

-i/–input_type

Determines if the model internally uses a float tensor (tensor - [None, 224, 224, 3] - default) or a base64 encoded
string tensor (base64_jpeg - [None, ]) as input. If base64_jpeg is used, then the tensorflow image loader will
be used, regardless of the -l/–image-loader argument.

Tools

The tools folder contains some utility scripts to test the model.

create_predict_request.py

Takes an input image and generates a json file suitable for prediction requests to a Open NSFW Model deployed
with Google Cloud ML Engine (gcloud ml-engine predict)
or tensorflow-serving.

export_savedmodel.py

Exports the model using the tensorflow serving export api (SavedModel). The export can be used to deploy the model
on Google Cloud ML Engine
, Tensorflow Serving or on mobile (haven’t tried that one yet).

export_tflite.py

Exports the model in TFLite format. Use this one if you want to run inference on
mobile or IoT devices. Please note that the base64_jpeg input type does not work with TFLite since the standard
runtime lacks a number of required tensorflow operations.

export_graph.py

Exports the tensorflow graph and checkpoint. Freezes and optimizes the graph per default for improved inference and
deployment usage (e.g. android, ios, etc.). Import the graph with tf.import_graph_def.

Python3 - Docker部署caffe open_nsfw 图片鉴黄

文章目录

本篇博文重点介绍 Centos:6.9 Docker部署Caffe, 测试open_nsfw实例, 意在学习caffe的快速安装, 这里以cpu版本的caffe安装为例, 简单应用 open_nsfw 鉴黄库, 当前使用图片和视频的项目越来越多, 在此基础上, 进一步封装可以商用, 节省成本。

1. Docker部署caffe

[harry@k8s-master01 open_nsfw]$ docker pull elezar/caffe:cpu
cpu: Pulling from elezar/caffe
6c953ac5d795: Pull complete 
3eed5ff20a90: Pull complete 
f8419ea7c1b5: Pull complete 
51900bc9e720: Pull complete 
a3ed95caeb02: Pull complete 
b968c02ba977: Pull complete 
291f35fdb68c: Pull complete 
6c428669041b: Pull complete 
ee9f5a7f3403: Pull complete 
380df6ccf740: Pull complete 
Digest: sha256:d2fc0a3e942290fdf275cc072f329557b1ba1b0210436c42cd11481d7b4b318c
Status: Downloaded newer image for elezar/caffe:cpu
docker.io/elezar/caffe:cpu

检测caffe是否安装成功

# 查看caffe版本号
[harry@k8s-master01 open_nsfw]$ docker run -ti elezar/caffe:cpu caffe --version
libdc1394 error: Failed to initialize libdc1394
caffe version 1.0.0-rc3
# 查看caffe镜像
[harry@k8s-master01 open_nsfw]$ docker images
REPOSITORY            TAG               IMAGE ID       CREATED             SIZE
elezar/caffe          cpu               aadc51f74429   5 years ago         1.31GB

2. 下载open_nsfw

wget https://github.com/yahoo/open_nsfw.git

3. 运行caffe

# 切换到刚才下载的open_nsfw文件夹
[harry@k8s-master01 ]$ cd open_nsfw

# 放几张待测试的图片
[harry@k8s-master01 open_nsfw]$ tree
.
├── 111.jpg
├── 222.jpg
├── 333.jpg
├── classify_nsfw.py
├── LICENSE.md
├── nsfw_model
│   ├── deploy.prototxt
│   └── resnet_50_1by2_nsfw.caffemodel
└── README.md

# 运行caffe
# --volume=$(pwd):/workspace 工作目录映射
# $(pwd)为当前文件路径
# /workspace 为caffe工作路径
[harry@k8s-master01 open_nsfw]$ docker run -ti --volume=$(pwd):/workspace elezar/caffe:cpu /bin/bash
root@c74059855431:/workspace# 

4. 运行鉴黄图片

# 使用open_nsfw识别库 检测图片
root@27a3cf6c655f:/workspace# python ./classify_nsfw.py --model_def nsfw_model/deploy.prototxt --pretrained_model nsfw_model/resnet_50_1by2_nsfw.caffemodel   333.jpg 
libdc1394 error: Failed to initialize libdc1394
WARNING: Logging before InitGoogleLogging() is written to STDERR
I0417 12:29:12.823567    21 net.cpp:49] Initializing net from parameters: 
name: "ResNet_50_1by2_nsfw"
state 
  phase: TEST

layer 
  name: "data"
  type: "Input"
  top: "data"
  input_param 
    shape 
      dim: 1
      dim: 3
      dim: 224
      dim: 224
    
  

...............
...............
I0417 12:28:58.549093    20 net.cpp:219] scale_1 does not need backward computation.
I0417 12:28:58.549103    20 net.cpp:219] bn_1 does not need backward computation.
I0417 12:28:58.549154    20 net.cpp:219] conv_1 does not need backward computation.
I0417 12:28:58.549166    20 net.cpp:219] data does not need backward computation.
I0417 12:28:58.549175    20 net.cpp:261] This network produces output prob
I0417 12:28:58.549315    20 net.cpp:274] Network initialization done.
I0417 12:28:58.809204    20 net.cpp:752] Ignoring source layer loss
('NSFW score:  ', 0.81271326541900635)
# 很明显0.8值高了, 你懂得~ 自己手动试试吧~

好啦🌶🌶, 关于caffe-open_nsfw鉴黄就介绍到这里, 喜欢点个赞吧~ ❤☕️

以上是关于python NSFW Model 图片识别鉴黄 后面更新视频检测的主要内容,如果未能解决你的问题,请参考以下文章

Python3 - Docker部署caffe open_nsfw 图片鉴黄

Python3 - Docker部署caffe open_nsfw 图片鉴黄

黄图识别-open nsfw

open nsfw封装成接口

nsfwjs鉴黄识别最小化案例

用Python做了鉴黄模型,内含多20万张“不可描述”图片