飞道的博客

百度人脸识别+php+微信小程序返回年龄和颜值

384人阅读  评论(0)

1:百度云注册账号登录

首页-》产品-》人工智能-》人脸识别与分析
https://cloud.baidu.com/product/face/detect
https://console.bce.baidu.com/ai/?_=1583931927969#/ai/face/app/detail~appId=1513185
创建应用-》获取secert key和api key

2:在服务器php文件中yanzhi.php中写入以下代码

<?php header('Content-Type:application/json; charset=utf-8'); $access_token = '自己的access_token';//获取方法在第三步 function checkPictureToCheckFace($image_path, $access_token) { $url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' . $access_token; $param = array( 'image' => base64EncodeImage($image_path), 'image_type' => 'BASE64', 'face_field' => 'quality,age,beauty,expression,gender' ); $param = json_encode($param); $result = execCurl($url, $param); $res = $result; if($result['result'] == false) return $result; $data = json_decode($result['data'], true); if($data['error_code'] != 0) return array('result' => false, 'err_msg' => '上传的人脸不符合要求,请重新上传五官清晰的真实头像'); $err_result = array('result' => false, 'err_msg' => '上传的人脸不符合要求,请重新上传五官清晰的真实头像!'); $quality = $data['result']['face_list'][0]['quality']; $age = $data['result']['face_list'][0]['age']; $beauty = $data['result']['face_list'][0]['beauty']; if($quality['occlusion']['left_eye'] > 0.6) return $err_result; // 左眼被遮挡的阈值 if($quality['occlusion']['right_eye'] > 0.6) return $err_result; // 右眼被遮挡的阈值 if($quality['occlusion']['nose'] > 0.7) return $err_result; // 鼻子被遮挡的阈值 if($quality['occlusion']['mouth'] > 0.7) return $err_result; // 嘴巴被遮挡的阈值 if($quality['occlusion']['left_cheek'] > 0.8) return $err_result; // 左脸颊被遮挡的阈值 if($quality['occlusion']['right_cheek'] > 0.8) return $err_result; // 右脸颊被遮挡的阈值 if($quality['occlusion']['chin_contour'] > 0.6) return $err_result; // 下巴被遮挡阈值 if($quality['blur'] != 0 && $quality['blur'] > 0.7) return $err_result; // 模糊度范围 0是最清晰,1是最模糊 if($quality['illumination'] < 40) return $err_result; // 光照范围 if($quality['completeness'] == 0) return $err_result; // 人脸完整度 0为人脸溢出图像边界,1为人脸都在图像边界内 $res = array('result' => true,'data' =>array('age'=>$age,'beauty'=>$beauty)); // return $res; echo json_encode($res); //var_dump($res['data']); } function execCurl($url, $param) { if (empty($url) || empty($param)) { return array('result' => false, 'err_msg' => 'url or param is null'); } $curl = curl_init();//初始化curl curl_setopt($curl, CURLOPT_URL, $url);//抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $param); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); $data = curl_exec($curl);//运行curl $curl_err_msg = curl_error($curl); curl_close($curl); if($data === false) { return array('result' => false, 'err_msg' => $curl_err_msg); }else { return array('result' => true, 'data' => $data); } } function base64EncodeImage($image_path) { $base64_image = ''; $image_info = getimagesize($image_path); $image_data = fread(fopen($image_path, 'r'), filesize($image_path)); $base64_image = chunk_split(base64_encode($image_data)); return $base64_image; } $image_path = 'images/1.jpg'; $result = checkPictureToCheckFace($image_path, $access_token); # 3:获取access_token function execCurl($url, $param) { if (empty($url) || empty($param)) { return array('result' => false, 'err_msg' => 'url or param is null'); } $curl = curl_init();//初始化curl curl_setopt($curl, CURLOPT_URL, $url);//抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $param); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); $data = curl_exec($curl);//运行curl $curl_err_msg = curl_error($curl); curl_close($curl); if($data === false) { return array('result' => false, 'err_msg' => $curl_err_msg); }else { return array('result' => true, 'data' => $data); } } $url = 'https://aip.baidubce.com/oauth/2.0/token'; $param = array( 'grant_type' => 'client_credentials', 'client_id' => '你的API Key', 'client_secret' => '你的Secret Key' ); $result = execCurl($url, $param); ## 4:微信小程序利用点击事件 testBeauty: function (options){ wx.request({ url: 'http://localhost:88/wxapp/yanzhi.php', header: { 'content-type': 'application/json' // 默认值 }, method: 'GET', success: resu => { this.setData({ age: resu.data.data.age}) this.setData({ beauty: resu.data.data.beauty }) } }); }, ## 5:页面显示 颜值测试 {{age}} {{beauty}}
转载:https://blog.csdn.net/lily_808/article/details/104805724
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场