亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Django調(diào)用百度AI接口實(shí)現(xiàn)人臉注冊(cè)登錄代碼實(shí)例

瀏覽:133日期:2022-07-06 11:19:37

面部識(shí)別----考勤打卡、注冊(cè)登錄、面部支付等等...感覺(jué)很高大上,又很方便,下面用python中的框架--django完成一個(gè)注冊(cè)登錄的功能,調(diào)用百度AI的接口,面部識(shí)別在網(wǎng)上也有好多教程,可以自己建模,訓(xùn)練模型,但是這都需要大量的數(shù)據(jù)去提高模型的準(zhǔn)確度,我們直接用了百度AI的接口,十分的快捷、高效、準(zhǔn)確,下來(lái)碼一下代碼啦!!

首先需要在百度AI官網(wǎng)注冊(cè)一個(gè)應(yīng)用,免費(fèi),并提供強(qiáng)大的人臉庫(kù)。

1.注冊(cè)表單

<div class='tab-content'> <div data-content='signup'> <!-- <form action='{% url ’regist’ %}' method='POST'> --><div class='row form-group'> <div class='col-md-12'> <input type='text' placeholder='用戶(hù)名'> </div></div><div class='row form-group'> <div class='col-md-12'> <input type='text' placeholder='手機(jī)號(hào)'> </div></div><div class='row form-group'> <div class='col-md-12'> <input type='password' placeholder='密碼'> </div></div><div class='row form-group'> <div class='col-md-12'> <!-- <input type='text' placeholder='驗(yàn)證碼'> <input type='button' value=' 獲取驗(yàn)證碼' id='zphone'> --> </div></div><div class='row form-group'> <div class='col-md-12'> <label for='password2'><font color=’green’>新用戶(hù)點(diǎn)擊注冊(cè)會(huì)有面部特征收集哦!</font></label> </div></div><div class='row form-group'> <div class='col-md-12'> <input type='submit' value='注冊(cè)' id='regist'> </div></div> <!-- </form> --> </div>

2.注冊(cè)時(shí)調(diào)用攝像頭,ajax封裝給后端的數(shù)據(jù)

