码迷,mamicode.com
首页 > 编程语言 > 详细

python鉴黄程序

时间:2017-10-18 16:08:43      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:os.path   目标   sdi   服务   not   for   pat   客户   col   

最近有客户向服务器上传了些垃圾图片,和正常图片混合在一起,大概有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%,杜绝一切危害公司利益和危害公共网络安全的事情发生。

python鉴黄程序

标签:os.path   目标   sdi   服务   not   for   pat   客户   col   

原文地址:http://www.cnblogs.com/itor/p/7687013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!