文章目录
申请百度云API
登录网站
打开开发者文档
- 获取SDK PHP人脸识别SDK
代码部分
PHP代码
upload.php文件(图片上传文件)
<?php
header("Content-type: text/html; charset=utf8");
$_FILES['file']['name'] = iconv('utf-8','gb2312',$_FILES['file']['name']);
echo upload($_FILES['file']);
function upload($file){
$result = "失败";
//设置一个后缀名与mime的映射关系
$type_map = array(
'.jpg'=>array('image/jpeg')
);
//后缀
$allow_ext_list = array('.jpg');
$ext = strtolower(strrchr($file['name'],'.'));
if(!in_array($ext,$allow_ext_list)){
return false;
}
//目录存储
$up_loadpath = 'imgs/';
$sub_dir = date('Ymdh');
$prefix = 'bee_';
$name = uniqid($prefix,true).$ext;
if(move_uploaded_file($file['tmp_name'],$up_loadpath.$name)){
return $name;
}else{
return $result;
}
}
face.php文件
<?php
//导入SDK
require_once 'plugin/aip-php-sdk-2.2.4/AipFace.php';
//从微信小程序中获取图片地址
$img = $_POST['img'];
//定义自己的信息
const APP_ID = '';//自己的APP_ID
const API_KEY = '';//自己的API_KEY
const SECRET_KEY = '';//自己的SECRET_KEY
//实例化对象
$client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
//设置图片编码
$img = base64_encode(file_get_contents($img));
//设置图片类型,必须大写
$imageType = "BASE64";
//最大人脸数
$opstion['max_face_num'] = 5;
//设置返回值
$opstion['face_field'] = 'age,beauty,gender,expression';
// 调用人脸检测
$result = $client->detect($img, $imageType,$opstion);
//将结果转化为json
echo json_encode($result);
小程序部分代码
代码分析
- 百度API只能识别已存在的文件,所以得先存下文件来,才能识别
- 存了文件之后,将文件名返回给小程序,小程序再将文件名传递给百度云API
- 百度云API获取到文件之后,将文件处理,返回一个json数据给微信小程序,然后微信小程序处理获取到的数据
wxml文件
<view>
<image src='../../image/renlianshibie.png' class='icon' bindtap='face'/>
<view class='description'>
<text>
年龄:{{age}}
性别:{{sex}}
颜值:{{beauty}}
表情:{{expression}}
</text>
</view>
</view>
wxss文件
.icon{
width: 60%;
height: 400rpx;
margin-left: 20%;
margin-top: 100rpx;
}
.description{
text-align: center;
line-height: 70rpx;
margin-top: 70rpx;
}
js文件
Page({
/**
* 页面的初始数据
*/
data: {
img:"",
age:"",
sex:"",
beauty:"",
expression:""
},
upload:function(re){
},
face: function () {
console.log("face接口");
var that = this;
//调用上传文件接口
wx.chooseImage({
count: 1, //最多可以选择的图片总数
sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['camera'],
success(res) {
const tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'url地址', // 仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData: {
user: 'test'
},
success(res) {
const data = res.data;
console.log(res.data);
that.setData({
img: res.data,
});
//调用人脸识别接口
wx.request({
url: 'url地址',
data: {
img: "imgs/" + that.data.img,
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log(res.data['result'])
var result = res.data['result'];
var expression = result.face_list[0]['expression']['type'];
if (expression == 'laugh'){
expression = '大笑';
} else if (expression == 'smile'){
expression = '微笑';
}else if(expression == 'none'){
expression = '面无表情';
}
that.setData({
"age": result.face_list[0]['age'],
"sex": result.face_list[0]['gender']['type'],
"beauty": result.face_list[0]['beauty'],
"expression": expression
}),
console.log(
"age:" + result.face_list[0]['age'],
"颜值:" + result.face_list[0]['beauty'],
"性别:" + result.face_list[0]['gender']['type']
);
}
})
},
})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
}
})
有什么不会的请联系
[email protected]