热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

人脸评分微信接口-PHP源码

人脸评分微信接口
php代码

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 = "
                     
                     
                     
                     
                     ";

                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\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[&#39;face_id&#39;];  
        $tempFace2 = $faceArray[1];  
        $tempId2 = $tempFace2[&#39;face_id&#39;];  

        // face++ 链接  
        $jsonStr1 = curl_get_contents(COMPARE_URL.API_KEY.API_SECRET."&face_id2=".$tempId2 ."&face_id1=".$tempId1);   
        $replyDic1 = json_decode($jsonStr1,true);  

        //取出相似程度  
        $tempResult = $replyDic1[&#39;similarity&#39;];  
        
        $tempSimilarity = $replyDic1[&#39;component_similarity&#39;];  
        $tempEye = $tempSimilarity[&#39;eye&#39;];  
        $tempEyebrow = $tempSimilarity[&#39;eyebrow&#39;];  
        $tempMouth = $tempSimilarity[&#39;mouth&#39;];  
        $tempNose = $tempSimilarity[&#39;nose&#39;];  

        $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[&#39;result&#39;];
        $landmarkArry = $result[0];
        $landmark =$landmarkArry[&#39;landmark&#39;];

        $right_eyebrow_left_corner  =$landmark[&#39;right_eyebrow_left_corner&#39;];
        $left_eyebrow_right_corner  =$landmark[&#39;left_eyebrow_right_corner&#39;];

        $left_eye_left_corner       =$landmark[&#39;left_eye_left_corner&#39;];
        $left_eye_right_corner      =$landmark[&#39;left_eye_right_corner&#39;];

        $mouth_left_corner          =$landmark[&#39;mouth_left_corner&#39;];
        $mouth_right_corner         =$landmark[&#39;mouth_right_corner&#39;];

        $nose_left                  =$landmark[&#39;nose_left&#39;];
        $nose_right                 =$landmark[&#39;nose_right&#39;];
        $nose_contour_lower_middle  =$landmark[&#39;nose_contour_lower_middle&#39;];

        $right_eye_left_corner      =$landmark[&#39;right_eye_left_corner&#39;];
        $right_eye_right_corner     =$landmark[&#39;right_eye_right_corner&#39;];

        $contour_left1              =$landmark[&#39;contour_left1&#39;];
        $contour_right1             =$landmark[&#39;contour_right1&#39;];
        $contour_chin               =$landmark[&#39;contour_chin&#39;];
        $contour_left6              =$landmark[&#39;contour_left6&#39;];
        $contour_right6             =$landmark[&#39;contour_right6&#39;];

        //计算两眉头间的距离
        $c1=distance($left_eyebrow_right_corner[&#39;x&#39;],$left_eyebrow_right_corner[&#39;y&#39;],$right_eyebrow_left_corner[&#39;x&#39;],$right_eyebrow_left_corner[&#39;y&#39;]);

        //眉毛之间的中点坐标;
        $c1_x=($right_eyebrow_left_corner[&#39;x&#39;]-$left_eyebrow_right_corner[&#39;x&#39;])/2+$left_eyebrow_right_corner[&#39;x&#39;];
        $c1_y=($right_eyebrow_left_corner[&#39;y&#39;]-$left_eyebrow_right_corner[&#39;y&#39;])/2+$left_eyebrow_right_corner[&#39;y&#39;];

        //眉毛中点到鼻子最低处的距离
        $c2 = distance($nose_contour_lower_middle[&#39;x&#39;],$nose_contour_lower_middle[&#39;y&#39;],$c1_x,$c1_y);

        //眼角之间的距离
        $c3 = distance($left_eye_right_corner[&#39;x&#39;],$left_eye_right_corner[&#39;y&#39;],$right_eye_left_corner[&#39;x&#39;],$right_eye_left_corner[&#39;y&#39;]);

        //鼻子的宽度
        $c4 = distance($nose_left[&#39;x&#39;],$nose_left[&#39;y&#39;],$nose_right[&#39;x&#39;],$nose_right[&#39;y&#39;]);

        //脸的宽度
        $c5 = distance($contour_left1[&#39;x&#39;],$contour_left1[&#39;y&#39;],$contour_right1[&#39;x&#39;],$contour_right1[&#39;y&#39;]);

        //下巴到鼻子下方的高度
        $c6 = distance($contour_chin[&#39;x&#39;],$contour_chin[&#39;y&#39;],$nose_contour_lower_middle[&#39;x&#39;],$nose_contour_lower_middle[&#39;y&#39;]);

        //眼睛的大小
        $c7_left = distance($left_eye_left_corner[&#39;x&#39;],$left_eye_left_corner[&#39;y&#39;],$left_eye_right_corner[&#39;x&#39;],$left_eye_right_corner[&#39;y&#39;]);
        $c7_right = distance($right_eye_left_corner[&#39;x&#39;],$right_eye_left_corner[&#39;y&#39;],$right_eye_right_corner[&#39;x&#39;],$right_eye_right_corner[&#39;y&#39;]);

        //嘴巴的大小
        $c8 = distance($mouth_left_corner[&#39;x&#39;],$mouth_left_corner[&#39;y&#39;],$mouth_right_corner[&#39;x&#39;],$mouth_right_corner[&#39;y&#39;]);

        //嘴巴处的face大小
        $c9 = distance($contour_left6[&#39;x&#39;],$contour_left6[&#39;y&#39;],$contour_right6[&#39;x&#39;],$contour_right6[&#39;y&#39;]);

        /* 开始计算步骤 */
        $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;
}

?>

推荐阅读
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • 微信官方授权及获取OpenId的方法,服务器通过SpringBoot实现
    主要步骤:前端获取到code(wx.login),传入服务器服务器通过参数AppID和AppSecret访问官方接口,获取到OpenId ... [详细]
  • 本文介绍了JavaScript进化到TypeScript的历史和背景,解释了TypeScript相对于JavaScript的优势和特点。作者分享了自己对TypeScript的观察和认识,并提到了在项目开发中使用TypeScript的好处。最后,作者表示对TypeScript进行尝试和探索的态度。 ... [详细]
  • 基于深度学习的遥感应用
    文章目录深度学习的发展过程深度学习在遥感中的应用基于深度学习的遥感样例库建设基于深度学习的遥感影像目标及场景检索基于深度学习的建筑物提取基于深度学习的密集建筑物自动检测基于深度学习 ... [详细]
  • jmeter实践:从csv中获取带引号的数据详情的技巧和运行全部数据的方法
    本文分享了jmeter实践中从csv中获取带引号的数据的解决办法,包括设置CSV Data Set Config和运行脚本获取数据的方法。另外还介绍了循环运行csv中全部数据的解决方法,避免每次修改csv用例都需要修改脚本的麻烦。通过了解和掌握工具的细节点,可以更好地解决问题和提高技术水平。 ... [详细]
  • 数字账号安全与数据资产问题的研究及解决方案
    本文研究了数字账号安全与数据资产问题,并提出了解决方案。近期,大量QQ账号被盗事件引起了广泛关注。欺诈者对数字账号的价值认识超过了账号主人,因此他们不断攻击和盗用账号。然而,平台和账号主人对账号安全问题的态度不正确,只有用户自身意识到问题的严重性并采取行动,才能推动平台优先解决这些问题。本文旨在提醒用户关注账号安全,并呼吁平台承担起更多的责任。令牌云团队对此进行了长期深入的研究,并提出了相应的解决方案。 ... [详细]
  • Go语言实现堆排序的详细教程
    本文主要介绍了Go语言实现堆排序的详细教程,包括大根堆的定义和完全二叉树的概念。通过图解和算法描述,详细介绍了堆排序的实现过程。堆排序是一种效率很高的排序算法,时间复杂度为O(nlgn)。阅读本文大约需要15分钟。 ... [详细]
  • uniapp开发H5解决跨域问题的两种代理方法
    本文介绍了uniapp开发H5解决跨域问题的两种代理方法,分别是在manifest.json文件和vue.config.js文件中设置代理。通过设置代理根域名和配置路径别名,可以实现H5页面的跨域访问。同时还介绍了如何开启内网穿透,让外网的人可以访问到本地调试的H5页面。 ... [详细]
  • python限制递归次数(python最大公约数递归)
    本文目录一览:1、python为什么要进行递归限制 ... [详细]
author-avatar
lb000
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有