php使用face++实现一个简单的人脸识别系统

文件目录:php使用face++实现一个简单的人脸识别系统_第1张图片

php使用face++实现一个简单的人脸识别系统_第2张图片


dir.php

  
  
  
  

  
\n"; 
	while($file = $mydir->read())
	{ 
		if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")) 
		{
			echo "
  • $file
  • \n"; tree("$directory/$file"); } else echo "
  • $file
  • \n"; } echo "\n"; $mydir->close(); } //开始运行 //echo "

    目录为粉红色


    \n"; //tree("D:\\AR"); /*********************** 第二种实现办法:用readdir()函数 ************************/ function listDir($dir, &$names, &$img_urls) { if(is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if((is_dir($dir."/".$file)) && $file!="." && $file!="..") { //echo "文件名:",$file,"

    "; listDir($dir."/".$file."/"); } else { $file = iconv("gb2312","UTF-8",$file); if($file!="." && $file!="..") { //echo $file."
    "; //var_dump($file); $file_name = strstr($file, '.', true); //echo $file_name."
    "; array_push($names, $file_name); array_push($img_urls, $dir."/".$file); } } } closedir($dh); } } } //开始运行 //$file_names = array(); //listDir("E:\\test", $file_names); //var_dump($file_names); //$name = array("叶子", "朱镇模"); //echo $name[0]; ?>

    face.php

    
     * @since  2013-12-11
     * @version  1.1
     * @modified 16-01-2014
     * @copyright 2013 - 2015 Tianye
     **/
    class Facepp
    {
        ######################################################
        ### If you choose Amazon(US) server,please use the ###
        ### http://apius.faceplusplus.com/v2               ###
        ### or                                             ###
        ### https://apius.faceplusplus.com/v2              ###
        ######################################################
        public $server          = 'http://apicn.faceplusplus.com/v2';
        #public $server         = 'https://apicn.faceplusplus.com/v2';
        #public $server         = 'http://apius.faceplusplus.com/v2';
        #public $server         = 'https://apius.faceplusplus.com/v2';
        public $api_key         = '';        // set your API KEY or set the key static in the property
        public $api_secret      = '';        // set your API SECRET or set the secret static in the property
        private $useragent      = 'Faceplusplus PHP SDK/1.1';
        /**
         * @param $method - The Face++ API
         * @param array $params - Request Parameters
         * @return array - {'http_code':'Http Status Code', 'request_url':'Http Request URL','body':' JSON Response'}
         * @throws Exception
         */
        public function execute($method, array $params)
        {
            if( ! $this->apiPropertiesAreSet()) {
                throw new Exception('API properties are not set');
            }
            $params['api_key']      = $this->api_key;
            $params['api_secret']   = $this->api_secret;
            return $this->request("{$this->server}{$method}", $params);
        }
        private function request($request_url, $request_body)
        {
            $curl_handle = curl_init();
            curl_setopt($curl_handle, CURLOPT_URL, $request_url);
            curl_setopt($curl_handle, CURLOPT_FILETIME, true);
            curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false);
            if(version_compare(phpversion(),"5.5","<=")){
                curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY,CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
            }else{
                curl_setopt($curl_handle, CURLOPT_SAFE_UPLOAD, false);
            }
            curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
            curl_setopt($curl_handle, CURLOPT_HEADER, false);
            curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000);
            curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120);
            curl_setopt($curl_handle, CURLOPT_NOSIGNAL, true);
            curl_setopt($curl_handle, CURLOPT_REFERER, $request_url);
            curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->useragent);
            
            if (extension_loaded('zlib')) {
                curl_setopt($curl_handle, CURLOPT_ENCODING, '');
            }
            curl_setopt($curl_handle, CURLOPT_POST, true);
            if (array_key_exists('img', $request_body)) {
                $request_body['img'] = '@' . $request_body['img'];
            } else {
                $request_body = http_build_query($request_body);
            }
            curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $request_body);
            $response_text      = curl_exec($curl_handle);
            $response_header    = curl_getinfo($curl_handle);
            curl_close($curl_handle);
            return array (
                'http_code'     => $response_header['http_code'],
                'request_url'   => $request_url,
                'body'          => $response_text
            );
        }
        private function apiPropertiesAreSet()
        {
            if( ! $this->api_key) {
                return false;
            }
            if( ! $this->api_secret) {
                return false;
            }
            
            return true;
        }
    }


    index.html

    
    
     
    
    
    
    
    
    
    

    使用说明:

    训练部分:

    1.训练只需要做一次就可以
    2.如果训练目录中的图片发生了改变,则需要再训练一次
    3.需要联网才可以使用啊
    4.训练时需要选择训练用的目录
    5.训练目录中每个人的图片都有一张就可以
    6.命名时用英文命名

    测试部分:

    1.测试时选择一张图片后输入即可
    2.请在recognition.php页面中使用$person_name变量
    记录好英文文件名与中文人名的对应
    3.比如$person_name = array("ami" => "艾米",
    "dongjian" => "张东健", "xiaowang" => "小王");
    4.'=>'前的是训练时的英文文件名,
    箭头后边的则是这个英文名对应的中文名


    recognition.php

    
    array('age','gender'),'face_id'=>'abcd','position'=>array('center'),'tag'=>""));
    //print_r($face);
    require_once 'face.php';
    require_once 'dir.php';
    $facepp = new Facepp();
    $facepp->api_key       = '4b50af19005588bbf1244b99014a5613';
    $facepp->api_secret    = '-ztE4oSbzxE_Qg-BraSVzDFYNhLgYx_I';
    /*
    $img_url = array();
    $person_name = array();
    listDir("E:/test", $person_name, $img_url);
    //var_dump($img_url);
    foreach ($img_url as $url){
    $response = $facepp->execute('/recognition/identify', array('group_name' => 'oldpeople_qiaoxi', 'img' => $url));
    // $url."
    "; $face_body = json_decode($response['body'], 1); //var_dump($face_body); $face = $face_body['face']; $face1 = $face[0]; $candidates = $face1['candidate']; $person_identify = $candidates[0]; $identify_name = $person_identify['person_name']; echo $identify_name."
    "; */ $img_url = $_GET["testImgPath"]; /*----------------------------------- 中英文名字的映射就在下面 -----------------------------------*/ $person_name = array("ami" => "艾米", "dongjian" => "张东健", "xiaowang" => "小王"); //$img_url = $_GET["testImgDir"]; $response = $facepp->execute('/recognition/identify', array('group_name' => 'oldpeople_qiaoxi', 'img' => $img_url)); $face_body = json_decode($response['body'], 1); $face = $face_body['face']; $face1 = $face[0]; $candidates = $face1['candidate']; $person_identify = $candidates[0]; $identify_name = $person_identify['person_name']; //echo $identify_name; echo "识别结果为:

    ".$person_name[$identify_name]."

    "; ?>


    startTrain.php

    
    
    
      
    测试提交
    
    
    
    
    请输入训练图像文件夹路径名:
    比如D:/test
    训练需要一段时间,请耐心等待呀O(∩_∩)O亲!



    test.php

    
    
    
    
    
    
    
    
    请选择测试图片地址


    train.php

    
    
    
    api_key       = '4b50af19005588bbf1244b99014a5613';
    $facepp->api_secret    = '-ztE4oSbzxE_Qg-BraSVzDFYNhLgYx_I';
    #detect local image 
    $img_url = array();
    $person_name = array();
    $trainDir = $_GET["trainDir"];
    listDir($trainDir, $person_name, $img_url);
    echo "从目录中我们得到了 ".sizeof($img_url)." 张图片\n";
    
    //var_dump($img_url);
    //$person_name = array('lili','wangda');
    $face_ids = array();
    $i = 0;
    $response = $facepp->execute('/group/delete', array('group_name' => 'oldpeople_qiaoxi'));
    $response = $facepp->execute('/group/create', array('group_name' => 'oldpeople_qiaoxi'));
    
    foreach ($img_url as $img){
    	$params['img']          = $img;
    	$params['attribute']    = 'gender,age,race,smiling,glass,pose';
    	$response               = $facepp->execute('/detection/detect',$params);
    	
    	$face_body = json_decode($response['body'], 1);
    	//var_dump($face_body);
    	$face = $face_body['face'];
    	//$face_id = $face[1];
    
    	$face_1 = $face['0'];
    	$face_id = $face_1['face_id'];
    	array_push($face_ids, $face_id);
    	$response = $facepp->execute('/person/delete', array('person_name' => $person_name[$i],'group_name' => 'oldpeople_qiaoxi'));
    	$response = $facepp->execute('/person/create', array('person_name' => $person_name[$i],'group_name' => 'oldpeople_qiaoxi'));
    	//var_dump($response);
    	$response = $facepp->execute('/person/add_face', array('person_name' => $person_name[$i], 'face_id' => $face_id, 'group_name' => 'oldpeople_qiaoxi'));
    	$i = $i + 1;
    	
    }
    $response = $facepp->execute('/train/identify', array('group_name' => 'oldpeople_qiaoxi'));
    //var_dump($response);
    echo "哈哈,训练成功啦!\n";
    ?>
    
    
    
    
    
    
    execute('/group/create', array('group_name' => 'oldpeople_qiaoxi'));
    //$response = $facepp->execute('/group/add_people', array('group_name' => 'oldpeople_qiaoxi'));
    
    #detect image by url
    //$params['url']          = 'http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/1.jpg';
    //$response               = $facepp->execute('/detection/detect',$params);
    //print_r($response);
    /*
    	if($response['http_code'] == 200) {
        #json decode 
        $data = json_decode($response['body'], 1);
    	//var_dump($data);
    	//print_r(array_keys($data));
    	
    	//var_dump($data["face"]);
        //print_r($data1);
        //print_r($data1);
        #get face landmark
        foreach ($data['face'] as $face) {
            $response = $facepp->execute('/detection/landmark', array('face_id' => $face['face_id']));
            //print_r($response);
        }
        
        #create person 
    	
        //$response = $facepp->execute('/group/create', array('person_name' => 'unique_person_name'));
        print_r($response);
        #delete person
        //$response = $facepp->execute('/person/delete', array('person_name' => 'unique_person_name'));
        //print_r($response);
    }
    */
    ?>
    
    


    你可能感兴趣的:(php使用face++实现一个简单的人脸识别系统)