借用face++人脸识别,来识别年龄

新手笔记,大神不要笑话

前端代码

index.htnl

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<h1>年龄预测</h1>
<form action="index.php" method="post"
      enctype="multipart/form-data">
    <label for="file">输入一张单人照片:</label>
    <input type="file" name="file" id="file" />
    <br />
    <br />
    <br />
    <input type="submit" name="submit" value="预测年龄" />
</form>
</body>
</html> 

php代码

index.php

<?php
require_once(__DIR__ . "/FacePPClientDemo.php");

header("Content-type: text/html; charset=utf-8");

$time=time();

if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/pjpeg"))
//为了限制用户上传的文件的类型
&& ($_FILES["file"]["size"] < 20000000))
//为了限制文件上传的大小,这里设置为20M,足够了 {
if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$time .$_FILES["file"]["name"]); //加$time的目的是为了防止用户上传的图片名一致 } } else { echo "Invalid file"; } $api_key = "你在face上注册之后获得的api_key"; $api_secret = "你在face上注册之后获得的api_secret";
$api = new FacePPClientDemo($api_key, $api_secret); $person_name="你创建的personname";
$imageurl="之前上传文件在你服务器中的地址".$time. $_FILES["file"]["name"];
$result = $api->face_detect($imageurl);
if (empty($result->face)) { echo "没有检测到人脸,再换一张照片试试吧"; }
elseif (count($result->face) > 1){ echo "检测到有多张人脸,请上传一张单人照片"; }
else{ $age= $result->face[0]->attribute->age->value;
echo "<html><head></head><body><font>你的年龄大概是:$age 岁。</font> <img src= $imageurl ></body></html>" ; }

需要引入的php函数

FacePPClientDemo.php
<?php
define ("DEBUG_MODE", true);

class FacePPClientDemo
{    //定义一个类
    private $api_server_url;//服务器提交地址
    private $auth_params;

    public function __construct($api_key, $api_secret)
    {
        $this->api_server_url = "http://api.cn.faceplusplus.com/v2/";
        $this->auth_params = array();
           $this->auth_params['api_key'] = $api_key;
           $this->auth_params['api_secret'] = $api_secret;
    }
    
    //////////////////////////////////////////////////////////
    // public mathods
    //////////////////////////////////////////////////////////
    
    public function person_create($person_name) 
    {
        return $this->call("person/create", array("person_name" => $person_name));
    }
    
    public function person_delete($person_name)
    {
        return $this->call("person/delete", array("person_name" => $person_name));
    }
    
    public function person_add_face($face_id, $person_name) 
    {
        return $this->call("person/add_face", array("person_name" => $person_name,
                                                         "face_id" => $face_id));
    }
    
    public function train_identify($group_name) 
    {
        return $this->call("train/identify", array("group_name" => $group_name));
    }
    
    public function face_detect($urls = null)
    {
        return $this->call("detection/detect", array("url" => $urls));
    }
    
    public function recognition_identify($url, $group_name) 
    {
        return $this->call("recognition/identify", array("url" => $url,
                                                          "group_name" => $group_name));
    }
    
    public function group_create($group_name)
    {
        return $this->call("group/create", array("group_name" => $group_name));
    }

    public function group_delete($group_name)
    {
        return $this->call("group/delete", array("group_name" => $group_name));
    }
    
    public function group_add_person($person_name, $group_name) 
    {
        return $this->call("group/add_person", array("person_name" => $person_name,
                                                      "group_name" => $group_name));
    }
    
    public function info_get_session($session_id) {
        return $this->call("info/get_session", array("session_id" => $session_id));
        
    }
    
    public function face_detect_post($filename)
    {
        return $this->post_call("detection/detect", array(
                                  "img" => '@'.$filename
                                  ));
    }
    
    //////////////////////////////////////////////////////////
    // private mathods
    //////////////////////////////////////////////////////////
    
    protected function call($method, $params = array())
    {
        $params = array_merge($this->auth_params, $params);
        $url = $this->api_server_url . "$method?".http_build_query($params);
        
        if (DEBUG_MODE)
        {
//            echo "REQUEST: $url" . "
";
        }
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $data = curl_exec($ch);
        curl_close($ch);    

        $result = null;
        if (!empty($data))
        {
            if (DEBUG_MODE)
            {
//                echo "RETURN: " . $data . "
";
            }
            $result = json_decode($data);

        }

        return $result;

    }
    
    protected function post_call($method, $params = array())
    {
        $params = array_merge($this->auth_params, $params);
        $url = $this->api_server_url . "$method";
        
        if (DEBUG_MODE)
        {
//            echo "REQUEST: $url?" .http_build_query($params)."
";
        }
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $data = curl_exec($ch);
        curl_close($ch);
        
        $result = null;
        if (!empty($data))
        {
            if (DEBUG_MODE)
            {
//                echo "RETURN: " . $data . "
";
            }
            $result = json_decode($data);
        }
        
        return $result;
    }
    
}
?>

它的api在这里  http://www.faceplusplus.com.cn/api-overview/

php的demo    http://www.faceplusplus.com.cn/wp-content/uploads/Demo_PHP.zip

如果对图片上传有疑问的请看:http://www.cnblogs.com/tobemaster/p/5376768.html

原文地址:https://www.cnblogs.com/tobemaster/p/5376797.html