您的位置:首页 > 编程语言 > Python开发

Python基于YCbCr 肤色模型的情色图片检测的简单实现

2015-01-27 13:03 871 查看
<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>Python基于YCbCr 肤色模型的情色图片检测的简单实现</strong></span>
#Detection of Pornographic Digital Images
#YCbCr Model
#author:Robin chen
#input:the dir of an image
'''
ref:
1.http://www.naun.org/multimedia/NAUN/computers/20-462.pdf
2.http://blog.csdn.net/gzlaiyonghao/article/details/3166735
'''
from PIL import Image
import sys
def image_ifo(image):
    try:
        img = Image.open(image)
    except Exception,e:
        print "Can not open the image!"
    print 'Image Mode:%s,Image Size:%s,Image Format:%s' % (img.mode,img.size,img.format)
    return img
def preprocessed_image(image):
    img = image_ifo(image)
    if not img.mode == 'YCbCr':
        img = img.convert('YCbCr')
    return img
    
def detector(image):
    img = preprocessed_image(image)
    ycbcr_data = img.getdata()
    W,H = img.size
    THRESHOLD = 0.3
    count = 0
    for i,ycbcr in enumerate(ycbcr_data):
        y,cb,cr = ycbcr
        #if 80 <= cb <= 120 and 133 <= cr <= 173:
        if 86 <= cb <= 127 and 130 <= cr < 168:
            count += 1
    if count > THRESHOLD*W*H:
        print 'The image is pornographic!'
    else:
        print 'The image is not pornographic!'
        
if __name__ == '__main__':
    image = sys.argv[-1]
    print 'Detector is working on it,please wait a second...'
    detector(image)
    print 'Detecting is done!'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: