前面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();
//初始化VK
this.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.config
config.cameraPosition = 1;
session.config = config;
console.log('VKSession.start ok,version:', session.version)
// VKSession EVENT resize
session.on('resize', () => {
})
// 开启三维识别
session.update3DMode({open3d: true})
// VKSession EVENT addAnchors
session.on('addAnchors', anchors => {
console.log("addAnchor", anchors)
})
// VKSession EVENT updateAnchors
session.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 = 30
let fpsInterval = 1000 / fps
let 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 2
uint8_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);
else
digitalWrite(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); // 设置要发送的值为1
pTxCharacteristic->notify(); // 广播
txValue++; // 指针数值自加1
delay(2000); // 如果有太多包要发送,蓝牙会堵塞
}
// disconnecting 断开连接
if (!deviceConnected && oldDeviceConnected)
{
delay(500); // 留时间给蓝牙缓冲
pServer->startAdvertising(); // 重新广播
Serial.println(" 开始广播 ");
oldDeviceConnected = deviceConnected;
}
// connecting 正在连接
if (deviceConnected && !oldDeviceConnected)
{
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}