PHP实现人脸检测功能,简单易用

发布时间:2018-12-18 16:23:34编辑:丝画阁阅读(1203)

PHP可以实现的功能也是很多

这次使用接口的形式,实现人脸检测功能

下面为代码展示

class Youtu
{
 public function index()
 {
 try{
 $file = request()->file('image');
 //目录
 $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
 if ($info) {
 $res = $this->Youtu(ROOT_PATH . 'public' . DS . 'uploads' . DS . $info->getSaveName());
 if ($res['errormsg'] == 'OK') {
 $data = $this->formatData($res['face'][0]);
 out_put(200, $data);
 } else {
 if($res['errormsg'] == 'SDK_IMAGE_FACEDETECT_FAILED'){
 $err = '人脸检测失败';
 }else{
 $err = $res['errormsg'];
 }
 exception($err);
 }
 } else {
 // 上传失败获取错误信息
 exception('图片上传失败');
 }
 }catch (Exception $e){
 out_put(400, $e->getMessage());
 }
 }
 public function formatData($data)
 {
 //判断男女
 if($data['gender'] >= 50 ){
 $gender = '男';
 }else{
 $gender = '女';
 }
 $res = [
 'age' => $data['age'],
 'beauty' => $data['beauty'],
 'glass' => $data['glass'],
 'gender' => $gender,
 ];
 return $res;
 }
 public function Youtu($file_path)
 {
 //配置
 $appid = 'appid ';
 $secretId = 'secretId';
 $secretKey = 'secretKey';
 $userid = 'x';
 //加载类文件
 	hinkLoader::import('qqyoutusdk.TencentYoutuyun.Youtu', EXTEND_PATH);
 	hinkLoader::import('qqyoutusdk.TencentYoutuyun.Auth', EXTEND_PATH);
 	hinkLoader::import('qqyoutusdk.TencentYoutuyun.Conf', EXTEND_PATH);
 	hinkLoader::import('qqyoutusdk.TencentYoutuyun.Http', EXTEND_PATH);
 //初始化类
 TencentYoutuyunConf::setAppInfo($appid, $secretId, $secretKey, $userid, TencentYoutuyunconf::API_YOUTU_END_POINT);
 // 人脸检测 调用列子
 $uploadRet = TencentYoutuyunYouTu::detectface($file_path, 1);
 return $uploadRet;
 }
}

关键字