最近学习了人脸识别,就是判断某个图片是否存在人脸,此次实现功能是,如果此图片存在一张人脸,则存入数据,多张人脸或无人脸均返回人脸过多或人脸不存在。
首先使用百度云,并在小程序中关联上百度云账号,在百度云中找到人脸识别功能,通过API Key 和Secret Key 来初始化人脸识别,代码如下
private function init_face(){
$app_id = '';
$app_key = '';
$secret_key = '';
$dir = APP_PATH .'/face_sdk/';
require_once $dir .'AipFace.php';
return new \AipFace($app_id,$app_key,$secret_key);
}
检查图片是否存在,代码如下
$file='';
if(!file_exists($file)){
die('文件不存在');
}
下面是识别人脸的代码
$image= base64_encode(file_get_contents($file));
$options=array();
$options['max_face_num']=2;
$client=$this->init_face();
$ret=$clie