0

我需要的

  • 我需要扫描并检查图像是否为裸体。

  • 我使用nude.js进行图像检测问题我在加载页面时包含nude.js后面临的错误出现在控制台中。

裸体.js

(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);

    },
    scanImage = function(){
        // get the image data
        var image = ctx.getImageData(0, 0, canvas.width, canvas.height),
        imageData = image.data;

        var myWorker = new Worker('worker.nude.js'),
        message = [imageData, canvas.width, canvas.height];
        myWorker.postMessage(message);
        myWorker.onmessage = function(event){
            resultHandler(event.data);
        }
    },

abc.html

 <script>
$(document).ready(function(){
$("img").load(function(){

nude.load('testImage');
nude.scan(function(result)
{

if(!result) alert("no nude"); 
else
alert("nude)"); });
 });
 });

错误

  Uncaught TypeError: Cannot read property 'width' of null
  canvas.width = img.width; // line 28
4

1 回答 1

0

你需要打电话nude.init();。我今天遇到了同样的问题。即使在将裸体对象注册到窗口后,nude.js 将自己称为裸体.init() - 但它似乎并没有发生。旁注:canvas本身为空。错误来自canvas,而不是来自img

包括 jQuery 并在其中调用$(document).ready(...)并执行操作nude.init();

于 2015-08-12T17:28:29.670 回答