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

微信小程序-人脸检测-眨眼驱动ESP32蓝牙设备灯

前面2篇文章已经写了具体的人脸检测和蓝牙
这里直接结合,只列js 代码,剩下的其他代码在另外文章里面
https://blog.csdn.net/walle167/article/details/136261993
https://blog.csdn.net/walle167/article/details/136261919
上代码


import bleBehavior from './ble'Component({behaviors: [bleBehavior],session: undefined, // 全局的VKsession对象data:{originx:"1%",originy:"1%",eyeLetfHeight:"100%",eyeLetfWidth:"30%",eyeRightHeight:"100%",eyeRightWidth:"30%"},methods: {onReady(){//初始化蓝牙this.openBluetoothAdapter();//初始化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]));if(eyeLetfLen < 5){console.log("open led");//发送蓝牙数据this.writeBLECharacteristicValue(0x61);}var eyeRightLen = this.calen(this.calculateEye(anchor.points[75],anchor.points[76],anchor.points[58],anchor.points[61]));if(eyeRightLen < 5){console.log("close led");//发送蓝牙数据this.writeBLECharacteristicValue(0x62);}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)},},
})

esp32的代码是抄了其他博主的
https://blog.csdn.net/m0_45199510/article/details/131642604

代码如下

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>#define Led 2uint8_t txValue = 0;                         //后面需要发送的值
BLEServer *pServer = NULL;                   //BLEServer指针 pServer
BLECharacteristic *pTxCharacteristic = NULL; //BLECharacteristic指针 pTxCharacteristic
bool deviceConnected = false;                //本次连接状态
bool oldDeviceConnected = false;             //上次连接状态d
// See the following for generating UUIDs: https://www.uuidgenerator.net/
#define SERVICE_UUID "2563121a-5e23-42e7-a40a-8e1c4e522e96" // UART service UUID
#define CHARACTERISTIC_UUID_RX "2563121a-5e23-42e7-a40a-8e1c4e522e96"
#define CHARACTERISTIC_UUID_TX "2563121a-5e23-42e7-a40a-8e1c4e522e96"//回调程序
class MyServerCallbacks : public BLEServerCallbacks
{void onConnect(BLEServer *pServer){deviceConnected = true;};void onDisconnect(BLEServer *pServer){deviceConnected = false;}
};class MyCallbacks : public BLECharacteristicCallbacks
{void onWrite(BLECharacteristic *pCharacteristic){std::string rxValue = pCharacteristic->getValue(); //接收信息if (rxValue.length() > 0){ //向串口输出收到的值Serial.print("RX: ");for (int i = 0; i < rxValue.length(); i++)Serial.print(rxValue[i]);Serial.println();if (rxValue[0]=='a')digitalWrite(Led, HIGH);elsedigitalWrite(Led, LOW);              }}
};void setup()
{Serial.begin(115200);// 创建一个 BLE 设备BLEDevice::init("ESP32BLE");//在这里面是ble的名称// 创建一个 BLE 服务pServer = BLEDevice::createServer();pServer->setCallbacks(new MyServerCallbacks()); //设置回调BLEService *pService = pServer->createService(SERVICE_UUID);// 创建一个 BLE 特征pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);pTxCharacteristic->addDescriptor(new BLE2902());BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);pRxCharacteristic->setCallbacks(new MyCallbacks()); //设置回调pService->start();                  // 开始服务pServer->getAdvertising()->start(); // 开始广播Serial.println(" 等待一个客户端连接,且发送通知... ");pinMode(Led, OUTPUT);
}void loop()
{// deviceConnected 已连接if (deviceConnected){pTxCharacteristic->setValue(&txValue, 1); // 设置要发送的值为1pTxCharacteristic->notify();              // 广播txValue++;                                // 指针数值自加1delay(2000);                              // 如果有太多包要发送,蓝牙会堵塞}// disconnecting  断开连接if (!deviceConnected && oldDeviceConnected){delay(500);                  // 留时间给蓝牙缓冲pServer->startAdvertising(); // 重新广播Serial.println(" 开始广播 ");oldDeviceConnected = deviceConnected;}// connecting  正在连接if (deviceConnected && !oldDeviceConnected){// do stuff here on connectingoldDeviceConnected = deviceConnected;}
}
http://www.lryc.cn/news/305437.html

相关文章:

  • 怎么在wifi中实现手机和电脑文件互传
  • 07 STL 简介
  • unity学习(39)——创建(create)角色脚本(panel)——静态(static)
  • MacOS环境下用powerline配置Terminal终端
  • liunx单机项目部署
  • SQL 中如何实现多表关联查询?
  • oracle 设置权限 禁止删除用户
  • 港科夜闻|香港科大计划建立北部都会区卫星校园完善科大创新带,发展未来创新科技 未来医药发展及跨学科教育...
  • linux反弹shell简单使用
  • 前后端分离Vue+nodejs校园论坛bbs系统x450z
  • ChatGPT的能力边界在哪?
  • Sentinel微服务流量治理组件实战下
  • vue+node.js美食分享推荐管理系统 io551
  • 云原生超融合八大核心能力|ZStack Edge云原生超融合产品技术解读
  • 认识K8S
  • K8S-001-Virtual box - Network Config
  • ShardingSphere5.x 分库分表
  • cmake 项目。qt5升级 qt6 报错 error: “Qt requires a C++17 compiler 已解决
  • Flutter Engine 编译
  • 数据可视化在商业领域有哪些重要性?
  • UI风格汇:扁平化风格来龙去脉,特征与未来趋势
  • 【雷达指标】MTI/MTD性能
  • 暴雨服务器:科技创新构建高效、高质、可持续的新质生产力
  • 在 where子句中使用子查询(一)
  • kafka为什么性能这么高?
  • 安卓OpenGL添加水印并录制(二)---抖音录制原理
  • 动态绑定样式,uniapp,用三元运算动态绑定多个class类样式,动态绑定的样式可以和原始样式共存
  • 神经网络基础——激活函数的选择、参数初始化
  • ElasticSearch之聚合aggs
  • Android 系统定位和高德定位