question

RajKumar-1631 avatar image
0 Votes"
RajKumar-1631 asked RajKumar-1631 answered

How to use face recognition Rest API?

I am using Face API with curl in PHP. But I am having issue when matching images.
I am able to generate faceId's but when matching I get different results than expected. I have two images belonges to same person but API indicates that these images are different. But when using Microsoft demo to compare images I get right result.

Here is the code as well.

function compare($image1, $image2)
{
$faceid = array();
$images = array($image1 , $image2);

$headers = ["Ocp-Apim-Subscription-Key:**","Content-Type:application/json" ];
/
Getting faceId /
foreach($images as $data)
{
/
First step is to detect face */
$request_url='https://nexever.cognitiveservices.azure.com/face/v1.0/detect?detectionModel=detection_02&returnFaceId=true&returnFaceLandmarks=false';

     /* Image to get faceid */
        
     $detect = array('url' => $data);
 
 $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($curl, CURLOPT_URL, $request_url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($detect)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);        
 $strResponse = curl_exec($curl);
 $curlErrno   = curl_errno($curl);
 if ($curlErrno) { $curlError = curl_error($curl);throw new Exception($curlError); }
 $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl);
 $strResponse = json_decode($strResponse , true);
 array_push($faceid , $strResponse[0]['faceId']);        
 }        

// comparing by face ID
/ Match face url /
$request_url = 'https://nexever.cognitiveservices.azure.com/face/v1.0/verify';

/ Face ID to compare /
$match = array("faceId1"=>$faceid[0], "faceId2"=>$faceid[1],"maxNumOfCandidatesReturned" =>10,"mode"=> "matchFace");



 $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($curl, CURLOPT_URL, $request_url);   curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($match)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 $strResponse = curl_exec($curl);  $curlErrno   = curl_errno($curl);
 if ($curlErrno) {$curlError = curl_error($curl); throw new Exception($curlError); }
 $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 curl_close($curl);
 return json_decode($strResponse, true);

}

$img1 = "";
$img2 = "
***";
$ret = compare($img1, $img2);

print_r($ret);
if(isset($ret['isIdentical']))
{
if($ret['isIdentical'] == 1)
{
echo "Same Person ";
}
else if($ret['isIdentical'] == 0)
{
echo "Different Person ";
}
}

I have replaced my secret key and image path with ***.

I am using PHP language and kindly give me solution in PHP only.

azure-face
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cheong00 avatar image
0 Votes"
cheong00 answered RajKumar-1631 commented

There ought to be something wrong along these lines as you're not feeding the first parameter in the images to the API...

 $img1 = "";
 $img2 = "***";
 $ret = compare($img1, $img2);
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

These are the URLs of the images to compare. I have just hid them because they are personal images. there is nothing wrong with this.
Kindly check my compare function carefully.

0 Votes 0 ·

@RajKumar-1631 In the second part of your function to call the verify API you have used maxNumOfCandidatesReturned and mode as part of the request body. I think these are supported for findSimilar API rather than verify. I am not sure if this is causing any issue but since the same face ids are working correctly when you check from API console i think may be the request body from your PHP code might be an issue.

Also, printing the face ids from your array could help check if the ids passed to final verify API are the same as you do from the console. Hope this helps debug your issue further.


0 Votes 0 ·

@romungi-MSFT I have tried that way as well still no luck. Means I removed maxNumOfCandidatesReturned and mode. And I am sure there is no problem with my faceid's as well. I have printed them to make sure that I am passing same values.


0 Votes 0 ·
Show more comments
RajKumar-1631 avatar image
0 Votes"
RajKumar-1631 answered

I have solved this myself. I was not missing recognition model in my request. Adding recognition model works for me.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.