forked from pa7/nude.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.nude.js
340 lines (267 loc) · 7.97 KB
/
worker.nude.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Nude.js - Nudity detection with Javascript and HTMLCanvas
*
* Author: Patrick Wied ( http://www.patrick-wied.at )
* Version: 0.1 (2010-11-21)
* License: MIT License
*/
var skinRegions = [],
skinMap = [],
canvas = {};
onmessage = function(event){
canvas.width = event.data[1];
canvas.height = event.data[2];
scanImage(event.data[0]);
};
Array.prototype.remove = function(index) {
var rest = this.slice(index + 1);
this.length = index;
return this.push.apply(this, rest);
};
function scanImage(imageData){
var detectedRegions = [],
mergeRegions = [],
width = canvas.width,
lastFrom = -1,
lastTo = -1;
var addMerge = function(from, to){
lastFrom = from;
lastTo = to;
var len = mergeRegions.length,
fromIndex = -1,
toIndex = -1;
while(len--){
var region = mergeRegions[len],
rlen = region.length;
while(rlen--){
if(region[rlen] == from){
fromIndex = len;
}
if(region[rlen] == to){
toIndex = len;
}
}
}
if(fromIndex != -1 && toIndex != -1 && fromIndex == toIndex){
return;
}
if(fromIndex == -1 && toIndex == -1){
mergeRegions.push([from, to]);
return;
}
if(fromIndex != -1 && toIndex == -1){
mergeRegions[fromIndex].push(to);
return;
}
if(fromIndex == -1 && toIndex != -1){
mergeRegions[toIndex].push(from);
return;
}
if(fromIndex != -1 && toIndex != -1 && fromIndex != toIndex){
mergeRegions[fromIndex] = mergeRegions[fromIndex].concat(mergeRegions[toIndex]);
mergeRegions.remove(toIndex);
return;
}
};
// iterate the image from the top left to the bottom right
var length = imageData.length,
width = canvas.width;
for(var i = 0, u = 1; i < length; i+=4, u++){
var r = imageData[i],
g = imageData[i+1],
b = imageData[i+2],
x = (u>width)?((u%width)-1):u,
y = (u>width)?(Math.ceil(u/width)-1):1;
if(classifySkin(r, g, b)){ //
skinMap.push({"id": u, "skin": true, "region": 0, "x": x, "y": y, "checked": false});
var region = -1,
checkIndexes = [u-2, (u-width)-2, u-width-1, (u-width)],
checker = false;
for(var o = 0; o < 4; o++){
var index = checkIndexes[o];
if(skinMap[index] && skinMap[index].skin){
if(skinMap[index].region!=region && region!=-1 && lastFrom!=region && lastTo!=skinMap[index].region){
addMerge(region, skinMap[index].region);
}
region = skinMap[index].region;
checker = true;
}
}
if(!checker){
skinMap[u-1].region = detectedRegions.length;
detectedRegions.push([skinMap[u-1]]);
continue;
}else{
if(region > -1){
if(!detectedRegions[region]){
detectedRegions[region] = [];
}
skinMap[u-1].region = region;
detectedRegions[region].push(skinMap[u-1]);
}
}
}else{
skinMap.push({"id": u, "skin": false, "region": 0, "x": x, "y": y, "checked": false});
}
}
merge(detectedRegions, mergeRegions);
analyseRegions();
};
// function for merging detected regions
function merge(detectedRegions, mergeRegions){
var length = mergeRegions.length,
detRegions = [];
// merging detected regions
while(length--){
var region = mergeRegions[length],
rlen = region.length;
if(!detRegions[length])
detRegions[length] = [];
while(rlen--){
var index = region[rlen];
detRegions[length] = detRegions[length].concat(detectedRegions[index]);
detectedRegions[index] = [];
}
}
// push the rest of the regions to the detRegions array
// (regions without merging)
var l = detectedRegions.length;
while(l--){
if(detectedRegions[l].length > 0){
detRegions.push(detectedRegions[l]);
}
}
// clean up
clearRegions(detRegions);
};
// clean up function
// only pushes regions which are bigger than a specific amount to the final result
function clearRegions(detectedRegions){
var length = detectedRegions.length;
for(var i=0; i < length; i++){
if(detectedRegions[i].length > 30){
skinRegions.push(detectedRegions[i]);
}
}
};
function analyseRegions(){
// sort the detected regions by size
var length = skinRegions.length,
totalPixels = canvas.width * canvas.height,
totalSkin = 0;
// if there are less than 3 regions
if(length < 3){
postMessage(false);
return;
}
// sort the skinRegions with bubble sort algorithm
(function(){
var sorted = false;
while(!sorted){
sorted = true;
for(var i = 0; i < length-1; i++){
if(skinRegions[i].length < skinRegions[i+1].length){
sorted = false;
var temp = skinRegions[i];
skinRegions[i] = skinRegions[i+1];
skinRegions[i+1] = temp;
}
}
}
})();
// count total skin pixels
while(length--){
totalSkin += skinRegions[length].length;
}
// check if there are more than 15% skin pixel in the image
if((totalSkin/totalPixels)*100 < 15){
// if the percentage lower than 15, it's not nude!
//console.log("it's not nude :) - total skin percent is "+((totalSkin/totalPixels)*100)+"% ");
postMessage(false);
return;
}
// check if the largest skin region is less than 35% of the total skin count
// AND if the second largest region is less than 30% of the total skin count
// AND if the third largest region is less than 30% of the total skin count
if((skinRegions[0].length/totalSkin)*100 < 35
&& (skinRegions[1].length/totalSkin)*100 < 30
&& (skinRegions[2].length/totalSkin)*100 < 30){
// the image is not nude.
//console.log("it's not nude :) - less than 35%,30%,30% skin in the biggest areas :" + ((skinRegions[0].length/totalSkin)*100) + "%, " + ((skinRegions[1].length/totalSkin)*100)+"%, "+((skinRegions[2].length/totalSkin)*100)+"%");
postMessage(false);
return;
}
// check if the number of skin pixels in the largest region is less than 45% of the total skin count
if((skinRegions[0].length/totalSkin)*100 < 45){
// it's not nude
//console.log("it's not nude :) - the biggest region contains less than 45%: "+((skinRegions[0].length/totalSkin)*100)+"%");
postMessage(false);
return;
}
// TODO:
// build the bounding polygon by the regions edge values:
// Identify the leftmost, the uppermost, the rightmost, and the lowermost skin pixels of the three largest skin regions.
// Use these points as the corner points of a bounding polygon.
// TODO:
// check if the total skin count is less than 30% of the total number of pixels
// AND the number of skin pixels within the bounding polygon is less than 55% of the size of the polygon
// if this condition is true, it's not nude.
// TODO: include bounding polygon functionality
// if there are more than 60 skin regions and the average intensity within the polygon is less than 0.25
// the image is not nude
if(skinRegions.length > 60){
//console.log("it's not nude :) - more than 60 skin regions");
postMessage(false);
return;
}
// otherwise it is nude
postMessage(true);
};
function classifySkin(r, g, b){
// A Survey on Pixel-Based Skin Color Detection Techniques
var rgbClassifier = ((r>95) && (g>40 && g <100) && (b>20) && ((Math.max(r,g,b) - Math.min(r,g,b)) > 15) && (Math.abs(r-g)>15) && (r > g) && (r > b)),
nurgb = toNormalizedRgb(r, g, b),
nr = nurgb[0],
ng = nurgb[1],
nb = nurgb[2],
normRgbClassifier = (((nr/ng)>1.185) && (((r*b)/(Math.pow(r+g+b,2))) > 0.107) && (((r*g)/(Math.pow(r+g+b,2))) > 0.112));
var hsv = toHsv(r, g, b),
h = hsv[0],
s = hsv[1],
hsvClassifier = (h > 0 && h < 35 && s > 0.23 && s < 0.68);
var ycc = toYcc(r, g, b),
y = ycc[0],
cb = ycc[1],
cr = ycc[2],
yccClassifier = ((y > 80) && (cb > 77 && cb < 127) && (cr > 133 && cr < 173));
return (rgbClassifier || normRgbClassifier || hsvClassifier || yccClassifier);
};
function toYcc(r, g, b){
var y = 16 + 0.29900*r + 0.58700*g + 0.11400*b,
cr = 128 - 0.16874*r - 0.33126*g + 0.50000*b,
cb = 128 + 0.50000*r - 0.41869*g - 0.08131*b;
return [y, cr, cb];
};
// RGB to HSV by Foley et al. 1992
function toHsv(r, g, b){
var h = 0,
max = Math.max(r, g, b),
min = Math.min(r, g, b),
diff = max - min;
if(max == r){
h = (g - b)/diff;
}else if(max == g){
h = 2+((g - r)/diff)
}else{
h = 4+((r - g)/diff);
}
h = h*60;
if(h < 0){
h = h+360;
}
return [h, 1-(3*(max/(r+g+b))),(1/3)*(r+g+b)] ;
};
function toNormalizedRgb(r, g, b){
var sum = r+g+b;
return [(r/sum), (g/sum), (b/sum)];
};