-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnude.js
102 lines (97 loc) · 2.76 KB
/
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
/*
* 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
*/
(function(){
var nude = (function(){
// private var definition
var canvas = null,
ctx = null,
resultFn = null,
// private functions
initCanvas = function(){
canvas = document.createElement("canvas");
// the canvas should not be visible
canvas.style.display = "none";
var b = document.getElementsByTagName("body")[0];
b.appendChild(canvas);
ctx = canvas.getContext("2d");
},
loadImageById = function(id){
// get the image
var img = document.getElementById(id);
// apply the width and height to the canvas element
canvas.width = img.width;
canvas.height = img.height;
// reset the result function
resultFn = null;
// draw the image into the canvas element
ctx.drawImage(img, 0, 0);
},
loadImageByElement = function(element){
// apply width and height to the canvas element
// make sure you set width and height at the element
canvas.width = element.width;
canvas.height = element.height;
// reset result function
resultFn = null;
// draw the image/video element into the canvas
ctx.drawImage(element, 0, 0);
},
scanImage = function(){
// get the image data
var image = ctx.getImageData(0, 0, canvas.width, canvas.height);
imageData.crossOrigin = 'anonymous';
imageData = image.data;
console.log(imageData);
var myWorker = new Worker('worker.nude.js'),
message = [imageData, canvas.width, canvas.height];
myWorker.postMessage(message);
myWorker.onmessage = function(event){
resultHandler(event.data);
}
},
// the result handler will be executed when the analysing process is done
// the result contains true (it is nude) or false (it is not nude)
// if the user passed an result function to the scan function, the result function will be executed
resultHandler = function(result){
if(resultFn){
resultFn(result);
}else{
if(result)
console.log("the picture contains nudity");
}
}
// public interface
return {
init: function(){
initCanvas();
// if web worker are not supported, append the noworker script
if(!!!window.Worker){
document.write(unescape("%3Cscript src='noworker.nude.js' type='text/javascript'%3E%3C/script%3E"));
}
},
load: function(param){
if(typeof(param) == "string"){
loadImageById(param);
}else{
loadImageByElement(param);
}
},
scan: function(fn){
if(arguments.length>0 && typeof(arguments[0]) == "function"){
resultFn = fn;
}
scanImage();
}
};
})();
// register nude at window object
if(!window.nude)
window.nude = nude;
// and initialize it
nude.init();
})();