/recognition/compare接口,总是返回NULL,搞了半天没搞定。最后还是贴上自己写的代码。
<?php
function UrlExec($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsonStr = curl_exec($ch);
curl_close ($ch);
$replyDic = json_decode($jsonStr);
return $replyDic;
}
function TestFace($imgUrl){
$url ="http://apicn.faceplusplus.com/v2/detection/detect?url=".$imgUrl."&api_key=7a02e80c1ee80d7c2c13e501d6f75de2&api_secret=11UvZenGqon8-6Ne845eOqpOytGnz5wh&&attribute=glass,pose,gender,age,race,smiling";
$replyDic=UrlExec($url);
$faceArray = $replyDic->{'face'};
return $faceArray;
}
function CompareFace($imgUrl1,$imgUrl2){
$face1Array=TestFace($imgUrl1);
$tempId1 = $face1Array[0]->{'face_id'};
$face2Array=TestFace($imgUrl2);
$tempId2 = $face2Array[0]->{'face_id'};
//var_dump(json_decode($face1Array));
$url1="https://apicn.faceplusplus.com/v2/recognition/compare?api_secret=11UvZenGqon8-6Ne845eOqpOytGnz5wh&api_key=7a02e80c1ee80d7c2c13e501d6f75de2&face_id2=".$tempId2 ."&face_id1=".$tempId1;
$replyDic=UrlExec($url1);
var_dump($replyDic);
// $tempAttr = $replyDic->{'attribute'};
// echo $tempAttr->{'eye'};
//$url="http://api.faceplusplus.com/detection/landmark?api_secret11UvZenGqon8-6Ne845eOqpOytGnz5wh&api_key=7a02e80c1ee80d7c2c13e501d6f75de2&face_id=".$tempId1."&type=83p";
echo $tempId1.",".$tempId2;
}
function face($imgUrl) {
// face++ 链接
$resultStr = "";
$faceArray=TestFace($imgUrl);
$resultStr .= "图中共检测到".count($faceArray)."张脸!\n";
for ($i= 0;$i< count($faceArray); $i++){
$resultStr .= "第".($i+1)."张脸\n";
$tempFace = $faceArray[$i];
// 获取所有属性
$tempAttr = $tempFace->{'attribute'};
// 年龄:包含年龄分析结果
// value的值为一个非负整数表示估计的年龄, range表示估计年龄的正负区间
$tempAge = $tempAttr->{'age'};
// 性别:包含性别分析结果
// value的值为Male/Female, confidence表示置信度
$tempGenger = $tempAttr->{'gender'};
// 种族:包含人种分析结果
// value的值为Asian/White/Black, confidence表示置信度
$tempRace = $tempAttr->{'race'};
// 微笑:包含微笑程度分析结果
//value的值为0-100的实数,越大表示微笑程度越高
$tempSmiling = $tempAttr->{'smiling'};
// 眼镜:包含眼镜佩戴分析结果
// value的值为None/Dark/Normal, confidence表示置信度
$tempGlass = $tempAttr->{'glass'};
// 造型:包含脸部姿势分析结果
// 包括pitch_angle, roll_angle, yaw_angle
// 分别对应抬头,旋转(平面旋转),摇头
// 单位为角度。
$tempPose = $tempAttr->{'pose'};
//返回年龄
$minAge = $tempAge->{'value'} - $tempAge->{'range'};
$minAge = $minAge < 0 ? 0 : $minAge;
$maxAge = $tempAge->{'value'} + $tempAge->{'range'};
$resultStr .= "年龄:".$minAge."-".$maxAge."岁\n";
// 返回性别
if($tempGenger->{'value'} === "Male")
$resultStr .= "性别:男\n";
else if($tempGenger->{'value'} === "Female")
$resultStr .= "性别:女\n";
// 返回种族
if($tempRace->{'value'} === "Asian")
$resultStr .= "种族:黄种人\n";
else if($tempRace->{'value'} === "Male")
$resultStr .= "种族:白种人\n";
else if($tempRace->{'value'} === "Black")
$resultStr .= "种族:黑种人\n";
// 返回眼镜
if($tempGlass->{'value'} === "None")
$resultStr .= "眼镜:木有眼镜\n";
else if($tempGlass->{'value'} === "Dark")
$resultStr .= "眼镜:目测墨镜\n";
else if($tempGlass->{'value'} === "Normal")
$resultStr .= "眼镜:普通眼镜\n";
//返回微笑
$resultStr .= "微笑:".round($tempSmiling->{'value'})."%\n";
}
if(count($faceArray) === 2){
// 获取face_id
$tempFace = $faceArray[0];
$tempId1 = $tempFace->{'face_id'};
$tempFace = $faceArray[1];
$tempId2 = $tempFace->{'face_id'};
// face++ 链接
$ur2="https://apicn.faceplusplus.com/v2/recognition/compare?api_secret=11UvZenGqon8-6Ne845eOqpOytGnz5wh&api_key=7a02e80c1ee80d7c2c13e501d6f75de2&face_id2=".$tempId2 ."&face_id=".$tempId1;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $ur2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsonStr = curl_exec($ch);
curl_close ($ch);
$replyDic = json_decode($jsonStr);
var_dump($replyDic);
if($replyDic!=null){
//取出相似程度
$tempResult = $replyDic->{'similarity'};
$resultStr .= "相似程度:".round($tempResult)."%\n";
//具体分析相似处
$tempSimilarity = $replyDic->{'component_similarity'};
$tempEye = $tempSimilarity->{'eye'};
$tempEyebrow = $tempSimilarity->{'eyebrow'};
$tempMouth = $tempSimilarity->{'mouth'};
$tempNose = $tempSimilarity->{'nose'};
$resultStr .= "相似分析:\n";
$resultStr .= "眼睛:".round($tempEye)."%\n";
$resultStr .= "眉毛:".round($tempEyebrow)."%\n";
$resultStr .= "嘴巴:".round($tempMouth)."%\n";
$resultStr .= "鼻子:".round($tempNose)."%\n";
}
}
//如果没有检测到人脸
if($resultStr === "")
$resultStr = "照片中木有人脸=.=";
return $resultStr;
};
function Test(){
}
$url1="http://myfirstweixin-images.stor.sinaapp.com/ym1.jpg";
$url2="http://myfirstweixin-images.stor.sinaapp.com/ym2.jpg";
CompareFace($url2,$url1);
$conter= face($url1);
$conter.=face($url2);
echo $conter;
?>