2 * Nude.js - Nudity detection with Javascript and HTMLCanvas
4 * Author: Patrick Wied ( http://www.patrick-wied.at )
5 * Version: 0.1 (2010-11-21)
10 var nude
= (function(){
11 // private var definition
16 initCanvas = function(){
17 canvas
= document
.createElement("canvas");
18 // the canvas should not be visible
19 canvas
.style
.display
= "none";
20 var b
= document
.getElementsByTagName("body")[0];
21 b
.appendChild(canvas
);
22 ctx
= canvas
.getContext("2d");
24 loadImageById = function(id
){
26 var img
= document
.getElementById(id
);
27 // apply the width and height to the canvas element
28 canvas
.width
= img
.width
;
29 canvas
.height
= img
.height
;
30 // reset the result function
32 // draw the image into the canvas element
33 ctx
.drawImage(img
, 0, 0);
36 loadImageByElement = function(element
){
37 // apply width and height to the canvas element
38 // make sure you set width and height at the element
39 canvas
.width
= element
.width
;
40 canvas
.height
= element
.height
;
41 // reset result function
43 // draw the image/video element into the canvas
44 ctx
.drawImage(element
, 0, 0);
46 scanImage = function(){
48 var image
= ctx
.getImageData(0, 0, canvas
.width
, canvas
.height
),
49 imageData
= image
.data
;
51 var myWorker
= new Worker('worker.nude.js'),
52 message
= [imageData
, canvas
.width
, canvas
.height
];
53 myWorker
.postMessage(message
);
54 myWorker
.onmessage = function(event
){
55 resultHandler(event
.data
);
58 // the result handler will be executed when the analysing process is done
59 // the result contains true (it is nude) or false (it is not nude)
60 // if the user passed an result function to the scan function, the result function will be executed
61 resultHandler = function(result
){
67 console
.log("the picture contains nudity");
75 // if web worker are not supported, append the noworker script
77 document
.write(unescape("%3Cscript src='noworker.nude.js' type='text/javascript'%3E%3C/script%3E"));
81 load: function(param
){
82 if(typeof(param
) == "string"){
85 loadImageByElement(param
);
89 if(arguments
.length
>0 && typeof(arguments
[0]) == "function"){
96 // register nude at window object