当前位置: 首页 > news >正文

微信小程序-人脸检测

微信小程序的人脸检测功能,配合蓝牙,配合ESP32 可以实现一些有趣的玩具
本文先只说微信小程序的人脸检测功能

1、人脸检测使用了摄像头,就必须在用户隐私权限里面声明。
修改用户隐私声明后,还需要等待审核,大概一天

2、app.json文件中也必须声明

 "permission":{"scope.bluetooth":{"desc": "获取蓝牙用于硬件链接"},"scope.camera":{"desc": "获取摄像头用于人脸检测"}},

这个不做,是无法真机调试的
模拟器上会一直报无效的
app.json permission[“scope.bluetooth”]、app.json permission[“scope.camera”]
这个错误,可以忽略

然后就是直接看代码了
face.xml

<block><view class="work-log" style="height: 300rpx;" ></view><view class="work-log" style="height: 200rpx;" > <view class="work-eyeview"><view class="work-eye" style="height: {{eyeLetfHeight}};width:{{eyeLetfWidth}}"></view></view><view class="work-eyeview"><view class="work-eye" style="height: {{eyeRightHeight}};width:{{eyeRightWidth}}"></view></view></view>
</block>

face.wxss

page{width: 100%;position: fixed;top:0;left:0;background-color: black;
}
.work-log {width: 96%;flex-direction:row;display: flex;margin-top: 15rpx;margin-bottom: 5rpx;margin-left: 2%;
}
.work-eyeview {width: 50%;height: 100%;background-color: black;display: flex;justify-content: center;align-items: center;
}
.work-eye {width: 30%;height: 100%;box-shadow: 4px 4px 15px #c0bfbf;background-color: #eef1f7;border-radius: 10rpx;
}

face.json

{"disableScroll": true,"renderer": "webview","navigationBarTitleText": "人脸三维识别","pageOrientation": "auto","navigationStyle":"custom"
}

face.js


Component({session: undefined, // 全局的VKsession对象data:{originx:"1%",originy:"1%",eyeLetfHeight:"100%",eyeLetfWidth:"30%",eyeRightHeight:"100%",eyeRightWidth:"30%"},methods: {onReady(){//初始化VKthis.init();},onHide :function(){//关闭this.closeBle();},onUnload :function(){this.closeBle();},// 对应案例的初始化逻辑,由统一的 behavior 触发 初始化VKstart完毕后,进行更新渲染循环init() {this// VKSession 配置const session = this.session = wx.createVKSession({track: {face: {mode: 1}},version: 'v2',});try {session.start(err => {if (err) return console.error('VK error: ', err);//摄像头设置为前置摄像头 1    0 为后置const config = session.configconfig.cameraPosition = 1;  session.config = config;console.log('VKSession.start ok,version:', session.version)//  VKSession EVENT resizesession.on('resize', () => {})// 开启三维识别session.update3DMode({open3d: true})// VKSession EVENT addAnchorssession.on('addAnchors', anchors => {console.log("addAnchor", anchors)})// VKSession EVENT updateAnchorssession.on('updateAnchors', anchors => {var anchor = anchors[0];//第一个人脸//43 两个眼睛中间点     46鼻头var  centeracch = anchor.points[46];//两个眼睛中间点//72 左上眼皮  73  左下眼皮   75 右上眼皮  76 右下眼皮//console.log(centeracch);//鼻头var eyeLetfLen  = this.calen(this.calculateEye(anchor.points[72],anchor.points[73],anchor.points[52],anchor.points[55]));var eyeRightLen = this.calen(this.calculateEye(anchor.points[75],anchor.points[76],anchor.points[58],anchor.points[61]));this.setData({originx:centeracch.x * 100 +"%",originy:centeracch.y * 100 +"%",eyeLetfHeight: eyeLetfLen + "%",eyeLetfWidth: (70 - (eyeLetfLen / 100 ) * 40) + "%",eyeRightHeight: eyeRightLen + "%",eyeRightWidth: (70 - (eyeRightLen / 100 ) * 40) + "%"})})// VKSession removeAnchors// 识别目标丢失时不断触发session.on('removeAnchors', anchors => {console.log("removeAnchors",anchors);this.setData({originx:"1%",originy:"1%"})});console.log('ready to initloop')// start 初始化完毕后,进行更新渲染循环this.initLoop();});} catch(e) {console.error(e);}},calen(eyelen){var l =  105 - eyelen;if(l>100){return 100;}else if (l < 5){return 3;}else{return l;}},calculateEye(up,dow,left,right){var ylen = this.calculateDistance(up.x,up.y,dow.x,dow.y);var xlen = this.calculateDistance(right.x,right.y,left.x,left.y);return xlen/ylen;},calculateDistance(x1, y1, x2, y2) {  var dx = x2 - x1;  var dy = y2 - y1;  return Math.sqrt(dx * dx + dy * dy);  },// 限帧逻辑initLoop() {// 限制调用帧率,暂时去掉let fps = 30let fpsInterval = 1000 / fpslet last = Date.now()const session = this.session;// 逐帧渲染const onFrame = timestamp => {try {let now = Date.now()const mill = now - last// 经过了足够的时间if (mill > fpsInterval) {last = now - (mill % fpsInterval); //校正当前时间session.getVKFrame(1,1);}} catch(e) {console.error(e);}session.requestAnimationFrame(onFrame)}session.requestAnimationFrame(onFrame)},},
})

最终实现的效果就是 屏幕对应的眼睛和自己眼睛对应,眨眼检测

http://www.lryc.cn/news/306414.html

相关文章:

  • 微信小程序自制动态导航栏
  • 金融知识分享系列之:五日线
  • 回归测试详解
  • 渲染效果图有哪几种分类?效果图为什么用云渲染更快
  • Docker镜像加速
  • 吴恩达deeplearning.ai:sigmoid函数的替代方案以及激活函数的选择
  • Alias许可分析中的数据可视化
  • 【计算机网络】数据链路层--以太网/MTU/ARP/RARP协议
  • typescript使用解构传参
  • CSP-J 2023 复赛第4题:旅游巴士
  • JAVA算法和数据结构
  • 每日五道java面试题之spring篇(七)
  • Keil编译GD32工程时找不到lib库文件
  • 测试C#使用ViewFaceCore实现图片中的人脸遮挡
  • 2.21 Qt day2 菜单栏/工具栏/状态栏/浮动窗口、UI界面、信号与槽
  • 300分钟吃透分布式缓存-16讲:常用的缓存组件Redis是如何运行的?
  • 上一篇文章补充:已经存在的小文件合并
  • 代码随想录训练营第三十期|第四十三天|动态规划 part05|1049. 最后一块石头的重量 II ● 494. 目标和 ● 474.一和零
  • c++学习记录 string容器—字符串插入和删除
  • 【IEEE会议征稿】2024年第九届智能计算与信号处理国际学术会议(ICSP 2024)
  • 【电机仿真】HFI算法脉振高频电压信号注入观测器-PMSM无感FOC控制
  • Java学习——集合框架
  • 【鸿蒙 HarmonyOS 4.0】UIAbility、页面及组件的生命周期
  • jdk动态代理与CGLib动态代理
  • Linux 命令行的世界 :4.操作文件和目录
  • 如何使用ArcGIS Pro生成等高线
  • golang学习2,golang开发配置国内镜像
  • Stable Diffusion 绘画入门教程(webui)-ControlNet(线稿约束)
  • 前端笔记——var let const 之间的区别
  • AI工具新革命:从ChatGPT到Sora,生成式AI改变世界