如何解决如何使用 AZURE 人脸识别 Rest API?
我在 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,$request_url); curl_setopt($curl,json_encode($match)); curl_setopt($curl,TRUE);
curl_setopt($curl,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 上它工作正常。
解决方法
当您检测人脸时,请尝试使用指定请求参数: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 ";
}
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。