原文地址:https://www.jb51.net/article/158886.htm

前言

这是一篇关于一个原创微信小程序开发过程的原创文章。涉及到的核心技术是微信小程序开发方法和百度云人脸识别接口。小程序的主体是一个用于个人密码存储的密码管理器,在登陆注册阶段,需要调用百度云人脸识别接口以及百度云在线人脸库的管理接口。本文主要涉及登陆注册模块的实现,而且不需要PHP后台代码,完全在线调用接口实现,希望后来的你能有所收获!

步骤 

步骤 涉及接口(百度云)
拍摄或者相册选择 并 上传比对样本照片到 人脸库 人脸库管理接口(main:人脸注册)
拍摄照片并上传,云服务器在线比对 人脸库照片与上传图片的相似度 人脸识别接口
获取返回结果(相似度) 人脸识别接口

开发过程

1.拍摄人脸图片上传至人脸库—注册

准备工作:需要在百度云注册(或者直接用百度云盘app扫码登陆),并创建人脸识别的应用。(完全免费)

具体如下:

百度云:https://cloud.baidu.com/

注册完成后(或者直接扫码登陆),进入管理控制台->产品服务->人工智能->人脸识别->创建应用->填写必要信息->立即创建

至此,我们已经创建好了人脸识别的应用。接下来,进入应用列表,找到我们才新建的应用,查看人脸库,我们需要创建用户组(用来集中管理小程序的用户人脸照片)

新建组(id不要太复杂,后面还要用的。)

至此,我们已经完成了在云上的所有必要操作。下面,我们在小程序中,拍照上传即可。

拍照上传

需要在pages中新建一个目录,用来承载我们的登陆注册模块。就假定为 camera{camera.js camera.wxml camera.wxss camera.json}

主要文件自然是 *.wxml 和 *.js 了。

camera.wxml

  1. <!-- camera.wxml相机大小需要从重新设置 -->
  2. <camera device-position="front" flash="off" binderror="error" style="width:100%;height:400px;"></camera>
  3. <!-- 需要使用 button 来授权登录 -->
  4. <button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" type="primary">
  5. 授权
  6. </button>
  7. <view wx:else>请升级微信版本</view>
  8. <!-- 拍照按钮 -->
  9. <button type="primary" bindtap="takePhoto"> 拍照 </button>
  10. <button bindtap=\'btnreg\'> 注册须知 </button>

camera.js

