【问题标题】:How to use AZURE face recognition Rest API?如何使用 AZURE 人脸识别 Rest API?
【发布时间】:2021-06-28 02:27:48
【问题描述】:

我在 PHP 中使用带有 curl 的 Face API。但是我在匹配图像时遇到了问题。 我能够生成 faceId,但匹配时得到的结果与预期不同。我有两张图片属于同一个人,但 API 表明这些图片不同。但是当使用 Microsoft 演示来比较图像时,我得到了正确的结果。

这里是微软演示链接: https://azure.microsoft.com/en-in/services/cognitive-services/face/#demo

这是我的图片网址

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

这是我的代码

<?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 ";
    }
}


?>

我已成功获取人脸 ID,但无法匹配。如果我尝试其他一些同一个人的图像,它有时会匹配。 问题是结果不准确。

但在 microsoft demo 上它工作正常。

【问题讨论】:

    标签: php azure curl face


    【解决方案1】:

    当您检测到人脸时,请尝试使用指定请求参数:recognitionModel=recognition_04,建议使用official doc

    我修改了您的代码如下,它非常适合我:

    <?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 ";
        }
    }
    
    ?>
    

    您的代码结果:

    【讨论】:

    • 感谢@Stanley Gong。但我昨天解决了这个问题。还是谢谢。
    • @dummyuser 很高兴知道您的问题已经解决。请单击答案旁边的复选标记将其从灰色切换为填写以接受它作为答案,以便帮助其他人并关闭此查询:)
    猜你喜欢
    • 2020-09-03
    • 2018-01-30
    • 2018-01-23
    • 2019-10-05
    • 2022-12-17
    • 2018-05-01
    • 2011-12-11
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多