当前位置:   article > 正文

uni小程序-人脸采集、人脸检测功能_uniapp小程序人脸识别

uniapp小程序人脸识别

适用于人脸采集,人脸检测等功能。

逻辑就是先调起摄像头,获取到实时帧数据之后,使用wx.faceDetect进行人脸检测,检测到正面的人脸就可以拍照上传给后端了

直接上代码

  1. <template>
  2. <view class="content">
  3. <view class="camera-box">
  4. <view class="camera-bg-box"><camera class="camera" device-position="front" flash="off" resolution="low"></camera></view>
  5. <view v-show="tipsText" class="camera-tip">{{ tipsText }}</view>
  6. </view>
  7. </view>
  8. </template>
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. tipsText: '',
  6. };
  7. },
  8. onShow() {
  9. let that = this
  10. // 初始化相机引擎
  11. that.initData();
  12. },
  13. methods: {
  14. // 初始化相机引擎
  15. initData() {
  16. let that = this;
  17. // #ifdef MP-WEIXIN
  18. // 1、初始化人脸识别
  19. wx.initFaceDetect();
  20. // 2、创建 camera 上下文 CameraContext 对象
  21. that.cameraEngine = wx.createCameraContext();
  22. // 3、获取 Camera 实时帧数据
  23. const listener = that.cameraEngine.onCameraFrame(frame => {
  24. // 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
  25. wx.faceDetect({
  26. frameBuffer: frame.data,
  27. width: frame.width,
  28. height: frame.height,
  29. enablePoint: true,
  30. enableConf: true,
  31. enableAngle: true,
  32. enableMultiFace: true,
  33. success: faceData => {
  34. let face = faceData.faceInfo[0];
  35. //人脸位置校验
  36. var centerWidth = 150;
  37. var centerHeight = 150;
  38. if (faceData.x == -1 || faceData.y == -1) {
  39. that.tipsText = '检测不到人';
  40. }
  41. if (faceData.faceInfo.length > 1) {
  42. that.tipsText = '请保证只有一个人';
  43. } else {
  44. const { pitch, roll, yaw } = face.angleArray;
  45. const standard = 0.3;
  46. if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard || Math.abs(yaw) >= standard) {
  47. that.tipsText = '请平视摄像头';
  48. } else if (
  49. face.x < (frame.width - centerWidth) / 2 ||
  50. face.x > (frame.width - centerWidth) / 2 + centerWidth ||
  51. face.y < (frame.height - centerHeight) / 2 ||
  52. face.y > (frame.height - centerHeight) / 2 + centerHeight
  53. ) {
  54. this.tipsText = '请将人脸对准中心位置';
  55. } else if (
  56. face.confArray.global <= 0.8 ||
  57. face.confArray.leftEye <= 0.8 ||
  58. face.confArray.mouth <= 0.8 ||
  59. face.confArray.nose <= 0.8 ||
  60. face.confArray.rightEye <= 0.8
  61. ) {
  62. that.tipsText = '请勿遮挡五官';
  63. } else {
  64. listener.stop();
  65. that.tipsText = '即将拍照,请保持!';
  66. setTimeout(function() {
  67. that.handleTakePhotoClick();
  68. }, 3000);
  69. return;
  70. let time = 3;
  71. that.tipsText = time + '秒后拍照,请保持!';
  72. let timer3 = setInterval(function() {
  73. time--;
  74. if (time <= 0) {
  75. clearInterval(timer3);
  76. // 拍照
  77. return that.handleTakePhotoClick();
  78. } else {
  79. that.tipsText = time + '秒后拍照,请保持!';
  80. }
  81. }, 1000);
  82. }
  83. }
  84. },
  85. fail: err => {
  86. if (err.x == -1 || err.y == -1) {
  87. that.tipsText = '检测不到人';
  88. } else {
  89. that.tipsText = err.errMsg || '网络错误,请退出页面重试';
  90. }
  91. }
  92. });
  93. });
  94. // 5、开始监听帧数据
  95. listener.start();
  96. },
  97. // 拍照
  98. handleTakePhotoClick() {
  99. this.tipsText = '正在上传...';
  100. setTimeout(()=>{
  101. this.tipsText = '上传成功';
  102. },2000)
  103. // 检测是否授权相机
  104. uni.getSetting({
  105. success: res => {
  106. if (!res.authSetting['scope.camera']) {
  107. this.isAuthCamera = false;
  108. uni.openSetting({
  109. success: res => {
  110. if (res.authSetting['scope.camera']) {
  111. this.isAuthCamera = true;
  112. }
  113. }
  114. });
  115. }
  116. }
  117. });
  118. this.cameraEngine.takePhoto({
  119. quality: 'low',
  120. success: ({ tempImagePath }) => {
  121. let mybase64 = wx.getFileSystemManager().readFileSync(tempImagePath, 'base64');
  122. //拼接成base64格式
  123. let data = 'data:image/jpg' + ';base64,' + mybase64;
  124. console.log("上传",data);
  125. // 调用后端接口进行人脸识别,使用base64图片格式
  126. // 后端人脸识别可以使用阿里云,百度智能云这些
  127. }
  128. });
  129. }
  130. }
  131. };
  132. </script>
  1. <style lang="scss" scoped>
  2. .content {
  3. position: absolute;
  4. top: 0;
  5. right: 0;
  6. bottom: 0;
  7. left: 0;
  8. .camera-box {
  9. position: relative;
  10. width: 100%;
  11. height: 100%;
  12. }
  13. .camera-bg-box{
  14. position: relative;
  15. width: 100%;
  16. height: 100%;
  17. overflow: hidden;
  18. &::after {
  19. content: '';
  20. position: absolute;
  21. left: 50%;
  22. top: 50%;
  23. transform: translateX(-50%) translateY(-50%);
  24. border-radius: 100%;
  25. width: 600rpx;
  26. height: 600rpx;
  27. // border: 1000rpx solid white;
  28. border: 1000rpx solid rgba(0, 0, 0, 0.5);
  29. }
  30. }
  31. .camera {
  32. width: 100%;
  33. height: 100%;
  34. border-top: 200rpx solid black;
  35. border-bottom: 200rpx solid black;
  36. box-sizing: border-box;
  37. }
  38. .camera-tip {
  39. position: absolute;
  40. bottom: 220rpx;
  41. left: 50%;
  42. transform: translateX(-50%);
  43. color: white;
  44. }
  45. }
  46. </style>

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

闽ICP备14008679号