侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

人脸识别测颜值、测脸龄、测相似度微信接口

微信(小程序)  /  管理员 发布于 7年前   239

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

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

valid();}else{  $wechatObj->responseMsg();} class wechatCallbackapiTest{  public function valid(){    $echoStr = $_GET["echostr"];    //valid signature , option    if($this->checkSignature()){      echo $echoStr;      exit;    }  }   public function responseMsg(){    //get post data, May be due to the different environments    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];     //extract post data    if (!empty($postStr)){        /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,          the best way is to check the validity of xml by yourself */        libxml_disable_entity_loader(true);        $postObj   = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);        $fromUsername = $postObj->FromUserName;        $toUsername  = $postObj->ToUserName;        $keyword   = trim($postObj->Content);        $imgUrl    = $postObj->PicUrl;        $Event    = $postObj->Event;        $EventKey   = $postObj->EventKey;        $MsgType   = $postObj->MsgType;        $time     = time();         $itemTpl = "           <![CDATA[%s]]>                                            ";         if($MsgType == "image"){          $item_str = sprintf($itemTpl, "颜值报告单", face($imgUrl), $imgUrl, MESSAGE_URL);           $xmlTpl = "                                 %s                      %s           $item_str           ";          $resultStr = sprintf($xmlTpl, $fromUsername, $toUsername, $time, 1);          echo $resultStr;        }}else {      echo "";      exit;    }  }       private function checkSignature(){    // you must define TOKEN by yourself    if (!defined("TOKEN")){      throw new Exception('TOKEN is not defined!');    }         $signature = $_GET["signature"];    $timestamp = $_GET["timestamp"];    $nonce = $_GET["nonce"]; $token = TOKEN;    $tmpArr = array($token, $timestamp, $nonce);    // use SORT_STRING rule    sort($tmpArr, SORT_STRING);    $tmpStr = implode( $tmpArr );    $tmpStr = sha1( $tmpStr );         if( $tmpStr == $signature ){      return true;    }else{      return false;    }  } } // 调用人脸识别的API返回识别结果function face($imgUrl){  // face++ 链接   $jsonStr  =curl_get_contents(DETECT_URL.API_KEY.API_SECRET."&url=".$imgUrl.ATTRIBUTE);  $replyDic = json_decode($jsonStr,true);  $faceArray = $replyDic['face'];     $resultStr = "";       for ($i= 0;$i< count($faceArray); $i++){         $resultStr .= "<----第".($i+1)."张脸---->\n";     $tempFace  = $faceArray[$i];    $faceId   = $tempFace['face_id'];     $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'];        // 返回性别    $sex=$tempGenger['value'];    if($sex === "Male") {      $resultStr .= "性别:男\n";    } else if($sex === "Female") {      $resultStr .= "性别:女\n";    }     //返回年龄    $maxAge = $tempAge['value'] + ($tempAge['range'])/2;    $age=ceil($maxAge);    $resultStr .= "年龄:".$age."岁左右吧~ \n";     //返回种族    if($tempRace['value'] === "Asian") {      $resultStr .= "肤色:很健康哦~\n";    }    else if($tempRace['value'] === "White") {      $resultStr .= "肤色:皮肤好白哟!^ 3^\n";    }    else if($tempRace['value'] === "Black") {      $resultStr .= " 肤色:你有点黑?!!!\n";    }     //返回微笑度    $smiling = intval($tempSmiling['value']);    $smile = round($tempSmiling['value'],3);    $resultStr .= "微笑:".$smile."%\n";     if($count<3){      //计算颜值      $yanzhi=getYanZhi($faceId,$smiling);      $resultStr .= "外貌协会专家评分:".$yanzhi."分\n\n";      $resultStr .= "\xe2\x9c\xa8小编想说:\n";      switch ($yanzhi){        case $yanzhi>94:          $resultStr .="这颜值,爆表了!\n";          break;        case $yanzhi>87:          $resultStr .="你这么好看,咋不上天呢!\n";          break;        case $yanzhi>82:          $resultStr .="百看不厌,继续加油!\n";          break;        case $yanzhi>72:          $resultStr .="还好,还能看!\n";          break;        case $yanzhi>67:          $resultStr .="哎,只是丑的不明显!\n";          break;        case $yanzhi>62:          $resultStr .="如果有钱,可以去整整!\n";          break;        default:          $resultStr .="让我静静,你家没镜子么?\n";      }    }   //图片中两个人时,计算相似度  if(count($faceArray) === 2){     // 获取face_id     $tempFace1 = $faceArray[0];     $tempId1 = $tempFace1['face_id'];     $tempFace2 = $faceArray[1];     $tempId2 = $tempFace2['face_id'];      // face++ 链接     $jsonStr1 = curl_get_contents(COMPARE_URL.API_KEY.API_SECRET."&face_id2=".$tempId2 ."&face_id1=".$tempId1);      $replyDic1 = json_decode($jsonStr1,true);      //取出相似程度     $tempResult = $replyDic1['similarity'];          $tempSimilarity = $replyDic1['component_similarity'];     $tempEye = $tempSimilarity['eye'];     $tempEyebrow = $tempSimilarity['eyebrow'];     $tempMouth = $tempSimilarity['mouth'];     $tempNose = $tempSimilarity['nose'];      $resultStr .= "<----相似分析---->\n";     $resultStr .= "眼睛:".round($tempEye,3)."%\n";     $resultStr .= "眉毛:".round($tempEyebrow,3)."%\n";     $resultStr .= "嘴巴:".round($tempMouth,3)."%\n";     $resultStr .= "鼻子:".round($tempNose,3)."%\n";          $resultStr .= "\n<----匹配结果---->\n两人相似程度:".round($tempResult,3)."%\n";      if($tempResult>70){      $resultStr .="哇塞!绝对的夫妻相了!\n";    }elseif ($tempResult>50){      $resultStr .="哎哟,长得挺像!你们快点在一起吧!\n";    }else{      $resultStr .="0.0 长得不太一样哦。\n";    }     }    //如果没有检测到人脸  if($resultStr === ""){    $resultStr = "对不起,俺没有识别出来,请换张正脸照试试=.=";  }  return $resultStr;}  //颜值算法function getYanZhi($faceId,$smiling){  $t1=microtime(1);  $jsonStr = curl_get_contents(LANDMARK_URL.API_KEY.API_SECRET."&face_id=".$faceId.TYPE);  $t2=microtime(1);  if(($t2-$t1)>1.5){    return 75.632;  }   if ($jsonStr!=false) {    $replyDic = json_decode($jsonStr,true);     $result = $replyDic['result'];    $landmarkArry = $result[0];    $landmark =$landmarkArry['landmark'];     $right_eyebrow_left_corner =$landmark['right_eyebrow_left_corner'];    $left_eyebrow_right_corner =$landmark['left_eyebrow_right_corner'];     $left_eye_left_corner    =$landmark['left_eye_left_corner'];    $left_eye_right_corner   =$landmark['left_eye_right_corner'];     $mouth_left_corner     =$landmark['mouth_left_corner'];    $mouth_right_corner     =$landmark['mouth_right_corner'];     $nose_left         =$landmark['nose_left'];    $nose_right         =$landmark['nose_right'];    $nose_contour_lower_middle =$landmark['nose_contour_lower_middle'];     $right_eye_left_corner   =$landmark['right_eye_left_corner'];    $right_eye_right_corner   =$landmark['right_eye_right_corner'];     $contour_left1       =$landmark['contour_left1'];    $contour_right1       =$landmark['contour_right1'];    $contour_chin        =$landmark['contour_chin'];    $contour_left6       =$landmark['contour_left6'];    $contour_right6       =$landmark['contour_right6'];     //计算两眉头间的距离    $c1=distance($left_eyebrow_right_corner['x'],$left_eyebrow_right_corner['y'],$right_eyebrow_left_corner['x'],$right_eyebrow_left_corner['y']);     //眉毛之间的中点坐标;    $c1_x=($right_eyebrow_left_corner['x']-$left_eyebrow_right_corner['x'])/2+$left_eyebrow_right_corner['x'];    $c1_y=($right_eyebrow_left_corner['y']-$left_eyebrow_right_corner['y'])/2+$left_eyebrow_right_corner['y'];     //眉毛中点到鼻子最低处的距离    $c2 = distance($nose_contour_lower_middle['x'],$nose_contour_lower_middle['y'],$c1_x,$c1_y);     //眼角之间的距离    $c3 = distance($left_eye_right_corner['x'],$left_eye_right_corner['y'],$right_eye_left_corner['x'],$right_eye_left_corner['y']);     //鼻子的宽度    $c4 = distance($nose_left['x'],$nose_left['y'],$nose_right['x'],$nose_right['y']);     //脸的宽度    $c5 = distance($contour_left1['x'],$contour_left1['y'],$contour_right1['x'],$contour_right1['y']);     //下巴到鼻子下方的高度    $c6 = distance($contour_chin['x'],$contour_chin['y'],$nose_contour_lower_middle['x'],$nose_contour_lower_middle['y']);     //眼睛的大小    $c7_left = distance($left_eye_left_corner['x'],$left_eye_left_corner['y'],$left_eye_right_corner['x'],$left_eye_right_corner['y']);    $c7_right = distance($right_eye_left_corner['x'],$right_eye_left_corner['y'],$right_eye_right_corner['x'],$right_eye_right_corner['y']);     //嘴巴的大小    $c8 = distance($mouth_left_corner['x'],$mouth_left_corner['y'],$mouth_right_corner['x'],$mouth_right_corner['y']);     //嘴巴处的face大小    $c9 = distance($contour_left6['x'],$contour_left6['y'],$contour_right6['x'],$contour_right6['y']);     /* 开始计算步骤 */    $yourmark = 100;    $mustm = 0;     //眼角距离为脸宽的1/5,    $mustm += abs(($c3/$c5)*100 - 25);     //鼻子宽度为脸宽的1/5    $mustm += abs(($c4/$c5)*100 - 25);     //眼睛的宽度,应为同一水平脸部宽度的!/5    $eyepj = ($c7_left+$c7_right)/2;    $mustm += abs($eyepj/$c5*100 - 25);     //理想嘴巴宽度应为同一脸部宽度的1/2    $mustm += abs(($c8/$c9)*100 - 50);      //下巴到鼻子下方的高度 == 眉毛中点到鼻子最低处的距离    $mustm += abs($c6 - $c2);     return round($yourmark-$mustm+$smiling/10,3);  }else{    return 60;  } } //两点之间的距离function distance($px1,$py1,$px2,$py2){  return sqrt(abs(pow($px2 - $px1,2)) + abs(pow($py2 - $py1,2)));}  function curl_get_contents($url) {  $ch = curl_init();  curl_setopt( $ch , CURLOPT_URL,$url);  curl_setopt( $ch , CURLOPT_RETURNTRANSFER,1);  curl_setopt( $ch , CURLOPT_TIMEOUT,1);  curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT,1.5);  $result = curl_exec($ch);  return $result;} ?>

演示图

您可能感兴趣的文章:

  • tracking.js页面人脸识别插件使用方法
  • JavaScript人脸识别技术及脸部识别JavaScript类库Tracking.js
  • OPENCV+JAVA实现人脸识别
  • trackingjs+websocket+百度人脸识别API实现人脸签到


  • 上一条:
    PHP微信开发之微信消息自动回复下所遇到的坑
    下一条:
    PHP 生成微信红包代码简单
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客