调用wxAPI takePhoto() 拍照并获取src -> wx.request() 访问百度云 用先前创建的应用的API Key & Screct Key 获取 access_token ->wx.request() 访问百度云 上传 所拍照片(要经过base64编码)详情可参考小程序API文档 以及 百度云API文档(接口以及于18年升级至v3)

  1. // camera.js
  2. const app = getApp()
  3. Page({
  4. data: {
  5. canIUse: wx.canIUse(\'button.open-type.getUserInfo\'),
  6. nickName: "",
  7. src: "",//图片的链接
  8. baidutoken: "",
  9. base64: "",
  10. msg: ""
  11. },
  12. //拍照
  13. takePhoto() {
  14. var that = this;
  15. //拍照
  16. const ctx = wx.createCameraContext()
  17. ctx.takePhoto({
  18. quality: \'high\',
  19. success: (res) => {
  20. this.setData({
  21. src: res.tempImagePath//获取图片
  22. })
  23. //图片base64编码
  24. wx.getFileSystemManager().readFile({
  25. filePath: this.data.src, //选择图片返回的相对路径
  26. encoding: \'base64\', //编码格式
  27. success: res => { //成功的回调
  28. this.setData({
  29. base64: res.data
  30. })
  31. }
  32. })
  33. this.getBaiduToken();
  34. }//拍照成功结束
  35. })//调用相机结束
  36.  
  37. //失败尝试
  38. wx.showToast({
  39. title: \'请重试\',
  40. icon: \'loading\',
  41. duration: 500
  42. })
  43. },
  44. error(e) {
  45. console.log(e.detail)
  46. },
  47. getBaiduToken:function(){
  48. var that=this;
  49. //acess_token获取,qs:需要多次尝试
  50. wx.request({
  51. url: \'https://aip.baidubce.com/oauth/2.0/token\', //是真实的接口地址
  52. data: {
  53. grant_type: \'client_credentials\',
  54. client_id: \'xxx\',//用你创建的应用的API Key
  55. client_secret: \'xx\'//用你创建的应用的Secret Key
  56. },
  57. header: {
  58. \'Content-Type\': \'application/json\' // 默认值
  59. },
  60. success(res) {
  61. that.setData({
  62. baidutoken: res.data.access_token//获取到token
  63. })
  64. that.uploadPhoto();
  65. }
  66. })
  67. },
  68. uploadPhoto:function()
  69. {
  70. var that=this;
  71. //上传人脸进行注册-----test
  72. wx.request({
  73. url: \'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=\' + this.data.baidutoken,
  74. method: \'POST\',
  75. data: {
  76. image: this.data.base64,
  77. image_type: \'BASE64\',
  78. group_id: \'tingdemo\',//自己建的用户组id
  79. user_id: \'ting\'//这里获取用户昵称
  80. },
  81. header: {
  82. \'Content-Type\': \'application/json\' // 默认值
  83. },
  84. success(res) {
  85. that.setData({
  86. msg: res.data.error_msg
  87. })
  88. console.log(that.data.msg)
  89. //做成功判断
  90. if (that.data.msg == \'SUCCESS\') {//微信js字符串请使用单引号
  91. wx.showToast({
  92. title: \'注册成功\',
  93. icon: \'success\',
  94. duration: 2000
  95. })
  96. wx.switchTab({
  97. url: \'../UI/ui\',
  98. })
  99. }
  100. }
  101. })
  102. },
  103. //获取用户信息
  104. bindGetUserInfo: function (e) {
  105. this.setData({
  106. nickName: e.detail.userInfo.nickName
  107. })
  108. debugger;
  109. wx.showToast({
  110. title: \'授权成功\',
  111. icon: \'success\',
  112. duration: 1000
  113. })
  114. },
  115. //先授权登陆,再拍照注册
  116. btnreg: function () {
  117. wx.showModal({
  118. title: \'注册须知\',
  119. content: \'先授权登陆,再拍照注册哦!网络可能故障,如果不成功,请再试一下!\',
  120. })
  121. }
  122. })

这里要多试几次,我以为可能由于网络的问题,会调用失败, 但其实是wx.request()是并发的,所以获取access_token和上传请求会冲突(可能没有获取到access_token就上传,会发生错误)。

另外,要开启微信小程序 IDE 的 不校验合法域名的选项(设置->项目设置 -> 勾选 不校验……)

至此,注册 就完成了(即获取用户昵称、拍照、上传人脸库注册。)

2.拍照上传在线人脸识别—登陆

找到指定用户组中与上传照片最相似的人脸并返回,比对结果。

我们仍然需要再建立一个页面来承载我们的登陆相关操作。就假定为 camera2

  1. <!--pages/Camera/verifyphoto.wxml-->
  2. <!-- verifyphoto.wxml相 -->
  3. <camera device-position="front" flash="off" binderror="error" class="camera"></camera>
  4. <!-- 拍照按钮 -->
  5. <button type="primary" bindtap="takePhoto"> 识别 </button>
  1. Page({
  2. data: {
  3. src:\'\',
  4. base64: "",
  5. baidutoken: "",
  6. msg: null
  7. },
  8. //拍照并编码
  9. takePhoto() {
  10. var that=this;
  11. //拍照
  12. const ctx = wx.createCameraContext()
  13. ctx.takePhoto({
  14. quality: \'high\',
  15. success: (res) => {
  16. that.setData({
  17. src: res.tempImagePath
  18. })
  19. //图片base64编码
  20. wx.getFileSystemManager().readFile({
  21. filePath: that.data.src, //选择图片返回的相对路径
  22. encoding: \'base64\', //编码格式
  23. success: res => { //成功的回调
  24. that.setData({
  25. base64: res.data
  26. })
  27. that.checkPhoto();
  28. }
  29. })
  30. }
  31. })
  32. wx.showToast({
  33. title: \'请重试\',
  34. icon: \'loading\',
  35. duration: 500
  36. })
  37. },
  38. error(e) {
  39. console.log(e.detail)
  40. },
  41. checkPhoto: function () {
  42. var that=this;
  43. //acess_token获取
  44. wx.request({
  45. url: \'https://aip.baidubce.com/oauth/2.0/token\', //真实的接口地址
  46. data: {
  47. grant_type: \'client_credentials\',
  48. client_id: \'xx\',
  49. client_secret: \'xx\'//用自己的
  50. },
  51. header: {
  52. \'Content-Type\': \'application/json\' // 默认值
  53. },
  54. success(res) {
  55. that.setData({
  56. baidutoken: res.data.access_token//获取到token
  57. })
  58. that.validPhoto();
  59. }
  60. })
  61. },
  62. validPhoto:function(){
  63. var that = this;
  64. //上传人脸进行 比对
  65. wx.request({
  66. url: \'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=\' + that.data.baidutoken,
  67. method: \'POST\',
  68. data: {
  69. image: this.data.base64,
  70. image_type: \'BASE64\',
  71. group_id_list: \'tingdemo\',//自己建的用户组id
  72. },
  73. header: {
  74. \'Content-Type\': \'application/json\' // 默认值
  75. },
  76. success(res) {
  77. that.setData({
  78. msg: res.data.result.user_list[0].score
  79. })
  80. if (that.data.msg > 80) {
  81. wx.showToast({
  82. title: \'验证通过\',
  83. icon: \'success\',
  84. duration: 1000
  85. }) //验证通过,跳转至UI页面
  86. wx.switchTab({
  87. url: \'../UI/ui\',
  88. })
  89. }
  90. }
  91. });
  92. }
  93. })

至此,我们的登陆也搞定了。

实际用到的样式

  1. .Top {
  2. position: absolute;
  3. top: 0rpx;
  4. width: 100%;
  5. bottom: 100rpx;
  6. }
  7. .csscamera {
  8. margin: 50rpx 5% 0 5%;
  9. padding: 5%;
  10. height: 70%;
  11. width: 80%;
  12. border: dashed 1rpx rgb(185, 185, 189);
  13. }
  14. .sumbit {
  15. position: absolute;
  16. bottom: 0rpx;
  17. height: 100rpx;
  18. width: 100%;
  19. }
  1. <view class="Top">
  2. <camera device-position="front" flash="off" binderror="error" class="csscamera"></camera>
  3. <!-- 拍照按钮 -->
  4. <button type="primary" bindtap="takePhoto" class="sumbit"> 识别 </button>
  5. </view>

 

版权声明:本文为luckyting原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/luckyting/articles/11316754.html