当前位置:   article > 正文

[uniapp] uniapp 实现人脸识别 示例_uniapp 人脸识别

uniapp 人脸识别

 

微信小程序实现人脸识别,具体应用场景   前端实现人脸信息采集  拍到正面照片  发送给后端

        该方法暂时只针对微信小程序

前端具体效果展示 如下:

 

我们分开来分开解释这个方法哈

         首先要新写一个页面用来跳转,具体页面的布局可以自由发挥 根据实际要求来 ,

        主要运用了  wx.initFaceDetect(Object object)   具体想了解更多可以去查看官方文档哈,链接放在下面了 wx.initFaceDetect(Object object) | Weixin public doc

        在项目里进入页面初始化人脸识别   可以识别到是否拍摄的是人脸  检测五官哈

具体实现方法   下面就直接贴代码了

        代码语法语句都放在了代码注释中

html部分:

  1. template>
  2. <view class="page-content">
  3. <view class="containerV">
  4. <view class="headerV">
  5. <view class="top-tips1">
  6. <view>请将正对手机,头部匹配摄像区域</view>
  7. </view>
  8. <view class="top-tips2">
  9. 为了便于识别认证,请拍摄本人头像
  10. </view>
  11. </view>
  12. <view class="contentV">
  13. <view class="mark"></view>
  14. <image v-if="tempImg" mode="widthFix" :src="tempImg" />
  15. <camera v-if='isAuthCamera' :device-position="devicePosition ?'front': 'back'" class="camera"
  16. flash="off" resolution='high' />
  17. <view v-show="!tempImg && tipsText" class="tipV">{{ tipsText }}</view>
  18. </view>
  19. <view class="footerV">
  20. <view style="width: 100%;">
  21. <view v-if="!tempImg" style="width: 100%;">
  22. <view class="privacyV">
  23. <view class="icon"></view>
  24. <view class="text">
  25. 照片隐私<text @click="handleJumpSecurityClick">安全保障</text>中…
  26. </view>
  27. </view>
  28. <view class="bottom-tips-2">该照片仅作为你认证的凭证</view>
  29. <view class="take-photo-bgV">
  30. <!-- 图片上传 -->
  31. <view v-show="true" class="btn-change-upload" @click="handleChooseImage" ></view>
  32. <!--拍照-->
  33. <view class="btn-take-photo" @click="handleTakePhotoClick" ></view>
  34. <!-- 切换镜头 -->
  35. <view class="btn-change-camera" @click="handleChangeCameraClick" ></view>
  36. </view>
  37. </view>
  38. <view class="confirmV" v-else>
  39. <view class="btn-cancel" @click="handleCancelClick">
  40. 取消
  41. </view>
  42. <view class="btn-ok" @click="handleOkClick">
  43. 确定
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>

