十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
本篇文章給大家分享的是有關(guān)如何在微信小程序中實(shí)現(xiàn)人臉識(shí)別功能,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

首先是wxml
{{userInfo.tips}}
然后js代碼
var app = getApp()
Page({
data: {
motto: '上傳靚照',
userInfo: {},
tempFilePaths: ''
},
chooseimage: function () {
var that = this;
wx.chooseImage({ //選擇圖片
count: 1,
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths
that.setData({
tempFilePaths: tempFilePaths[0]
})
wx.uploadFile({ //上傳圖片
url: '', //這里是你php的路徑!!
filePath: tempFilePaths[0],
name: 'file',
header: {
'content-type': 'multipart/form-data'
},
success: function (res) {
console.log("add success", res.data);
that.uploadImage(res.data);
wx.showToast({
title: "圖片上傳成功",
icon: 'success',
duration: 700
})
}
})
}
})
},
//事件處理函數(shù)
uploadImage: function(picName) {
var that = this
wx.showToast({
title: '鑒定中,請(qǐng)稍候',
icon: 'loading',
duration: 2000
})
wx.request({
url: '', //這里是阿里云人臉識(shí)別功能php的路徑
data: {
type: 0,
image_url: picName,
},
header: {
'Content-Type': 'application/json'
},
// filePath: tempFilePaths[0],
name: 'file',
success: function(res){
console.log(res.data)
wx.hideToast()
var data = res.data;
var sex = data.gender;
const genders = {
'Male': '基佬',
'Female': '小仙女'
}
if(data.face_num == 0){
that.setData({
userInfo:{
tips:'未檢測(cè)到人臉'
}
})
return
} else {
if (sex == 0) {
that.setData({
userInfo: {
tips: data.face_num + '位' + data.age + '歲的' + genders.Female
}
})
} else {
that.setData({
userInfo: {
tips: data.face_num + '位' + data.age + '歲的' + genders.Male
}
})
}
return
}
}
})
},
onLoad: function () {
console.log('onLoad');
},
onShareAppMessage: function () {
}
})最后上php
首先是阿里云人臉識(shí)別功能代碼
0,
'image_url' => $image_url
);
$options = array(
'http' => array(
'header' => array(
'accept'=> "application/json",
'content-type'=> "application/json",
'date'=> gmdate("D, d M Y H:i:s \G\M\T"),
'authorization' => ''
),
'method' => "POST", //可以是 GET, POST, DELETE, PUT
'content' => json_encode($content)//如有數(shù)據(jù),請(qǐng)用json_encode()進(jìn)行編碼
)
);
$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))
$path = $urlObj["path"];
else
$path = $urlObj["path"]."?".$urlObj["query"];
$body = $http['content'];
if(empty($body))
$bodymd5 = $body;
else
$bodymd5 = base64_encode(md5($body,true));
$stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date']."\n".$path;
$signature = base64_encode(
hash_hmac(
"sha1",
$stringToSign,
$akSecret, true));
$authHeader = "Dataplus "."$akId".":"."$signature";
$options['http']['header']['authorization'] = $authHeader;
$options['http']['header'] = implode(
array_map(
function($key, $val){
return $key.":".$val."\r\n";
},
array_keys($options['http']['header']),
$options['http']['header']));
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context );
echo($file);
?>然后是后臺(tái)圖片上傳服務(wù)器功能,這里的代碼也是我參考大佬,然后自己修改的【侵刪】
以上就是如何在微信小程序中實(shí)現(xiàn)人臉識(shí)別功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。