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




Thursday, September 10, 2015

android M, sdk 23, proguard, apache library errors

Hi!

I've spent last one and a half days banging my head against the table because i could not build a signed apk file with proguard, because it had some errors, similar to this:


  • cant find superclass or interface org.apache...
  • cant find referenced class org.apache...

While debug builds and apk generation works without proguard, enabling proguard produces this error.

What i understood from this:

  • google removed apache library from android sdk 23. While the classes itself are still there, they were removed from some kind of index
  • thats why in debug modes everything works, but with enabled proguard proguard obfuscates apache classes making them unusable by some other libraries those are referencing apache classes
  • it also became clear that no additional jar files or dependencies are needed, because your debug modes works ok, just the correct settings for proguard are needed. (im talking about google's advice to add apache legacy library)
The problematic classes those i seen in error logs were:
  1. org.apache.http.*
  2. android.net.http.*
Also when i made a build my app crashed because of support library obfuscations (i could not open the preference activity, i checked stack trace from emulator and seen that support libraries tried to reference obfuscated classes/methods).

Summarising, everything was fixed and working on release apk with the next lines in my proguard-rules.pro file:


-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**

-keep class android.net.http.** { *; }
-dontwarn android.net.http.**

-keep class android.support.v7.** { *; }
-keep class android.support.v4.** { *; }