<!-- jQuery --> <script src='http://www.aoyou183.cn/static/assets/js/jquery.min.js'></script> <!-- jQuery Easing --> <script src='http://www.aoyou183.cn/static/assets/js/jquery.easing.1.3.js'></script> <!-- Bootstrap --> <script src='http://www.aoyou183.cn/static/assets/js/bootstrap.min.js'></script> <!-- Waypoints --> <script src='http://www.aoyou183.cn/static/assets/js/jquery.waypoints.min.js'></script> <!-- Carousel --> <script src='http://www.aoyou183.cn/static/assets/js/owl.carousel.min.js'></script> <!-- countTo --> <script src='http://www.aoyou183.cn/static/assets/js/jquery.countTo.js'></script> <!-- Magnific Popup --> <script src='http://www.aoyou183.cn/static/assets/js/jquery.magnific-popup.min.js'></script> <script src='http://www.aoyou183.cn/static/assets/js/magnific-popup-options.js'></script> <!-- Main --> <script src='http://www.aoyou183.cn/static/assets/js/main.js'></script><script> !(function () { // 老的瀏覽器可能根本沒(méi)有實(shí)現(xiàn) mediaDevices,所以我們可以先設(shè)置一個(gè)空的對(duì)象 if (navigator.mediaDevices === undefined) {navigator.mediaDevices = {}; } if (navigator.mediaDevices.getUserMedia === undefined) {navigator.mediaDevices.getUserMedia = function (constraints) { // 首先,如果有g(shù)etUserMedia的話(huà),就獲得它 var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; // 一些瀏覽器根本沒(méi)實(shí)現(xiàn)它 - 那么就返回一個(gè)error到promise的reject來(lái)保持一個(gè)統(tǒng)一的接口 if (!getUserMedia) { return Promise.reject(new Error(’getUserMedia is not implemented in this browser’)); } // 否則,為老的navigator.getUserMedia方法包裹一個(gè)Promise return new Promise(function (resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); });} } const constraints = {video: true,audio: false }; videoPlaying = false; v = document.getElementById(’v’); promise = navigator.mediaDevices.getUserMedia(constraints); promise.then(stream => {// 舊的瀏覽器可能沒(méi)有srcObjectif ('srcObject' in v) { v.srcObject = stream;} else { // 防止再新的瀏覽器里使用它,應(yīng)為它已經(jīng)不再支持了 v.src = window.URL.createObjectURL(stream);}v.onloadedmetadata = function (e) { v.play(); videoPlaying = true;}; }).catch(err => {console.error(err.name + ': ' + err.message); }) document.getElementById(’regist’).addEventListener(’click’, function () {if (videoPlaying) { mycanvas = document.getElementById(’canvas’); mycanvas.width = v.videoWidth; mycanvas.height = v.videoHeight; mycanvas.getContext(’2d’).drawImage(v, 0, 0); // 圖片數(shù)據(jù)轉(zhuǎn)換成數(shù)組 data = mycanvas.toDataURL(’image/webp’); document.getElementById(’photo’).setAttribute(’src’, data); // ajax提交數(shù)據(jù)到后臺(tái) $.ajax({ type:'POST', url:’http://127.0.0.1:8000/regist/’, data:{username:$('#username').val(),mobile:$(’#mobile’).val(),password:$(’#password’).val(),mobile_code:$(’#mobile_code’).val(),imagecontent:data}, dataType:'json', success:function(data){ alert(data.result) $(’#resText’).text(data[’result’]); if(data.code == 200){window.location.href=’http://127.0.0.1:8000/home/’ }else{alert(data.result); } } })} }, false);

3.將已經(jīng)注冊(cè)的應(yīng)用中的各種id和key貼上來(lái)

# 導(dǎo)入百度AIfrom django.apps import AppConfigfrom aip import AipFaceimport json# django內(nèi)置事務(wù)from django.db import transaction# 導(dǎo)入狀態(tài)碼from jyapp.ErrorCode import * # 官網(wǎng)給出的狀態(tài)碼,通過(guò)pandas讀出保存到# 百度AI基本信息class AppConfig(AppConfig): name = ’’ APP_ID = ’’ API_KEY = ’’ SECRECT_KEY = ’’ client = AipFace(APP_ID,API_KEY,SECRECT_KEY) client.setConnectionTimeoutInMillis(1000*5) client.setSocketTimeoutInMillis(1000*5)

4.注冊(cè)接口,按照接口文檔傳入必須的參數(shù),手機(jī)驗(yàn)證碼功能已在本文中注釋掉,需要時(shí)自行百度。

# 注冊(cè)class Regist(View): def get(self,request): return render(request,’moban_index.html’) def post(self,request): # 獲取前端數(shù)據(jù) imagecontent = request.POST.get(’imagecontent’) username = request.POST.get(’username’) mobile = request.POST.get(’mobile’) password = request.POST.get(’password’) # mobile_code = request.POST.get(’mobile_code’) # print(imagecontent,username,mobile,password,mobile_code) # mobile_code_right = request.session.get(’message_code’) if not all([imagecontent,username,mobile,password]): return JsonResponse({’result’:’注冊(cè)信息不能為空’}) # if mobile_code != mobile_code_right: # return JsonResponse({’result’:’請(qǐng)輸入正確的驗(yàn)證碼’}) else: # 驗(yàn)證該用戶(hù)是否存在 user = models.User.objects.filter(mobile=mobile) if user:return JsonResponse({’result’:’該用戶(hù)已存在,請(qǐng)直接登錄’}) else:try: # 引入事務(wù) with transaction.atomic(): # 分割字符串 base_data = imagecontent.split(’,’)[1] # base64解碼 base64_decode = base64.b64decode(base_data) # 圖片寫(xiě)入本地 with open(’static/image/’+mobile+’.jpeg’, ’wb’) as f: f.write(base64_decode) # 添加到mysql數(shù)據(jù)庫(kù) models.User.objects.create( imagecontent = ’static/image/’+mobile+’.jpeg’, # 可以根據(jù)需求是否保存注冊(cè)照片到數(shù)據(jù)庫(kù),也可以通過(guò)百度AI人臉庫(kù)查看 username = username, mobile = mobile, password = password, ) imageType = ’BASE64’ groupId = ’usergroup’ # 自定義 userId = mobile # 加入可選參數(shù) options = {} options[’user_info’] = username options[’quality_control’] = ’NORMAL’ options[’liveness_control’] = ’LOW’ result = AppConfig.client.addUser(base_data,imageType,groupId,userId,options) print(result) error_code = result[’error_code’] if isinstance(error_code,int) and error_code == 0: request.session[’mobile’] = mobile return JsonResponse({’code’:200,’result’:’注冊(cè)成功’}) # return JsonResponse({’result’:’注冊(cè)成功’}) else: error = ErrorCode().getErrorInfo(error_code) return JsonResponse({’result’:’{}’.format(error)})except: return JsonResponse({’result’:’注冊(cè)失敗’})

5.登錄.html

<div data-content='login'> <!-- <form action='{% url ’login’ %}' method='POST'> --><div class='row form-group'> <div class='col-md-12'> <input type='text' placeholder='請(qǐng)輸入手機(jī)號(hào)'> </div></div><div class='row form-group'> <div class='col-md-12'> <input type='password' placeholder='請(qǐng)輸入密碼'> </div></div><div class='row form-group'> <div class='col-md-12'> <input type='submit' value='密碼登陸' id='login'> <input type='submit' value='人臉登陸' id='login_face'> </div></div> <!-- </form> --> </div>

6.ajax封裝登錄信息

document.getElementById(’login_face’).addEventListener(’click’, function () {if (videoPlaying) { mycanvas = document.getElementById(’canvas’); mycanvas.width = v.videoWidth; mycanvas.height = v.videoHeight; mycanvas.getContext(’2d’).drawImage(v, 0, 0); data = mycanvas.toDataURL(’image/webp’); document.getElementById(’photo’).setAttribute(’src’, data); $.ajax({ type:'POST', url:’http://127.0.0.1:8000/login_face/’, data:{mobile:$(’#mobile1’).val(),imagecontent:data}, dataType:'json', success:function(data){ $(’#resText’).text(data[’result’]); document.getElementById(’photo’).setAttribute(’src’,’static/’+data[’point72src’]); console.log(data[’point72src’]) if(data.code == 200){alert(data.result)window.location.href=’http://127.0.0.1:8000/idcard/’ }else{alert(data.result); } } })} }, false);

7.人臉快速登錄

class Login_face(View): def get(self,request): return render(request,’moban_index.html’) def post(self,request): imagecontent = request.POST.get(’imagecontent’) mobile = request.POST.get(’mobile’) if not all([imagecontent,mobile]): return JsonResponse({’code’:100,’result’:’登錄信息不能為空’}) else: user = models.User.objects.filter(mobile=mobile) if not user:return JsonResponse({’code’:113,’result’:’用戶(hù)不存在’}) else:base_data = imagecontent.split(’,’)[1]imageType = ’BASE64’groupIdList = ’usergroup’# 加入可選參數(shù)options = {}options[’max_user_num’] = 1options[’quality_control’] = ’NORMAL’options[’liveness_control’] = ’LOW’# options[’user_id’] = mobileresult = AppConfig.client.search(base_data,imageType,groupIdList,options)print(result)error_code = result[’error_code’]try: user_id = result[’result’][’user_list’][0][’user_id’] score = result[’result’][’user_list’][0][’score’] if isinstance(error_code,int) and error_code == 0 and user_id == mobile and score >= 90: request.session[’mobile’] = mobile return JsonResponse({’code’:200,’result’:’快速登錄成功’}) else: error = ErrorCode().getErrorInfo(error_code) return JsonResponse({’result’:’{}’.format(error)})except: error = ErrorCode().getErrorInfo(error_code) return JsonResponse({’result’:’{}’.format(error)})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: 百度
相關(guān)文章:
主站蜘蛛池模板: 欧美一级毛片免费观看软件 | 一级黄色激情片 | aaaaaa国产毛片孕妇版 | 一本大道一卡2卡三卡4卡麻豆 | 国产精品你懂得 | 欧美日韩国产在线人成dvd | 日本在线播放一区 | 免费成人毛片 | 国模精品视频一区二区三区 | 欧美精品大片 | 丁香六月婷婷精品免费观看 | 777在线视频 | 一级一片免费视频播放 | 免费黄色在线网址 | 精品视频一区二区三区在线播放 | 爱爱视频免费看 | a级毛片基地 | 国产成人精品日本亚洲专区6 | 狠狠做久久深爱婷婷97动漫 | 亚洲免费色图 | 91情侣在线偷精品国产 | 久久窝窝国产精品午夜看15 | 国产性生活 | 久久婷婷五综合一区二区 | 日本久久久久久久中文字幕 | 污污网站在线免费观看 | 精品国产一区二区三区不卡在线 | 在线看的毛片 | 久久久久久久免费 | 91嫩草视频在线观看 | 亚洲一区二区三区高清不卡 | 一级片黄色免费 | 亚洲九九色 | 性生活毛片 | 1024国产在线 | 麻豆传媒国产 | 国产伦精一区二区三区 | 亚洲黄色片子 | 国产精品青草久久久久婷婷 | 免费在线观看中日高清生活片 | 内地精品露脸自拍视频香蕉 |