This article mainly introduces python's ability to detect pornographic images with 10 lines of code. it involves Python's image library PIL-based image moderation techniques and has some reference value, for more information, see the example in this article. python uses 10 lines of code to detect pornographic images. Share it with you for your reference. The details are as follows:
Principle: convert the image to the YCbCr mode, and search for the image color value pixels in the image. if the pixel area in the skin color value exceeds 1/3 of the image size, it is regarded as a yellow image.
Statement: a simple scenario is enough, and it is a little complicated and inaccurate. for example, if the whole picture is a person's profile picture, the skin color value must exceed 50% pixels, and the mistaken yellow image is too arbitrary.
Python image library PIL support must be installed
Porn_detect.py is as follows:
Import sys, PIL. image as Imageimg = Image. open (sys. argv [1]). convert ('ycbcr ') w, h = img. sizedata = img. getdata () cnt = 0for I, ycbcr in enumerate (data): y, cb, cr = ycbcr if 86 <= cb <= 117 and 140 <= cr <= 168: cnt + = 1 print '% s a porn image. '% (sys. argv [1], 'is' if cnt> w * h * 0.3 else 'is not ')
Run:
The code is as follows:
Python porn_detect.py myphoto.png
I hope this article will help you with Python programming.