2013-04-10 79 views
0

我正在使用this plugin进行照片人脸检测。它可以工作,但是当图片中没有可检测的脸部时,错误功能不会被调用。 “完整”功能被调用。这个jquery插件总是返回一个数组吗?

我想,当它不能检测到人脸,它没有返回数组,或返回未定义的对象,或者为null,或什么的,所以我尝试:

//also tried null this way 
if(typeof coords[i].confidence === 'undefined') { 
alert("you have little aptitude for programming."); 
} 


if(coords instanceof Array) { 

     alert("good job!!"); 
} else { 
alert("not even close!!"); 
} 


if(!(coords.length)) { 
alert("you have a distorted perception of your own abilities."); 
} 


if (positionX > coords.length) { 

alert(coords[i].confidence); 
} 

当检测显示人脸适当的警报,但是当没有检测到面部时,不显示警报。它只是称为“完整”功能。

这是脚本。让我知道我是否应该提供更多的代码。

<script> 
    $(function() { 
     $('#try').click(function() { 
      var $this = $(this); 

      var coords = $('img').faceDetection({ 


       complete:function(img, coords) { 
        $this.text('Done!'); 

       }, 
       error:function(img, code, message) { 
        $this.text('error!'); 
        alert('Error: '+message); 
       } 

      }); 



      for (var i = 0; i < coords.length; i++) { 

//Here is where I have been putting those conditional statements. 

//This part, which appends a bordered div (#face) to the div containing the face pic, works fine. 

       $('<div>', { 
        'class':'face', 
        'id':'face', 
        'css': { 
         'position': 'absolute', 
         'left':  coords[i].positionX +'px', 
         'top':  coords[i].positionY +'px', 
         'width': coords[i].width +'px', 
         'height': coords[i].height +'px' 
        } 

       }) 
       .appendTo('#content');        


      } 
     }); 


     return false; 
    }); 
    </script> 

<a href="#" id="try">TRY IT NOW!</a>    

<div id="content"> 
<img src="pic.jpg" id="myPicture"/> 
</div> 
+0

无论成功还是失败,都可能调用'complete',类似于jQuery中'ajax'方法的完整选项? – 2013-04-10 03:33:55

+0

您是否尝试过在控制台中记录'coords'并查看没有识别到​​人脸时的价值? – JCOC611 2013-04-10 03:34:51

回答

0

complete确实被调用而不管成功。如果未能检测到任何面孔,插件会返回空阵列,这就是为什么您没有看到任何警报。您的for循环未执行。

+0

你是对的。我无法理解为什么我设定的条件中没有一条是在失败时调用这些警报,但事实证明我把它们放在了错误的地方。现在我有if(!(coords.length))alert(“fail”); }在循环之上,它工作。谢谢你的帮助! – user1877124 2013-04-10 14:29:59

相关问题