php调用face++实现人脸识别自学(仅供参考)

来源:互联网 发布:unity3d 5.x 动画系统 编辑:程序博客网 时间:2024/05/29 17:49

前言:要调用使用php+face++实现人脸识别先把思路清晰一下,首先要在face++我的应用里创建一个group组,在group组里创建person类(属于人)再把上传的图片放入person里(大概的思路,但是还有很多)下面开始。

第一 :进入face++官网注册一下,之后创建应用就可以看到API_key和API_secret值。

第二:如下代码(模拟一张图片的请求)

<?php
      function curlPost($url,$data,$method){
       $ch = curl_init();   //1.初始化
       curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
       //4.参数如下
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
           curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
       curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
 
       if($method=="POST"){//5.post方式的时候添加数据
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $tmpInfo = curl_exec($ch);//6.执行
 
       if (curl_errno($ch)) {//7.如果出错
           return curl_error($ch);
       }
       curl_close($ch);//8.关闭
       return $tmpInfo;
   }  

//(图片路径)↓

        $filename="G:/phpstudy/WWW/dayi/face/zhangyushi.jpg";
        $data=array
                (
                  "api_secret"=>"写入你的值",
                  "api_key"=>"写入你的值",
                  "attribute"    => "gender,age,race,smiling,glass,pose",
                  "img"=>new CURLFile($filename),
                );
         $url="http://apicn.faceplusplus.com/v2/detection/detect";
         $method="POST";
         $files=curlPost($url,$data,$method);
         $files=json_decode($files,true);  
         // $face = $face_body['face'];  
          // $face_1 = $files['face']['0']['face_id'];  
          // $face_id = $face_1['face_id'];  
          // echo $face_1;     
         print_r($files);
?>

//打印一下如下效果

Array( [face] => Array ( [0] => Array ( [attribute] => Array ( [age] => Array ( [range] => 7 [value] => 29 ) [gender] => Array ( [confidence] => 99.997 [value] => Male ) [glass] => Array ( [confidence] => 97.2905 [value] => None ) [pose] => Array ( [pitch_angle] => Array ( [value] => -0.000628 ) [roll_angle] => Array ( [value] => 0.704834 ) [yaw_angle] => Array ( [value] => -0.024123 ) ) [race] => Array ( [confidence] => 99.9767 [value] => Asian ) [smiling] => Array ( [value] => 0.269433 ) ) [face_id] => fa60657aae7e788df9b909f2d2b0fcd8 [position] => Array ( [center] => Array ( [x] => 51.166667 [y] => 54.302671 ) [eye_left] => Array ( [x] => 47.139167 [y] => 48.767656 ) [eye_right] => Array ( [x] => 55.6335 [y] => 48.953709 ) [height] => 27.299703 [mouth_left] => Array ( [x] => 48.077333 [y] => 66.743027 ) [mouth_right] => Array ( [x] => 54.1245 [y] => 65.949852 ) [nose] => Array ( [x] => 51.3475 [y] => 57.11632 ) [width] => 15.333333 ) [tag] => ) [1] => Array ( [attribute] => Array ( [age] => Array ( [range] => 5 [value] => 18 ) [gender] => Array ( [confidence] => 99.9995 [value] => Male ) [glass] => Array ( [confidence] => 91.7143 [value] => Normal ) [pose] => Array ( [pitch_angle] => Array ( [value] => -13.012756 ) [roll_angle] => Array ( [value] => -3.56781 ) [yaw_angle] => Array ( [value] => -20.454009 ) ) [race] => Array ( [confidence] => 64.0587 [value] => White ) [smiling] => Array ( [value] => 3.56097 ) ) [face_id] => 3472fb953a066d9a14ab587a18e0990e [position] => Array ( [center] => Array ( [x] => 7.666667 [y] => 77.744807 ) [eye_left] => Array ( [x] => 5.7807 [y] => 75.197923 ) [eye_right] => Array ( [x] => 8.905517 [y] => 74.851039 ) [height] => 11.275964 [mouth_left] => Array ( [x] => 6.568533 [y] => 81.006231 ) [mouth_right] => Array ( [x] => 8.6705 [y] => 80.450445 ) [nose] => Array ( [x] => 7.005367 [y] => 78.283086 ) [width] => 6.333333 ) [tag] => ) ) [img_height] => 720 [img_id] => cd8c9cd52380129712f6e94c6bfd4d98 [img_width] => 1280 [session_id] => 4471f29aa5904ded937f6203ba1a4b96 [url] => )

记住第一个face_id,方便以后上传

第二:现在需要在我的应用里(face++官网里)创建一个group组,运行代码

<?php
      function curlPost($url,$data,$method){
       $ch = curl_init();   //1.初始化
       curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
       //4.参数如下
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
           curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
       curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
 
       if($method=="POST"){//5.post方式的时候添加数据
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $tmpInfo = curl_exec($ch);//6.执行
 
       if (curl_errno($ch)) {//7.如果出错
           return curl_error($ch);
       }
       curl_close($ch);//8.关闭
       return $tmpInfo;
   }
        $filename="G:/phpstudy/WWW/dayi/face/zhangyushi.jpg";
        $data=array
                (

                  //(这是我的sercret,写你自己的)

                  "api_secret"=>"iG5Z41L7cpMf8zLKu7s5iR31pk2LL_8k",

                 //(写你自己的key)

                  "api_key"=>"bf9139598d8aefe5b9045869698be798",                  
                  "group_name"=>"zhangyushi",
                );
         $url="http://apicn.faceplusplus.com/v2/group/create";
         $method="POST";
         $files=curlPost($url,$data,$method);
         $files=json_decode($files,true);        
         print_r($files);
?>

代码运行完成,出现

Array( [added_person] => 0 [group_id] => 67d08ff2caef42a386de63e0ffd7a086 [group_name] => zhangyushi [tag] => )

表示创建成功,进入我的应用查看一下

这就是刚才所创建的分组

第三:开始创建person,运行代码

<?php
      function curlPost($url,$data,$method){
       $ch = curl_init();   //1.初始化
       curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
       //4.参数如下
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
           curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
       curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
 
       if($method=="POST"){//5.post方式的时候添加数据
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $tmpInfo = curl_exec($ch);//6.执行
 
       if (curl_errno($ch)) {//7.如果出错
           return curl_error($ch);
       }
       curl_close($ch);//8.关闭
       return $tmpInfo;
   }
        $filename="G:/phpstudy/WWW/dayi/face/niuyongqiang.jpg";

     //下面的key填写自己的,person_name写上要上传到哪个person里,group_name写上刚才上一步所创建的  

       $data=array
                (
                  "api_secret"=>"iG5Z41L7cpMf8zLKu7s5iR31pk2LL_8k",
                  "api_key"=>"bf9139598d8aefe5b9045869698be798",
                  "person_name" => "yushi",
                  "group_name"=>"zhangyushi",
                );
         $url="http://apicn.faceplusplus.com/v2/person/create";
         $method="POST";
         $files=curlPost($url,$data,$method);
         $files=json_decode($files,true);        
         print_r($files);
?>

运行以后代码,效果如下

Array( [added_face] => 0 [added_group] => 1 [person_id] => 0a7aad64069656fc4641cdc1b72dc653 [person_name] => yushi [tag] => )

查看一下我的应用里

上面所显示的是我的应用里group(组)zhangyushi,yushi是刚才所创建的

第四:把第一步中的图片上传到现在yushi person里,运行代码

<?php
      function curlPost($url,$data,$method){
       $ch = curl_init();   //1.初始化
       curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
       //4.参数如下
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
           curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
       curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
 
       if($method=="POST"){//5.post方式的时候添加数据
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $tmpInfo = curl_exec($ch);//6.执行
 
       if (curl_errno($ch)) {//7.如果出错
           return curl_error($ch);
       }
       curl_close($ch);//8.关闭
       return $tmpInfo;
   }
        $filename="G:/phpstudy/WWW/dayi/face/zhangyushi.jpg";
        $data=array
                (
                  "api_secret"=>"iG5Z41L7cpMf8zLKu7s5iR31pk2LL_8k",
                 
                  "face_id"=>"f317ce915de60677f6af1538c7d41b50",
                   "api_key"=>"bf9139598d8aefe5b9045869698be798",
                  "person_name"=>"yushi",
                );
         $url="http://apicn.faceplusplus.com/v2/person/add_face";
         $method="POST";
         $files=curlPost($url,$data,$method);
         $files=json_decode($files,true);        
         print_r($files);
?>

返回如下

Array( [added] => 1 [success] => 1)

查看我的应用

图片已经上传到person  yushi里

之后进行测试(官网里说训练)

运行代码如下

<?php
      function curlPost($url,$data,$method){
       $ch = curl_init();   //1.初始化
       curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
       //4.参数如下
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
           curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
       curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
 
       if($method=="POST"){//5.post方式的时候添加数据
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $tmpInfo = curl_exec($ch);//6.执行
 
       if (curl_errno($ch)) {//7.如果出错
           return curl_error($ch);
       }
       curl_close($ch);//8.关闭
       return $tmpInfo;
   }
        $filename="G:/phpstudy/WWW/dayi/face/zhangyushi.jpg";
        $data=array
                (
                  "api_secret"=>"iG5Z41L7cpMf8zLKu7s5iR31pk2LL_8k",
                 
                  // "face_id"=>"ec58e97936285b05c2afe4af6e2e700e,b52e3b3c139aca22511d60e34196e197",
                   "api_key"=>"bf9139598d8aefe5b9045869698be798",
                  "group_name"=>"zhangyushi",
                  // "img"=>new CURLFile($filename),
                );
         $url="http://apicn.faceplusplus.com/v2/train/identify";
         $method="POST";
         $files=curlPost($url,$data,$method);
         $files=json_decode($files,true);        
         print_r($files);
?>

效果如下

Array( [session_id] => e6e9ed700083463ea95404f685a2a01d)

之后进行图片验证功能,运行代码,我这是按整个group组进行验证,返回的是组名

<?php
      function curlPost($url,$data,$method){
       $ch = curl_init();   //1.初始化
       curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
       //4.参数如下
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
           curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
       curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
 
       if($method=="POST"){//5.post方式的时候添加数据
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $tmpInfo = curl_exec($ch);//6.执行
 
       if (curl_errno($ch)) {//7.如果出错
           return curl_error($ch);
       }
       curl_close($ch);//8.关闭
       return $tmpInfo;
   }
        $filename="G:/phpstudy/WWW/dayi/face/zhangyushi.jpg";
        $data=array
                (
                  "api_secret"=>"iG5Z41L7cpMf8zLKu7s5iR31pk2LL_8k",
                   // "url"=>"apicn.faceplusplus.com",
                   "api_key"=>"bf9139598d8aefe5b9045869698be798",
                  "group_name"=>"zhangyushi",
                  "img"=>new CURLFile($filename),
                );
         $url="https://apicn.faceplusplus.com/v2/recognition/identify";
         $method="POST";
         $files=curlPost($url,$data,$method);
         $files=json_decode($files,true);
         $face = $files['face'];  
$face1 = $face[0];  
$candidates = $face1['candidate'];  
$person_identify = $candidates[0];  
$identify_name = $person_identify['person_name'];  
echo $identify_name."<br>";      
         // print_r($files);
?>

代码运行成功,返回(组名,表示验证的这个图片属于yushi的)    

yushi




0 0