您的位置:首页 > 编程语言 > PHP开发

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

2017-12-27 15:48 801 查看
文件目录:




dir.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

</html>
<?php
/**********************
一个简单的目录递归函数
第一种实现办法:用dir返回对象
***********************/
function tree($directory)
{
$mydir = dir($directory);
echo "<ul>\n";
while($file = $mydir->read())
{
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
{
echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n";
tree("$directory/$file");
}
else
echo "<li>$file</li>\n";
}
echo "</ul>\n";
$mydir->close();
}
//开始运行

//echo "<h2>目录为粉红色</h2><br>\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 "<b><font color='red'>文件名:</font></b>",$file,"<br><hr>";
listDir($dir."/".$file."/");
}
else
{
$file = iconv("gb2312","UTF-8",$file);
if($file!="." && $file!="..")
{
//echo $file."<br>";
//var_dump($file);
$file_name = strstr($file, '.', true);
//echo $file_name."<br>";
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
<?PHP
/**
* Class Facepp - Face++ PHP SDK
*
* @author Tianye
* @author Rick de Graaff <rick@lemon-internet.nl>
* @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
<style>
body
{
text-align:left;
height:500px;
width:600px;
top:50%;
margin-top:130px;
margin-left:550px;
background-image:url(./imgs/back.jpg);
background-position:center;
background-repeat:repeat-y;
}
</style>
<html>
<head>

<meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" />

</head>
<body>

<h1>使用说明:</h1>
<h2>训练部分:</h2>
1.训练只需要做一次就可以<br>
2.如果训练目录中的图片发生了改变,则需要再训练一次<br>
3.需要联网才可以使用啊<br>
4.训练时需要选择训练用的目录<br>
5.训练目录中每个人的图片都有一张就可以<br>
6.命名时用英文命名<br><br>
<input type = "button" value = "点击进入训练" onClick = "location='startTrain.php' "/>
<h2>测试部分:</h2>
1.测试时选择一张图片后输入即可<br>
2.请在recognition.php页面中使用$person_name变量<br>记录好英文文件名与中文人名的对应
<br>3.比如$person_name = array("ami" => "艾米", <br>"dongjian" => "张东健", "xiaowang" => "小王");
<br>4.'=>'前的是训练时的英文文件名,<br>箭头后边的则是这个英文名对应的中文名<br><br>
<input type = "button" value = "点击进入测试" onClick = "location='test.php' "/>

</body>
</html>

recognition.php
<style>
body
{
text-align:left;
height:500px;
width:600px;
top:50%;
margin-top:330px;
margin-left:650px;
background-image:url(./imgs/back.jpg);
background-position:center;
background-repeat:repeat-y;
}
</style>
<?php

//face = array(array('attribute'=>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."<br>";
$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."<br>";
*/
$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 "识别结果为:<br><br>".$person_name[$identify_name]."<br><br>";

?>
<form action = "test.php">
<input type = "submit" value = "继续测试">
</form>

startTrain.php
<style>
body
{
text-align:left;
height:500px;
width:600px;
top:50%;
margin-top:330px;
margin-left:590px;
background-image:url(./imgs/back.jpg);
background-position:center;
background-repeat:repeat-y;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试提交</title>
</head>
<body>

<form action="train.php" method="get">
请输入训练图像文件夹路径名:<br>比如D:/test
<br>
训练需要一段时间,请耐心等待呀O(∩_∩)O亲!
<br>

<input type="text" name="trainDir">
<br>

<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value = "重置">
</form>
</body>
</html>



test.php

<style>
body
{
text-align:left;
height:500px;
width:600px;
top:50%;
margin-top:330px;
margin-left:650px;
background-image:url(./imgs/back.jpg);
background-position:center;
background-repeat:repeat-y;
}
</style>
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" />

</head>
<body>
<form method = "get" action = "recognition.php">
请选择测试图片地址<br>
<input type = "file" name = "testImgPath">
<br>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value = "重置">
</body>
</html>

train.php
<style>
body
{
text-align:left;
height:500px;
width:600px;
top:50%;
margin-top:330px;
margin-left:590px;
background-image:url(./imgs/back.jpg);
background-position:center;
background-repeat:repeat-y;
}
</style>
<html>
<body>
<?php
require_once 'face.php';
require_once 'dir.php';
########################
### example ###
########################
$facepp = new Facepp();
$facepp->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";
?>

<form action = "test.php" name = "test" method = "get">
<input type = "submit" name = "submit" value = "开始测试吧">
</form>
</body>
</html>
<?
//$response = $facepp->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);
}
*/
?>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: