VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 人脸识别测颜值、测脸龄、测相似度微信接口

这篇文章主要给大家分享的是一段人脸识别测颜值、测脸龄、测相似度微信接口的代码,非常的有意思,有需要的小伙伴可以参考下。

人脸评分微信接口,获取微信图片地址,curl请求face++接口,解析json数据,计算颜值,返回用户。

颜值匹配版,请到腾讯微校上体验,http://weixiao.qq.com

  1. <?php 
  2. /** 
  3.  * 人脸识别测颜值、测脸龄、测相似度微信接口 
  4.  * @Created by MOS.Ving. 
  5.  * @Author: MOS.Ving 
  6.  * @Mail 904679843@qq.com 
  7.  * @Date: 2016-01-31 
  8.  */ 
  9.    
  10. define("TOKEN", 'weixin'); //设置token 
  11.    
  12. //FACE++ 参数 自行到face++官网注册并创建应用 
  13. define("API_KEY", "api_key=填在这里"); //你的face++应用 api_key 
  14. define("API_SECRET", "&api_secret=这里也要填");//你的face++应用 api_secret 
  15. define("ATTRIBUTE", "&attribute=glass,pose,gender,age,race,smiling");//需要返回的内容的参数 
  16.    
  17. define("DETECT_URL", "http://apicn.faceplusplus.com/v2/detection/detect?");//检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性api地址 
  18. define("LANDMARK_URL", "http://api.faceplusplus.com/detection/landmark?");//检测给定人脸(Face)相应的面部轮廓,五官等关键点的位置,包括25点和83点两种模式api地址 
  19. define("COMPARE_URL", "https://apicn.faceplusplus.com/v2/recognition/compare?");//计算两个Face的相似性以及五官相似度api地址 
  20.    
  21. define("TYPE","&type=83p");//83点模式 
  22.    
  23.    
  24. define("MESSAGE_URL", "");//放回图文消息被点击需要跳转的地址,不需要跳转可不填 
  25.    
  26. $wechatObj = new wechatCallbackapiTest(); 
  27. if($_GET['echostr']){ 
  28.   $wechatObj->valid(); 
  29. }else{ 
  30.   $wechatObj->responseMsg(); 
  31. } 
  32.    
  33. class wechatCallbackapiTest{ 
  34.   public function valid(){ 
  35.     $echoStr = $_GET["echostr"]; 
  36.     //valid signature , option 
  37.     if($this->checkSignature()){ 
  38.       echo $echoStr; 
  39.       exit; 
  40.     } 
  41.   } 
  42.    
  43.   public function responseMsg(){ 
  44.     //get post data, May be due to the different environments 
  45.     $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  46.    
  47.     //extract post data 
  48.     if (!emptyempty($postStr)){ 
  49.         /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, 
  50.           the best way is to check the validity of xml by yourself */ 
  51.         libxml_disable_entity_loader(true); 
  52.         $postObj   = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
  53.         $fromUsername = $postObj->FromUserName; 
  54.         $toUsername  = $postObj->ToUserName; 
  55.         $keyword   = trim($postObj->Content); 
  56.         $imgUrl    = $postObj->PicUrl; 
  57.         $Event    = $postObj->Event; 
  58.         $EventKey   = $postObj->EventKey; 
  59.         $MsgType   = $postObj->MsgType; 
  60.         $time     = time(); 
  61.    
  62.         $itemTpl = "<item> 
  63.            <Title><![CDATA[%s]]></Title> 
  64.            <Description><![CDATA[%s]]></Description> 
  65.            <PicUrl><![CDATA[%s]]></PicUrl> 
  66.            <Url><![CDATA[%s]]></Url> 
  67.            </item>"; 
  68.    
  69.         if($MsgType == "image"){ 
  70.           $item_str = sprintf($itemTpl, "颜值报告单", face($imgUrl), $imgUrl, MESSAGE_URL); 
  71.    
  72.           $xmlTpl = "<xml> 
  73.            <ToUserName><![CDATA[%s]]></ToUserName> 
  74.            <FromUserName><![CDATA[%s]]></FromUserName> 
  75.            <CreateTime>%s</CreateTime> 
  76.            <MsgType><![CDATA[news]]></MsgType> 
  77.            <ArticleCount>%s</ArticleCount> 
  78.            <Articles>$item_str</Articles> 
  79.            </xml>"; 
  80.           $resultStr = sprintf($xmlTpl, $fromUsername, $toUsername, $time, 1); 
  81.           echo $resultStr; 
  82.         } 
  83.           
  84.     }else { 
  85.       echo ""; 
  86.       exit; 
  87.     } 
  88.   } 
  89.        
  90.   private function checkSignature(){ 
  91.     // you must define TOKEN by yourself 
  92.     if (!defined("TOKEN")){ 
  93.       throw new Exception('TOKEN is not defined!'); 
  94.     } 
  95.        
  96.     $signature = $_GET["signature"]; 
  97.     $timestamp = $_GET["timestamp"]; 
  98.     $nonce = $_GET["nonce"]; 
  99.            
  100.     $token = TOKEN; 
  101.     $tmpArr = array($token, $timestamp, $nonce); 
  102.     // use SORT_STRING rule 
  103.     sort($tmpArr, SORT_STRING); 
  104.     $tmpStr = implode( $tmpArr ); 
  105.     $tmpStr = sha1( $tmpStr ); 
  106.        
  107.     if( $tmpStr == $signature ){ 
  108.       return true; 
  109.     }else{ 
  110.       return false; 
  111.     } 
  112.   } 
  113.    
  114. } 
  115.    
  116. // 调用人脸识别的API返回识别结果 
  117. function face($imgUrl){ 
  118.   // face++ 链接  
  119.   $jsonStr  =curl_get_contents(DETECT_URL.API_KEY.API_SECRET."&url=".$imgUrl.ATTRIBUTE); 
  120.   $replyDic = json_decode($jsonStr,true); 
  121.   $faceArray = $replyDic['face']; 
  122.      
  123.   $resultStr = ""; 
  124.        
  125.   for ($i= 0;$i< count($faceArray); $i++){ 
  126.        
  127.     $resultStr .= "<----第".($i+1)."张脸---->\n"; 
  128.    
  129.     $tempFace  = $faceArray[$i]; 
  130.     $faceId   = $tempFace['face_id']; 
  131.    
  132.     $tempAttr = $tempFace['attribute']; 
  133.     // 年龄:包含年龄分析结果 
  134.     // value的值为一个非负整数表示估计的年龄, range表示估计年龄的正负区间 
  135.     $tempAge = $tempAttr['age']; 
  136.     // 性别:包含性别分析结果 
  137.     // value的值为Male/Female, confidence表示置信度 
  138.     $tempGenger = $tempAttr['gender']; 
  139.     // 种族:包含人种分析结果 
  140.     // value的值为Asian/White/Black, confidence表示置信度 
  141.     $tempRace = $tempAttr['race']; 
  142.     // 微笑:包含微笑程度分析结果 
  143.     //value的值为0-100的实数,越大表示微笑程度越高 
  144.     $tempSmiling = $tempAttr['smiling']; 
  145.       
  146.     // 返回性别 
  147.     $sex=$tempGenger['value']; 
  148.     if($sex === "Male") { 
  149.       $resultStr .= "性别:男\n"; 
  150.     } else if($sex === "Female") { 
  151.       $resultStr .= "性别:女\n"; 
  152.     } 
  153.    
  154.     //返回年龄 
  155.     $maxAge = $tempAge['value'] + ($tempAge['range'])/2; 
  156.     $age=ceil($maxAge); 
  157.     $resultStr .= "年龄:".$age."岁左右吧~ \n"; 
  158.    
  159.     //返回种族 
  160.     if($tempRace['value'] === "Asian") { 
  161.       $resultStr .= "肤色:很健康哦~\n"; 
  162.     } 
  163.     else if($tempRace['value'] === "White") { 
  164.       $resultStr .= "肤色:皮肤好白哟!^ 3^\n"; 
  165.     } 
  166.     else if($tempRace['value'] === "Black") { 
  167.       $resultStr .= " 肤色:你有点黑?!!!\n"; 
  168.     } 
  169.    
  170.     //返回微笑度 
  171.     $smiling = intval($tempSmiling['value']); 
  172.     $smile = round($tempSmiling['value'],3); 
  173.     $resultStr .= "微笑:".$smile."%\n"; 
  174.    
  175.     if($count<3){ 
  176.       //计算颜值 
  177.       $yanzhi=getYanZhi($faceId,$smiling); 
  178.       $resultStr .= "外貌协会专家评分:".$yanzhi."分\n\n"; 
  179.       $resultStr .= "\xe2\x9c\xa8小编想说:\n"; 
  180.       switch ($yanzhi){ 
  181.         case $yanzhi>94: 
  182.           $resultStr .="这颜值,爆表了!\n"; 
  183.           break; 
  184.         case $yanzhi>87: 
  185.           $resultStr .="你这么好看,咋不上天呢!\n"; 
  186.           break; 
  187.         case $yanzhi>82: 
  188.           $resultStr .="百看不厌,继续加油!\n"; 
  189.           break; 
  190.         case $yanzhi>72: 
  191.           $resultStr .="还好,还能看!\n"; 
  192.           break; 
  193.         case $yanzhi>67: 
  194.           $resultStr .="哎,只是丑的不明显!\n"; 
  195.           break; 
  196.         case $yanzhi>62: 
  197.           $resultStr .="如果有钱,可以去整整!\n"; 
  198.           break; 
  199.         default: 
  200.           $resultStr .="让我静静,你家没镜子么?\n"; 
  201.       } 
  202.     } 
  203.    
  204.   //图片中两个人时,计算相似度 
  205.   if(count($faceArray) === 2){  
  206.     // 获取face_id  
  207.     $tempFace1 = $faceArray[0];  
  208.     $tempId1 = $tempFace1['face_id'];  
  209.     $tempFace2 = $faceArray[1];  
  210.     $tempId2 = $tempFace2['face_id'];  
  211.    
  212.     // face++ 链接  
  213.     $jsonStr1 = curl_get_contents(COMPARE_URL.API_KEY.API_SECRET."&face_id2=".$tempId2 ."&face_id1=".$tempId1);   
  214.     $replyDic1 = json_decode($jsonStr1,true);  
  215.    
  216.     //取出相似程度  
  217.     $tempResult = $replyDic1['similarity'];  
  218.        
  219.     $tempSimilarity = $replyDic1['component_similarity'];  
  220.     $tempEye = $tempSimilarity['eye'];  
  221.     $tempEyebrow = $tempSimilarity['eyebrow'];  
  222.     $tempMouth = $tempSimilarity['mouth'];  
  223.     $tempNose = $tempSimilarity['nose'];  
  224.    
  225.     $resultStr .= "<----相似分析---->\n";  
  226.     $resultStr .= "眼睛:".round($tempEye,3)."%\n";  
  227.     $resultStr .= "眉毛:".round($tempEyebrow,3)."%\n";  
  228.     $resultStr .= "嘴巴:".round($tempMouth,3)."%\n";  
  229.     $resultStr .= "鼻子:".round($tempNose,3)."%\n";  
  230.        
  231.     $resultStr .= "\n<----匹配结果---->\n两人相似程度:".round($tempResult,3)."%\n";  
  232.    
  233.     if($tempResult>70){ 
  234.       $resultStr .="哇塞!绝对的夫妻相了!\n"; 
  235.     }elseif ($tempResult>50){ 
  236.       $resultStr .="哎哟,长得挺像!你们快点在一起吧!\n"; 
  237.     }else{ 
  238.       $resultStr .="0.0 长得不太一样哦。\n"; 
  239.     } 
  240.      
  241.   }  
  242.    
  243.   //如果没有检测到人脸 
  244.   if($resultStr === ""){ 
  245.     $resultStr = "对不起,俺没有识别出来,请换张正脸照试试=.="; 
  246.   } 
  247.    
  248.  return $resultStr; 
  249. } 
  250.    
  251.    
  252. //颜值算法 
  253. function getYanZhi($faceId,$smiling){ 
  254.   $t1=microtime(1); 
  255.   $jsonStr = curl_get_contents(LANDMARK_URL.API_KEY.API_SECRET."&face_id=".$faceId.TYPE); 
  256.   $t2=microtime(1); 
  257.   if(($t2-$t1)>1.5){ 
  258.     return 75.632; 
  259.   } 
  260.    
  261.   if ($jsonStr!=false) { 
  262.     $replyDic = json_decode($jsonStr,true); 
  263.    
  264.     $result = $replyDic['result']; 
  265.     $landmarkArry = $result[0]; 
  266.     $landmark =$landmarkArry['landmark']; 
  267.    
  268.     $right_eyebrow_left_corner =$landmark['right_eyebrow_left_corner']; 
  269.     $left_eyebrow_right_corner =$landmark['left_eyebrow_right_corner']; 
  270.    
  271.     $left_eye_left_corner    =$landmark['left_eye_left_corner']; 
  272.     $left_eye_right_corner   =$landmark['left_eye_right_corner']; 
  273.    
  274.     $mouth_left_corner     =$landmark['mouth_left_corner']; 
  275.     $mouth_right_corner     =$landmark['mouth_right_corner']; 
  276.    
  277.     $nose_left         =$landmark['nose_left']; 
  278.     $nose_right         =$landmark['nose_right']; 
  279.     $nose_contour_lower_middle =$landmark['nose_contour_lower_middle']; 
  280.    
  281.     $right_eye_left_corner   =$landmark['right_eye_left_corner']; 
  282.     $right_eye_right_corner   =$landmark['right_eye_right_corner']; 
  283.    
  284.     $contour_left1       =$landmark['contour_left1']; 
  285.     $contour_right1       =$landmark['contour_right1']; 
  286.     $contour_chin        =$landmark['contour_chin']; 
  287.     $contour_left6       =$landmark['contour_left6']; 
  288.     $contour_right6       =$landmark['contour_right6']; 
  289.    
  290.     //计算两眉头间的距离 
  291.     $c1=distance($left_eyebrow_right_corner['x'],$left_eyebrow_right_corner['y'],$right_eyebrow_left_corner['x'],$right_eyebrow_left_corner['y']); 
  292.    
  293.     //眉毛之间的中点坐标; 
  294.     $c1_x=($right_eyebrow_left_corner['x']-$left_eyebrow_right_corner['x'])/2+$left_eyebrow_right_corner['x']; 
  295.     $c1_y=($right_eyebrow_left_corner['y']-$left_eyebrow_right_corner['y'])/2+$left_eyebrow_right_corner['y']; 
  296.    
  297.     //眉毛中点到鼻子最低处的距离 
  298.     $c2 = distance($nose_contour_lower_middle['x'],$nose_contour_lower_middle['y'],$c1_x,$c1_y); 
  299.    
  300.     //眼角之间的距离 
  301.     $c3 = distance($left_eye_right_corner['x'],$left_eye_right_corner['y'],$right_eye_left_corner['x'],$right_eye_left_corner['y']); 
  302.    
  303.     //鼻子的宽度 
  304.     $c4 = distance($nose_left['x'],$nose_left['y'],$nose_right['x'],$nose_right['y']); 
  305.    
  306.     //脸的宽度 
  307.     $c5 = distance($contour_left1['x'],$contour_left1['y'],$contour_right1['x'],$contour_right1['y']); 
  308.    
  309.     //下巴到鼻子下方的高度 
  310.     $c6 = distance($contour_chin['x'],$contour_chin['y'],$nose_contour_lower_middle['x'],$nose_contour_lower_middle['y']); 
  311.    
  312.     //眼睛的大小 
  313.     $c7_left = distance($left_eye_left_corner['x'],$left_eye_left_corner['y'],$left_eye_right_corner['x'],$left_eye_right_corner['y']); 
  314.     $c7_right = distance($right_eye_left_corner['x'],$right_eye_left_corner['y'],$right_eye_right_corner['x'],$right_eye_right_corner['y']); 
  315.    
  316.     //嘴巴的大小 
  317.     $c8 = distance($mouth_left_corner['x'],$mouth_left_corner['y'],$mouth_right_corner['x'],$mouth_right_corner['y']); 
  318.    
  319.     //嘴巴处的face大小 
  320.     $c9 = distance($contour_left6['x'],$contour_left6['y'],$contour_right6['x'],$contour_right6['y']); 
  321.    
  322.     /* 开始计算步骤 */ 
  323.     $yourmark = 100; 
  324.     $mustm = 0; 
  325.    
  326.     //眼角距离为脸宽的1/5, 
  327.     $mustm += abs(($c3/$c5)*100 - 25); 
  328.    
  329.     //鼻子宽度为脸宽的1/5 
  330.     $mustm += abs(($c4/$c5)*100 - 25); 
  331.    
  332.     //眼睛的宽度,应为同一水平脸部宽度的!/5 
  333.     $eyepj = ($c7_left+$c7_right)/2; 
  334.     $mustm += abs($eyepj/$c5*100 - 25); 
  335.    
  336.     //理想嘴巴宽度应为同一脸部宽度的1/2 
  337.     $mustm += abs(($c8/$c9)*100 - 50); 
  338.    
  339.    
  340.     //下巴到鼻子下方的高度 == 眉毛中点到鼻子最低处的距离 
  341.     $mustm += abs($c6 - $c2); 
  342.    
  343.     return round($yourmark-$mustm+$smiling/10,3); 
  344.   }else{ 
  345.     return 60; 
  346.   } 
  347.    
  348. } 
  349.    
  350. //两点之间的距离 
  351. function distance($px1,$py1,$px2,$py2){ 
  352.   return sqrt(abs(pow($px2 - $px1,2)) + abs(pow($py2 - $py1,2))); 
  353. } 
  354.    
  355.    
  356. function curl_get_contents($url) { 
  357.   $ch = curl_init(); 
  358.   curl_setopt( $ch , CURLOPT_URL,$url); 
  359.   curl_setopt( $ch , CURLOPT_RETURNTRANSFER,1); 
  360.   curl_setopt( $ch , CURLOPT_TIMEOUT,1); 
  361.   curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT,1.5); 
  362.   $result = curl_exec($ch); 
  363.   return $result; 
  364. } 
  365.    
  366. ?> 

出处:http://www.phpfensi.com/php/20210725/17392.html


相关教程