Pages

Monday, September 14, 2015

JavaScript nudity detection script

http://stackoverflow.com/questions/713247/what-is-the-best-way-to-programatically-detect-porn-images

I found a python script in this SO answer, and adapted it to JavaScript. It counts skin colored pixels and compares to total pixel count.

Input - image data;

//nudity check script
function(data) {
var length = data.length;
var skinPixels = 0;
var totalPixels = 0;

for (var i = 0, n = length; i < n; i += 4) {
//i+3 is alpha (the fourth element)
    var r = data[i]; 
    var g = data[i+1]; 
    var b = data[i+2]; 

    totalPixels = totalPixels + 1;

    if ((r > 60) && (g < (r * 0.85)) && (b < (r * 0.7)) && (g > (r * 0.4)) && (b > (r * 0.2))) {
    skinPixels = skinPixels + 1;
    }
}

var isPorn = false;
var ratio = (skinPixels / totalPixels);

if (ratio > 0.0744) {
isPorn = true
}

// console.log("skin:" + skinPixels + ", total:" + totalPixels + ", ratio:" + ratio);

return isPorn;
}

Sample results are like this:


isPorn:true



isPorn:true



isPorn:false



isPorn:false



isPorn:false




No comments:

Post a Comment