我正在使用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>
无论成功还是失败,都可能调用'complete',类似于jQuery中'ajax'方法的完整选项? – 2013-04-10 03:33:55
您是否尝试过在控制台中记录'coords'并查看没有识别到人脸时的价值? – JCOC611 2013-04-10 03:34:51