phpazurecurlface-recognition

How to use AZURE 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 microsoft demo link: https://azure.microsoft.com/en-in/services/cognitive-services/face/#demo

Here are My images url

$img1 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/1645715403_1.jpg"; $img2 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/3.png";

Here is my code

<?php

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_03&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);
    print_r($strResponse);
    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 */
print_r($faceid);

$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 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/1645715403_1.jpg";
$img2 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/3.png";
$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 successfully got face id but unable to match. If I try some other images of same person it matches sometimes. The problem is result is not accurate.

but on microsoft demo it is working fine.


Solution

  • Pls try to use specify request param: recognitionModel=recognition_04 when you detect faces as official doc recommanded: enter image description here

    I modified your code as below, it works for me perfectly:

    <?php
    
    function compare($image1, $image2)
    {
        $faceid = array();
        $images = array($image1 , $image2);
    
        $faceAPIName = "nexever";
        $apikey = "<your api key>";
    
        $faceidAPIHost = "https://$faceAPIName.cognitiveservices.azure.com";
            
        foreach($images as $data)
        {   
            $detect = array('url' => $data); 
            $result = do_post("$faceidAPIHost/face/v1.0/detect?recognitionModel=recognition_04&detectionModel=detection_03",json_encode($detect),$apikey);
            array_push($faceid , $result[0]['faceId']);
        }       
    
        $request_url = "$faceidAPIHost/face/v1.0/verify";      
    
        /* Face ID to compare */
        print_r($faceid);
    
        $match = array("faceId1"=>$faceid[0], "faceId2"=>$faceid[1],"maxNumOfCandidatesReturned" =>10,"mode"=> "matchFace");
     
        return do_post($request_url,json_encode($match),$apikey);
        
    }
    
    function do_post($url, $params,$key) {
        $options = array(
            'http' => array(
                'header'  => "Content-type: application/json\r\nOcp-Apim-Subscription-Key: $key",
                'method'  => 'POST',
                'content' => $params
            )
        );
        $result = file_get_contents($url, false, stream_context_create($options));
        
        return json_decode($result, true);
    }
    
    $img1 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/1645715403_1.jpg";
    $img2 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/3.png";
    $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 ";
        }
    }
    
    ?>
    

    Result of your code: enter image description here