I'm working on creating a facial detection system. So basically taking a picture using the webcam and to detect whether there's a face or not in that picture.
The response I got when there's a face is:
{"images":[{"status":"Complete","width":600,"height":450,"file":"content_5942003c94c10","faces":[{"topLeftX":259,"topLeftY":233,"height":188,"rightEyeCenterY":277,"rightEyeCenterX":314,"pitch":-8,"quality":0.20461,"confidence":0.99944,"chinTipX":350,"yaw":1,"chinTipY":430,"eyeDistance":88,"width":188,"leftEyeCenterY":280,"leftEyeCenterX":401,"attributes":{"lips":"Together","asian":0.99925,"gender":{"femaleConfidence":0.0031,"type":"M","maleConfidence":0.9969},"age":27,"hispanic":4.0e-5,"other":0.0007,"black":0,"white":1.0e-5,"glasses":"Eye"},"face_id":1,"roll":3}]}]}
The response I got when there is not a face is:
{"Errors":[{"Message":"no faces found in the image","ErrCode":5002}]}
I want to change the response to: If there's a face, alert "success" if there is not a face, alert "fail". How should I do that? Add an if/else statement?
The Ajax:
$.ajax({
type: 'POST',
url: 'detect.php',
data: data,
dataType: 'json'
}).done(function(data){
console.log(data);
$("#showCounter").html("");
$("#detectResponse").html(data);
});
$(video).hide();
}
</div>
Actually you're right, you need to add if-statement inside .done()
...
}).done(function(data){
if (data.images.length > 0){
alert("success");
//Do your codes
}else{
alert("fail");
//Do your codes
}
});
Note: In your case, even no faces detected, it will still fall into .done() as it indicate the ajax return data successfully.