写真の顔検出にこのプラグインを使用しています。動作しますが、写真に検出可能な顔がない場合、エラー関数は呼び出されません。代わりに "complete" 関数が呼び出されます。
顔の検出に失敗した場合、配列を返さない場合、または未定義のオブジェクトを返す場合、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>