创建一个thinkphp3的框架,创建一个小程序。
在tp3框架里创建公共文件,function.php,生成guid
//生成guid
function guid(){
if(function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);
$charid = strtoupper(md5(uniqid(rand(),true)));
$hyphen = chr(45);
$uuid = chr(123)
.substr($charid,0,8).$hyphen
.substr($charid,8,4).$hyphen
.substr($charid,12,4).$hyphen
.substr($charid,16,4).$hyphen
.substr($charid,20,12)
.chr(125);
return $uuid;
}
}
//生成变种guid
function myguid(){
$guid = guid();
$guid = trim($guid,'{}');
$guid = str_replace('-', '_', $guid);
return $guid;
}
然后创建indexcontroller.class.php
初始化人脸识别
//初始化人脸识别
private function init_face(){
$APP_ID='*****';
$API_KEY='*****';
$SECRET_KEY='****';
$dir=APP_PATH . '/face_sdk/';
require_once $dir . 'AipFace.php';
return new \AipFace($APP_ID,$API_KEY,$SECRET_KEY);
}
public function index($no,$name,$sex,$age){
$data['no']=$no;
$data['name']=$name;
$data['sex']=$sex;
$data['age']=$age;
$data['guid'] = myguid();
$id=M('student')->add($data);
if($id){
return $this->ajaxReturn(array('error'=>false,'msg'=>'添加成功!','guid'=>$student['guid']));
}else{
return $this->ajaxReturn(array('error'=>true,'msg'=>'添加出错'));
}
}
获取小组
private function face_group(){
//组名
$groupname='1002';
$client=$this->init_face();
$ret=$client->getGroupList();
if($ret['error_code']==0){
$grouplist=$ret['result']['group_id_list'];
if(in_array($groupname, $grouplist)){
return $groupname;
}else{
$ret = $client->groupAdd($groupname);
if($ret['error_code']==0){
return $groupname;
}else{
return false;
}
}
}else{
return false;
}
}
删除小组
//删除组
private function del_facegroup(){
$client = $this->init_face();
$ret = $client->groupDelete('1001');
print_r($ret);
}
人脸识别
//人脸识别
public function facevalid(){
$file = './Uploads/1.jpg';
if(!file_exists($file)){
die('文件不存在');
}
$image = base64_encode(file_get_contents($file));
//如果有可选参数
$options =array();
$options["max_face_num"]=2;
$client=$this->init_face();
$ret=$client->detect($image,'BASE64',$options);
print_r($ret);
if($ret['error_code']==0){//有人脸
$result=$ret['result'];
$face_num=$result['face_num'];
if(1 == $face_num){//人脸数量为1
$face_probability = $result['face_list'][0]['face_probability'];
if(1 == $face_probability){//可靠性为1
$guid=myguid();
$group=$this->face_group();
$client->addUser($image,'BASE64',$group,$guid);
echo "人脸检测完成,已入库";
}else{
die('可靠性为:' . $face_probability);
}
}else{
die('人脸数量大于1');
}
}else{
die('没有人脸');
}
}
上传文件
public function upload($id=''){
if(empty($id)){
return false;
}
$no = M("student")->where("id={$id}")->getField("no");
$dir = "./Uploads/studentface";//上传文件路径
if(!file_exists($dir)){
mkdir($dir,0777,true);
}
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 2048000 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = $dir; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$upload->saveName = $no;
$upload->replace = true;
$upload->autoSub = false;
// 上传文件
$info = $upload->uploadOne($_FILES['file']);
if(!$info) {// 上传错误提示错误信息
echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);
}else{// 上传成功
$file = $dir . $info['savePath'].$info['savename'];
$image = base64_encode(file_get_contents($file));
$this->facevalid($no,$image);
$m=M('head');
$data = $m->where("no='{$no}'")->find();
if($data){
//有数据,更新
$m->where("no='{$no}'")->save(array('base64'=>$image,'path'=>$file));
}else{
//无数据,添加
$m->add(array('no'=>$no,'base64'=>$image,'path'=>$file));
}
echo "采集照片成功";
}
}
小程序首页
上传照片页