下面是js部分   按钮都暂时用背景颜色来填充    自己更换所需要的图片哈

  1. <style lang="scss" scoped>
  2. .page-content {
  3. width: 100%;
  4. height: 100%;
  5. .containerV {
  6. width: 100%;
  7. height: 100%;
  8. .headerV {
  9. .top-tips1 {
  10. margin-top: 60rpx;
  11. color: #1C1C1C;
  12. font-size: 36rpx;
  13. text-align: center;
  14. }
  15. .top-tips2 {
  16. margin-top: 20rpx;
  17. color: #00AAFF;
  18. font-size: 28rpx;
  19. text-align: center;
  20. }
  21. }
  22. .contentV {
  23. position: relative;
  24. display: flex;
  25. flex-direction: column;
  26. align-items: center;
  27. justify-content: center;
  28. height: 661rpx;
  29. margin-top: 30rpx;
  30. .tipV {
  31. bottom: 30rpx;
  32. position: absolute;
  33. line-height: 90rpx;
  34. padding-left: 24rpx;
  35. padding-right: 24rpx;
  36. max-width: calc(100vw - 50rpx * 2);
  37. text-align: center;
  38. font-size: 30rpx;
  39. background: #000000;
  40. opacity: 0.75;
  41. color: #FFFFFF;
  42. border-radius: 16rpx;
  43. overflow: hidden;
  44. white-space: nowrap;
  45. text-overflow: ellipsis;
  46. z-index: 5;
  47. }
  48. .camera {
  49. width: 400upx;
  50. height: 400upx;
  51. border-radius: 50%;
  52. }
  53. .mark {
  54. position: absolute;
  55. left: 0;
  56. top: 0;
  57. z-index: 2;
  58. width: 750rpx;
  59. height: 100%;
  60. // background:deeppink;
  61. background-size: 750rpx 661rpx;
  62. }
  63. image {
  64. position: absolute;
  65. width: 100%;
  66. height: 100%;
  67. z-index: 3;
  68. }
  69. }
  70. .footerV {
  71. width: 100%;
  72. display: flex;
  73. flex-direction: row;
  74. align-items: center;
  75. justify-content: center;
  76. .privacyV {
  77. padding-top: 30rpx;
  78. display: flex;
  79. flex-direction: row;
  80. align-items: center;
  81. justify-content: center;
  82. .text {
  83. font-size: 30rpx;
  84. color: #1C1C1C;
  85. text-align: center;
  86. line-height: 42rpx;
  87. margin-left: 15rpx;
  88. text {
  89. font-size: 30rpx;
  90. color: #00AAFF;
  91. text-align: center;
  92. line-height: 42rpx;
  93. }
  94. }
  95. .icon {
  96. width: 40rpx;
  97. height: 47rpx;
  98. background:green;
  99. background-size: 100% auto;
  100. }
  101. }
  102. .bottom-tips-2 {
  103. margin-top: 20rpx;
  104. color: #999999;
  105. text-align: center;
  106. font-size: 26rpx;
  107. }
  108. .take-photo-bgV {
  109. width: 100%;
  110. margin-top: 30rpx;
  111. display: flex;
  112. flex-direction: row;
  113. align-items: center;
  114. justify-content: center;
  115. .btn-take-photo {
  116. margin: 0rpx 80rpx 0rpx 80rpx;
  117. width: 196rpx;
  118. height: 196rpx;
  119. background: yellow;
  120. background-size: 100% auto;
  121. }
  122. .btn-change-upload {
  123. left: 130rpx;
  124. width: 80rpx;
  125. height: 80rpx;
  126. background: blue;
  127. background-size: 100% auto;
  128. }
  129. .btn-change-camera {
  130. right: 130rpx;
  131. width: 80rpx;
  132. height: 80rpx;
  133. background:red;
  134. background-size: 100% auto;
  135. }
  136. }
  137. .confirmV {
  138. margin: 200rpx 100rpx 0rpx 100rpx;
  139. display: flex;
  140. flex-direction: row;
  141. align-items: center;
  142. justify-content: space-between;
  143. .btn-cancel {
  144. font-size: 32rpx;
  145. color: #1C1C1C;
  146. }
  147. .btn-ok {
  148. font-size: 32rpx;
  149. color: #00AAFF;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. </style>

js部分,一定要注意  上传图片接收到的路径和拍照接受到的路径

  1. <script>
  2. export default {
  3. name: 'index',
  4. components: {
  5. },
  6. data() {
  7. return {
  8. tipsText: '', // 错误文案提示
  9. tempImg: '', // 本地图片路径
  10. BASE_API,
  11. cameraEngine: null, // 相机引擎
  12. devicePosition: false, // 摄像头朝向
  13. isAuthCamera: true, // 是否拥有相机权限
  14. }
  15. },
  16. onLoad(options) {
  17. this.initData()
  18. },
  19. mounted(){
  20. },
  21. methods: {
  22. // 初始化相机引擎
  23. initData() {
  24. // #ifdef MP-WEIXIN
  25. // 1、初始化人脸识别
  26. wx.initFaceDetect()
  27. // 2、创建 camera 上下文 CameraContext 对象
  28. this.cameraEngine = wx.createCameraContext()
  29. // 3、获取 Camera 实时帧数据
  30. const listener = this.cameraEngine.onCameraFrame((frame) => {
  31. if (this.tempImg) {
  32. return;
  33. }
  34. // 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
  35. wx.faceDetect({
  36. frameBuffer: frame.data,
  37. width: frame.width,
  38. height: frame.height,
  39. enablePoint: true,
  40. enableConf: true,
  41. enableAngle: true,
  42. enableMultiFace: true,
  43. success: (faceData) => {
  44. let face = faceData.faceInfo[0]
  45. if (faceData.x == -1 || faceData.y == -1) {
  46. this.tipsText = '检测不到人'
  47. }
  48. if (faceData.faceInfo.length > 1) {
  49. this.tipsText = '请保证只有一个人'
  50. } else {
  51. const {
  52. pitch,
  53. roll,
  54. yaw
  55. } = face.angleArray;
  56. const standard = 0.5
  57. if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard ||
  58. Math.abs(yaw) >= standard) {
  59. this.tipsText = '请平视摄像头'
  60. } else if (face.confArray.global <= 0.8 || face.confArray.leftEye <=
  61. 0.8 || face.confArray.mouth <= 0.8 || face.confArray.nose <= 0.8 ||
  62. face.confArray.rightEye <= 0.8) {
  63. this.tipsText = '请勿遮挡五官'
  64. } else {
  65. this.tipsText = '请拍照'
  66. // 这里可以写自己的逻辑了
  67. }
  68. }
  69. },
  70. fail: (err) => {
  71. if (err.x == -1 || err.y == -1) {
  72. this.tipsText = '检测不到人'
  73. } else {
  74. this.tipsText = err.errMsg || '网络错误,请退出页面重试'
  75. }
  76. },
  77. })
  78. })
  79. // 5、开始监听帧数据
  80. listener.start()
  81. // #endif
  82. },
  83. // 切换设备镜头
  84. handleChangeCameraClick() {
  85. this.devicePosition = !this.devicePosition;
  86. },
  87. // 图片上传
  88. handleChooseImage() {
  89. uni.chooseImage({
  90. count: 1,
  91. sizeType: ['original', 'compressed'],
  92. sourceType: ['album','camera'],
  93. success: (res) => {
  94. console.log(res,'打印res,哈哈哈哈哈')
  95. if (res.errMsg === 'chooseImage:ok') {
  96. uni.showLoading({
  97. title: '照片上传中...'
  98. })
  99. const tempFilePaths = res.tempFilePaths[0]
  100. this.upLoad(res.tempFilePaths);
  101. }
  102. },
  103. fail: (res) => {
  104. },
  105. });
  106. },
  107. // 拍照点击
  108. handleTakePhotoClick() {
  109. if (this.tipsText != "" && this.tipsText != "请拍照") {
  110. return;
  111. }
  112. uni.getSetting({
  113. success: (res) => {
  114. if (!res.authSetting['scope.camera']) {
  115. this.isAuthCamera = false
  116. uni.openSetting({
  117. success: (res) => {
  118. if (res.authSetting['scope.camera']) {
  119. this.isAuthCamera = true;
  120. }
  121. }
  122. })
  123. }
  124. }
  125. })
  126. this.cameraEngine.takePhoto({
  127. quality: "high",
  128. success: ({
  129. tempImagePath
  130. }) => {
  131. this.tempImg = tempImagePath
  132. }
  133. })
  134. },
  135. // 点击确定上传
  136. handleOkClick() {
  137. // 这里的 this.tempImg 是经过人脸检测后 拍照拿到的路径
  138. this.upLoadOne(this.tempImg)
  139. uni.navigateBack({
  140. delta: 1
  141. });
  142. },
  143. upLoad(tempFilePaths) {
  144. // # 注意 这里上传图片拿到的tempFilePaths是一个数组啊
  145. // 图片上传
  146. uni.showLoading({
  147. title: "上传中,请稍后...",
  148. });
  149. let proAll = [];
  150. tempFilePaths.forEach((item) => {
  151. console.log(item,'打印item')
  152. proAll.push(this.upLoadOne(item));
  153. });
  154. Promise.all(proAll).then((res) => {
  155. // 上传完成
  156. uni.hideLoading();
  157. });
  158. },
  159. upLoadOne(imgPath) {
  160. // 然后这里imgPath 传过来的是 要上传的临时本地图片的路径
  161. // 具体上传方法根据自己的请求方式 请求自己的接口
  162. },
  163. // 点击 - 取消上传
  164. handleCancelClick() {
  165. this.tempImg = ''
  166. },
  167. // 点击 - 人脸安全保障按钮
  168. handleJumpSecurityClick() {
  169. uni.showToast({
  170. icon: "none",
  171. title: "假装跳转人脸安全保障",
  172. duration: 2000,
  173. })
  174. },
  175. }
  176. }
  177. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/318026
推荐阅读
相关标签
  

闽ICP备14008679号