-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
177 lines (169 loc) · 5.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const videoElement = document.getElementsByClassName('input_video')[0];
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const canvasCtx = canvasElement.getContext('2d');
const LEFT_MARGIN = 180;
const DRUM_WIDTH = 110;
const GAP = 100;
const CANVAS_WIDTH = 1100;
const CANVAS_HEIGHT = 700;
const KEY_SOUND = {
firstBlock: '1',
secondBlock: '2',
thirdBlock: '3',
forthBlock: '4',
};
// throttle util function
let throttleTimer;
const throttle = (callback, time) => {
if (throttleTimer) return;
throttleTimer = true;
setTimeout(() => {
callback();
throttleTimer = false;
}, time);
};
function onResults(results) {
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(
results.image,
0,
0,
canvasElement.width,
canvasElement.height
);
if (results.multiHandLandmarks) {
for (
let index = 0;
index < results.multiHandLandmarks.length;
index++
) {
const classification = results.multiHandedness[index];
const isRightHand = !(classification?.label === 'Right');
let leftHand = [];
let rightHand = [];
if (isRightHand) {
rightHand = results.multiHandLandmarks[index];
} else {
leftHand = results.multiHandLandmarks[index];
}
if (true) {
if (!isRightHand && leftHand.length > 6) {
const indexFinger = leftHand[7];
const condirateX = indexFinger.x * 1100;
const condirateY = indexFinger.y * 700;
if (
condirateY > 400 &&
condirateX <= 1100 - LEFT_MARGIN &&
condirateX >= 1100 - LEFT_MARGIN - DRUM_WIDTH
) {
throttle(() => {
const keyCode = KEY_SOUND['forthBlock'];
const audio = document.querySelector(
`audio[data-key="${keyCode}"]`
);
if (!audio) return;
audio.currentTime = 0;
audio.play();
}, 500);
}
}
if (!isRightHand && leftHand.length > 6) {
const indexFinger = leftHand[7];
const condirateX = indexFinger.x * 1100;
const condirateY = indexFinger.y * 700;
if (
condirateY > 400 &&
condirateX <= 1100 - LEFT_MARGIN - DRUM_WIDTH - GAP &&
condirateX >= 1100 - LEFT_MARGIN - DRUM_WIDTH * 2 - GAP
) {
throttle(() => {
const keyCode = KEY_SOUND['secondBlock'];
const audio = document.querySelector(
`audio[data-key="${keyCode}"]`
);
if (!audio) return;
audio.currentTime = 0;
audio.play();
}, 300);
}
}
if (isRightHand && rightHand.length > 6) {
const indexFinger = rightHand[7];
const condirateX = indexFinger.x * 1100;
const condirateY = indexFinger.y * 700;
if (
condirateY > 400 &&
condirateX <= 1100 - LEFT_MARGIN - DRUM_WIDTH * 2 - GAP * 2 &&
condirateX >= 1100 - LEFT_MARGIN - DRUM_WIDTH * 3 - GAP * 2
) {
throttle(() => {
const keyCode = KEY_SOUND['thirdBlock'];
const audio = document.querySelector(
`audio[data-key="${keyCode}"]`
);
if (!audio) return;
audio.currentTime = 0;
audio.play();
}, 500);
}
}
if (isRightHand && rightHand.length > 6) {
const indexFinger = rightHand[7];
const condirateX = indexFinger.x * 1100;
const condirateY = indexFinger.y * 700;
if (
condirateY > 400 &&
condirateX <= 1100 - LEFT_MARGIN - DRUM_WIDTH * 3 - GAP * 3 &&
condirateX >= 1100 - LEFT_MARGIN - DRUM_WIDTH * 4 - GAP * 3
) {
throttle(() => {
const keyCode = KEY_SOUND['firstBlock'];
const audio = document.querySelector(
`audio[data-key="${keyCode}"]`
);
if (!audio) return;
audio.currentTime = 0;
audio.play();
}, 500);
}
}
}
for (const landmarks of results.multiHandLandmarks) {
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {
color: '#00FF00',
lineWidth: 1,
});
drawLandmarks(canvasCtx, landmarks, {
color: '#FF0000',
lineWidth: 1,
fillColor: 'blue',
radius: () => {
return lerp(0, -0.15, 0.1, 10, 1);
},
});
}
}
}
canvasCtx.restore();
}
const hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
},
});
hands.setOptions({
maxNumHands: 2,
modelComplexity: 1,
minDetectionConfidence: 0.4,
minTrackingConfidence: 0.4,
});
hands.onResults(onResults);
const camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({ image: videoElement });
},
width: 1100,
height: 700,
});
camera.start();