xxxxxxxxxx
141
const FINGER_CLOSED_THRESHHOLD = 30;
const DOT_MIN_THRESHHOLD = 100;
const DOT_MAX_THRESHHOLD = 300;
const RECOGNITION_THRESHHOLD = 800;
const CLEAR_THRESHHOLD = 4500;
class ObjectManager {
constructor() {
this.morse = '';
this.displayText = '';
this.detected = false; // is recognized by cv?
this.pos = []; // current position (smoothed)
this.latestPos = []; // detected position
this.on = false;
this.wasOn = false;
this.onSince = Date.now();
this.offSince = Date.now();
}
drawObjects() {
text(this.displayText, width/2, height/2);
text(this.morse, width/2, height*3/4);
}
resultHandler(results) {
let isUpdated = false;
results?.forEach(result => {
this.update(result.keypoints);
isUpdated = true;
});
// if nothing is detected, turn down audio
if (isUpdated == false) {
osc.amp(0);
}
}
update(position) {
const detected = !!position && position.length > 0;
this.detected = detected;
// this.setOnOffTime(detected);
if (detected) {
this.latestPos = position;
this.detectTrigger();
}
this.pos = easeInPositions(this.pos, this.latestPos);
translate(VIDEO_WIDTH, 0);
scale(-1.0, 1.0);
this.drawSkeleton(this.pos);
}
processCode() {
const diffOn = Date.now() - this.onSince;
const diffOff = Date.now() - this.offSince;
if (this.on) {
osc.amp(0.5);
} else {
if (this.wasOn) {
this.morse += diffOn > DOT_MAX_THRESHHOLD ? '-' : '.';
}
if (diffOff > CLEAR_THRESHHOLD) {
this.displayText = '';
}
if (diffOff > RECOGNITION_THRESHHOLD) {
if (MORSECODE[this.morse]) {
this.displayText += MORSECODE[this.morse];
}
this.morse = '';
}
osc.amp(0);
}
}
detectTrigger() {
if (this.pos.length == 0) {return;}
const p = this.pos;
const pL = this.latestPos;
const d1 = dist(p[4].x, p[4].y, p[8].x, p[8].y); // 4,8
const c1 = d1 < FINGER_CLOSED_THRESHHOLD;
this.wasOn = this.on;
if (c1) {
if (!this.on) {
this.onSince = Date.now();
}
this.on = true;
// background(220)
} else {
if (this.on) {
this.offSince = Date.now();
}
this.on = false;
// background(0)
}
}
drawSkeleton(pos) {
if (!pos) {
return;
}
push();
stroke(20);
strokeWeight(25);
noFill();
// Loop through all the skeletons detected
const a = pos;
line(a[4].x, a[4].y, a[3].x, a[3].y);
line(a[3].x, a[3].y, a[2].x, a[2].y);
line(a[8].x, a[8].y, a[7].x, a[7].y);
line(a[7].x, a[7].y, a[6].x, a[6].y);
line(a[6].x, a[6].y, a[5].x, a[5].y);
line(a[12].x, a[12].y, a[11].x, a[11].y);
line(a[11].x, a[11].y, a[10].x, a[10].y);
line(a[10].x, a[10].y, a[9].x, a[9].y);
line(a[16].x, a[16].y, a[15].x, a[15].y);
line(a[15].x, a[15].y, a[14].x, a[14].y);
line(a[14].x, a[14].y, a[13].x, a[13].y);
line(a[20].x, a[20].y, a[19].x, a[19].y);
line(a[19].x, a[19].y, a[18].x, a[18].y);
line(a[17].x, a[17].y, a[18].x, a[18].y);
line(a[2].x, a[2].y, a[5].x, a[5].y);
line(a[5].x, a[5].y, a[9].x, a[9].y);
line(a[9].x, a[9].y, a[13].x, a[13].y);
line(a[13].x, a[13].y, a[17].x, a[17].y);
line(a[17].x, a[17].y, a[0].x, a[0].y);
line(a[0].x, a[0].y, a[1].x, a[1].y);
line(a[1].x, a[1].y, a[2].x, a[2].y);
pop();
}
}