PHP image filter

I am looking for some PHP nudity image filter library and I found this one on phpclasses.org: PHP Image Nudity Filter.

The class was written by Bakr Alsharif. According to the web site, the class “analyses the colors used in different sections of an image to determine whether those colors match the human skin color tones.”
I downloaded the code and it does use this interesting approach to analyze the image color pixel by pixel and see if they match the skin tone. Every match will add to a score and the higher the final score, the more likely the image contains nudity. For example, if an image scored above 30, it is likely a nude picture.

The class library is easy to understand and very simple to use. However I did discover one possbile bug. The upper bound for the “for” loop on the pixels may need to be changed to “<” instead of “<=”. Otherwise it will generate tons of “array out of bound” PHP warnings.

I test run the program using some pictures and it was quite interesting.

I used some baby photos, and it recognized them. I used some pictures with mountains and trees and it worked great.

But here is the issue. I used it to scan a bunch of real estate pictures and wow, one of them has a score of 100! I checked the photo and it is a kitchen photo with mostly beige cabinet and counter tops. The tone of the image is also light so that’s why the filter totally misjudged it. Beside that particular one, there are quite a few scored over 50 or 60. And they all have the similar characters.

I wonder if it would be help if we set a “ceiling score”. For example, if an image scores over 80 it is not likely a nude picture since you are not likely to have nudity filling up the whole screen. So I did another test and this time only single out the ones which scored between 30 and 80. Still, there are still too many “good” picutures being filtered out.

Although the test wasn’t quite successful I still think this PHP class is a heading to the right direction. Maybe some more sophisticated algorithm can improve the accuracy. For example, in stead of checking for the color for each pixel, we can figure out an “average tone” of an image and then analyze each pixel color to make the decision. Standard deviation, maybe?

This entry was posted in PHP development. Bookmark the permalink.

2 Responses to PHP image filter

  1. pSouper says:

    IMHO The nudity image filter is a non-starter when the algorithm is based on identifing skin tones. You will never be able to identify a nude by colour will only ever be able to find candidates for a pattern / shape recognition algorythm – which is a very long way of for a web script.

    Without judging the crtiticising the script This appears to not be a nudity filter but a flawed attempt at a skin filter.
    As you have found, there are too many other instances of skin tones -particularly when you take into account ALL the worlds ethnicities and the many tones within – The most obvious flaw is that a close up of a face would be flagged as nude too.
    regards,
    pSouper

  2. Azghanvi says:

    It’s really a nice effort. Some one should process it further to overcome the lackings in algo.

    @pSouper, skin color filter seems enough for 90% of nudity on internet. What you are saying is the exceptional case.

Comments are closed.