python鉴黄程序

最近有客户向服务器上传了些垃圾图片,和正常图片混合在一起,大概有10W张的数量,在经历了大概3个小时翻了2000多张的时候,手指抽了下,感觉很不舒服,硬着头皮上,写个程序鉴别下吧,但是怎么搞呢,经过从网上翻阅资料和同事的协助,终于把这个命中率不高的程序弄好了:

代码如下:

# -*- coding: utf8 -*-
# Author:wxq
#python 2.7
import sys
import os
import os.path as p
import shutil
import _io
from collections import namedtuple
from PIL import Image

def check_porn(file):
    img = Image.open(file).convert('YCbCr')
    w, h = img.size
    data = img.getdata()
    cnt = 0
    for i, ycbcr in enumerate(data):
        y, cb, cr = ycbcr
        if 86 <= cb <= 117 and 140 <= cr <= 168:
            cnt += 1
    if cnt > w * h * 0.1:
        return True
    else:
        return False

if __name__ == '__main__':
    imgdir = sys.argv[1]
    dstdir = sys.argv[2]
    if imgdir == '' or dstdir == '':
        raise Exception('参数不能为空, 用法: porn_check.py <图片目录> <目标目录>')
    if p.isdir(imgdir):
        filelist = [p.join(imgdir, file) for file in os.listdir(imgdir)]
    else:
        raise Exception('参数错误, 图片目录不合法')
        sys.exit(1)
    if not p.isdir(dstdir):
        raise Exception('参数错误, 目标目录不合法')

    for file in filelist:
        if p.isfile(file):
            is_porn = check_porn(file)
            if is_porn:
                print(file, 'is porn')
                shutil.move(file, dstdir)
            else:
                print(file, 'is not porn')

正在空余时间学习python,期待优化,争取把鉴黄率提高至100%,杜绝一切危害公司利益和危害公共网络安全的事情发生。

posted @ 2017-10-18 15:42  大象无形01  阅读(1592)  评论(0编辑  收藏  举报