Python uses 10 lines of code to detect pornographic images

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.