您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何在微信小程序中實現(xiàn)人臉識別功能,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
首先是wxml
<view class="container"> <view class="userinfo"> <image class="userinfo-avatar" mode="aspectFit" src="{{tempFilePaths}}" background-size="cover"></image> <text class="userinfo-tips">{{userInfo.tips}}</text> </view> <view class="usermotto"> <button class="button" type="primary" bindtap="chooseimage">{{motto}}</button> </view> </view>
然后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: '鑒定中,請稍候', icon: 'loading', duration: 2000 }) wx.request({ url: '', //這里是阿里云人臉識別功能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:'未檢測到人臉' } }) 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
首先是阿里云人臉識別功能代碼
<?php $akId = ""; $akSecret = ""; $image_url = ""; //更新api信息 $url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute"; $content = array( 'type' => 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ù),請用json_encode()進行編碼 ) ); $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); ?>
然后是后臺圖片上傳服務(wù)器功能,這里的代碼也是我參考大佬,然后自己修改的【侵刪】
<?php date_default_timezone_set("Asia/Suzhou"); //設(shè)置時區(qū) $code = $_FILES['file'];//獲取小程序傳來的圖片 if(is_uploaded_file($_FILES['file']['tmp_name'])) { //把文件轉(zhuǎn)存到你希望的目錄(不要使用copy函數(shù)) $uploaded_file=$_FILES['file']['tmp_name']; $username = "image"; //我們給每個用戶動態(tài)的創(chuàng)建一個文件夾 $user_path=$_SERVER['DOCUMENT_ROOT']."/WeChatphp/".$username; // DOCUMENT_ROOT是你域名配置的根目錄,后面的目錄可自己調(diào)整 //判斷該用戶文件夾是否已經(jīng)有這個文件夾 if(!file_exists($user_path)) { mkdir($user_path); } $file_true_name=$_FILES['file']['name']; $move_to_file_1 = time().rand(1,1000)."-".date("Y-m-d").substr($file_true_name,strrpos($file_true_name,".")); $move_to_file=$user_path."/".$move_to_file_1;//strrops($file_true,".")查找“.”在字符串中最后一次出現(xiàn)的位置 if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) { echo $move_to_file_1; } else { echo "上傳失敗".date("Y-m-d H:i:sa"); } } else { echo "上傳失敗".date("Y-m-d H:i:sa"); } ?>
以上就是如何在微信小程序中實現(xiàn)人臉識別功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。