-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
517 lines (462 loc) · 18.7 KB
/
sketch.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
var g_landmarks = [];
var g_angles = [];
var is_enable_mouse = false;
var is_pinched = false;
var dollar;
var trail_mode = {
kinds: [{ name: 'None' }, { name: 'All Fingers' }, { name: 'Index Finger' }],
name: 'None',
current: 0,
toggle: function () {
this.current++;
if (this.current > this.kinds.length - 1) {
this.current = 0;
}
this.name = this.kinds[this.current].name;
console.log(this.name);
}
}
var g_browser_mode;
var is_dollar_active = false;
var is_pose_active = false;
var time_stamp_pose_active;
var time_stamp_pose_deactive;
var ms_operation_activate = 1000;
var threshold = -0.08;
var poses = [
{
class: 'scissors',
angles: [151.0232640949309, 145.089203018993, 144.16041218595345, 169.68865560150968, 177.15161107874442, 175.53394799584137, 173.98758495731641, 179.36823806450363, 174.9455227261207, 164.41700628951898, 26.185195501561566, 169.24322576395195, 155.01530489937826, 38.58580456701814, 168.6758256467776],
active: false,
timestamp: 0
},
{
class: '5_fingers_pinch',
angles: [154.3371648438161, 174.62492934438916, 171.23768032070492, 166.31589751344552, 141.49517480457678, 162.75608187430615, 164.91395822446106, 125.91358194207635, 138.95927474274404, 162.89030937597866, 116.01607436927182, 140.65775245114062, 139.3503002981901, 151.04051353558503, 156.65701995872436],
active: false,
timestamp: 0
}
]
var trails_left;
var trails_right;
var previous_z = 0;
// ここからMediaPipeの記述
function onResults(results) {
if (results.multiHandLandmarks) {
g_landmarks = [];
g_angles = [];
let num = 0;
for (const landmarks of results.multiHandLandmarks) {
landmarks.handedness = results.multiHandedness[num];
g_landmarks.push(landmarks);
num++;
}
//console.log(g_landmarks);
//console.log(results.multiHandedness);
//console.log(results.multiHandness[0].Left);
// ハンドジェスチャ認識処理はこのタイミングがよいと思う
for (landmarks of g_landmarks) {
let tips = [4, 8, 12, 16, 20];
//console.log(landmarks.handedness);
if (landmarks.handedness.label == 'Left' && landmarks.handedness.score > 0.85) {
trails_right.addVertex([
{ x: width * (1.0 - landmarks[4].x), y: height * (landmarks[4].y) },
{ x: width * (1.0 - landmarks[8].x), y: height * (landmarks[8].y) },
{ x: width * (1.0 - landmarks[12].x), y: height * (landmarks[12].y) },
{ x: width * (1.0 - landmarks[16].x), y: height * (landmarks[16].y) },
{ x: width * (1.0 - landmarks[20].x), y: height * (landmarks[20].y) }
]);
if (is_enable_mouse) {
if (landmarks[8].z < threshold && previous_z >= threshold) {
(async () => {
const message = await window.myapi.leftClick({
x: width * (1.0 - landmarks[8].x),
y: height * (landmarks[8].y)
})
})()
}
}
previous_z = landmarks[8].z;
}
else if (landmarks.handedness.label == 'Right' && landmarks.handedness.score > 0.85) {
trails_left.addVertex([
{ x: width * (1.0 - landmarks[4].x), y: height * (landmarks[4].y) },
{ x: width * (1.0 - landmarks[8].x), y: height * (landmarks[8].y) },
{ x: width * (1.0 - landmarks[12].x), y: height * (landmarks[12].y) },
{ x: width * (1.0 - landmarks[16].x), y: height * (landmarks[16].y) },
{ x: width * (1.0 - landmarks[20].x), y: height * (landmarks[20].y) }
]);
}
let pos_fingers = [
[0, 1, 2, 3, 4],
[0, 5, 6, 7, 8],
[0, 9, 10, 11, 12],
[0, 13, 14, 15, 16],
[0, 17, 18, 19, 20]
];
// 各関節角度を計算して g_angles に保存
let index = 0;
for (pos_finger of pos_fingers) {
let v1 = createVector(
landmarks[pos_finger[0]].x - landmarks[pos_finger[1]].x,
landmarks[pos_finger[0]].y - landmarks[pos_finger[1]].y,
landmarks[pos_finger[0]].z - landmarks[pos_finger[1]].z);
let v2 = createVector(
landmarks[pos_finger[2]].x - landmarks[pos_finger[1]].x,
landmarks[pos_finger[2]].y - landmarks[pos_finger[1]].y,
landmarks[pos_finger[2]].z - landmarks[pos_finger[1]].z);
let v3 = createVector(
landmarks[pos_finger[1]].x - landmarks[pos_finger[2]].x,
landmarks[pos_finger[1]].y - landmarks[pos_finger[2]].y,
landmarks[pos_finger[1]].z - landmarks[pos_finger[2]].z);
let v4 = createVector(
landmarks[pos_finger[3]].x - landmarks[pos_finger[2]].x,
landmarks[pos_finger[3]].y - landmarks[pos_finger[2]].y,
landmarks[pos_finger[3]].z - landmarks[pos_finger[2]].z);
let v5 = createVector(
landmarks[pos_finger[2]].x - landmarks[pos_finger[3]].x,
landmarks[pos_finger[2]].y - landmarks[pos_finger[3]].y,
landmarks[pos_finger[2]].z - landmarks[pos_finger[3]].z);
let v6 = createVector(
landmarks[pos_finger[4]].x - landmarks[pos_finger[3]].x,
landmarks[pos_finger[4]].y - landmarks[pos_finger[3]].y,
landmarks[pos_finger[4]].z - landmarks[pos_finger[3]].z);
g_angles.push(abs(degrees(v1.angleBetween(v2))));
g_angles.push(abs(degrees(v3.angleBetween(v4))));
g_angles.push(abs(degrees(v5.angleBetween(v6))));
}
// pose_gesturing との比較
if (g_angles.length == 15) {
for (let pose of poses) {
let d_sum = 0;
for (let pos = 0; pos < g_angles.length; pos++) {
d_sum += pow(g_angles[pos] - pose.angles[pos], 2);
}
d_sum = sqrt(d_sum);
//console.log(d_sum);
if (pose.class == 'scissors') {
if (d_sum < 100) {
if (pose.active == false) {
pose.active = true;
pose.timestamp = millis();
}
}
else {
pose.active = false;
pose.timestamp = millis();
}
}
else if (pose.class == '5_fingers_pinch') {
//console.log(d_sum);
if (d_sum < 50) {
if (pose.active == false) {
pose.active = true;
pose.timestamp = millis();
}
}
else {
pose.active = false;
pose.timestamp = millis();
}
}
}
}
let distance_4to8 = dist(
landmarks[4].x, landmarks[4].y, landmarks[4].z,
landmarks[8].x, landmarks[8].y, landmarks[8].z);
if (distance_4to8 < 0.05) {
if (is_pinched == false) {
if (g_browser_mode == false) {
(async () => {
const message = await window.myapi.down({
x: width * (1.0 - (landmarks[4].x + (landmarks[8].x - landmarks[4].x) / 2)),
y: height * (landmarks[4].y + (landmarks[8].y - landmarks[4].y) / 2)
})
})()
}
}
else {
if (g_browser_mode == false) {
(async () => {
const message = await window.myapi.drag({
x: width * (1.0 - (landmarks[4].x + (landmarks[8].x - landmarks[4].x) / 2)),
y: height * (landmarks[4].y + (landmarks[8].y - landmarks[4].y) / 2)
})
})()
}
}
is_pinched = true;
}
else {
if (is_pinched == true) {
if (g_browser_mode == false) {
(async () => {
const message = await window.myapi.up({
x: width * (1.0 - (landmarks[4].x + (landmarks[8].x - landmarks[4].x) / 2)),
y: height * (landmarks[4].y + (landmarks[8].y - landmarks[4].y) / 2)
})
})()
}
}
is_pinched = false;
}
}
// draw();
}
if (isLooping() == false) {
loop();
}
}
// ここまでMediaPipeの記述
var camera;
var video;
var cam;
function startCamera() {
startMediaPipeHands('8ce8e876fe1196ef2fdff50084325a364d50628dc4f98bd938e163efed58a6cf');
}
function setup() {
// put setup code here
var mycanvas = createCanvas(windowWidth, windowHeight);
drawingContext.shadowBlur = 7;
drawingContext.shadowOffsetX = 5;
drawingContext.shadowOffsetY = 5;
drawingContext.shadowColor = color(150, 150, 150);
console.log(mycanvas);
document.querySelector('#p5Canvas').appendChild(mycanvas.elt);
//mycanvas.parent('#p5canvas');
// video = createCapture(VIDEO);
// video.size(1280, 720);
// video.hide();
//noLoop();
trails_left = new Trails(5);
trails_right = new Trails(5);
// electron利用時は startCamera はコメントアウトする
if (localStorage.getItem('electron') == null) {
//alert('debug mode')
g_browser_mode = true;
startCamera();
}
else {
g_browser_mode = false;
}
console.log('hello');
dollar = DollarRecognizer();
}
function draw() {
clear();
//background(127);
//image(video, 0, 0);
if (g_landmarks.length > 0) {
for (landmarks of g_landmarks) {
// 押し込んでマウスクリック操作
if (is_enable_mouse) {
if (landmarks[8].z / threshold > 0.4) {
noStroke();
fill(255, 0, 0, 150);
circle(
width * (1.0 - landmarks[8].x),
height * landmarks[8].y,
(width / 20) * landmarks[8].z / threshold,
(width / 20) * landmarks[8].z / threshold
);
strokeWeight(2);
noFill();
stroke(255, 0, 0);
circle(
width * (1.0 - landmarks[8].x),
height * landmarks[8].y,
(width / 20),
(width / 20)
);
}
}
let array_points = [
[0, 1, 2, 5, 9, 13, 17, 0],
[2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]
];
noFill();
stroke(50, 200);
for (array_point of array_points) {
if (array_point.length > 0) {
strokeWeight(width / 100);
beginShape();
curveVertex(
width * (1.0 - landmarks[array_point[0]].x),
height * (landmarks[array_point[0]].y)
);
for (point of array_point) {
vertex(
width * (1.0 - landmarks[point].x),
height * landmarks[point].y
);
}
curveVertex(
width * (1.0 - landmarks[array_point[array_point.length - 1]].x),
height * (landmarks[array_point[array_point.length - 1]].y)
);
endShape();
}
}
if (is_enable_mouse) {
if (is_pinched) {
noStroke();
fill(255, 0, 0, 200);
circle(
width * (1.0 - (landmarks[4].x + (landmarks[8].x - landmarks[4].x) / 2)),
height * (landmarks[4].y + (landmarks[8].y - landmarks[4].y) / 2),
50);
}
}
for (pose of poses) {
if (pose.class == 'scissors' && pose.active == true) {
noStroke();
fill(150, 0, 0, 150);
let my_x = width * (1.0 - (landmarks[12].x + (landmarks[8].x - landmarks[12].x) / 2));
let my_y = height * (landmarks[12].y + (landmarks[8].y - landmarks[12].y) / 2);
arc(width * (1.0 - (landmarks[12].x + (landmarks[8].x - landmarks[12].x) / 2)),
height * (landmarks[12].y + (landmarks[8].y - landmarks[12].y) / 2),
80, 80,
0,
TWO_PI * ((millis() - pose.timestamp) / ms_operation_activate),
PIE);
if ((millis() - pose.timestamp) / ms_operation_activate > 1.0) {
pose.active = false;
if (g_browser_mode == false) {
(async () => {
if (my_x < width / 2) {
const message = await window.myapi.pressKey({
kind: 'left'
})
message = await window.myapi.releaseKey({
kind: 'left'
})
}
else {
const message = await window.myapi.pressKey({
kind: 'right'
})
message = await window.myapi.releaseKey({
kind: 'right'
})
}
})()
}
}
//text(millis() - timestamp_pose_active, width / 2, height / 2);
}
if (pose.class == '5_fingers_pinch' && pose.active == true) {
noStroke();
fill(150, 150, 150, 150);
let my_x = width * (1.0 - (landmarks[12].x + (landmarks[8].x - landmarks[12].x) / 2));
let my_y = height * (landmarks[12].y + (landmarks[8].y - landmarks[12].y) / 2);
arc(width * (1.0 - (landmarks[12].x + (landmarks[8].x - landmarks[12].x) / 2)),
height * (landmarks[12].y + (landmarks[8].y - landmarks[12].y) / 2),
80, 80,
0,
TWO_PI * ((millis() - pose.timestamp) / ms_operation_activate),
PIE);
if ((millis() - pose.timestamp) / ms_operation_activate > 1.0) {
pose.active = false;
trail_mode.toggle();
}
}
}
}
}
if (trails_right.have_nothing_to_draw == true &&
trails_left.have_nothing_to_draw == true) {
noLoop();
}
if (trail_mode.name == 'All Fingers') {
trails_right.draw();
trails_left.draw();
}
else if (trail_mode.name == 'Index Finger') {
trails_right.drawIndex();
trails_left.drawIndex();
}
// strokeWeight(1);
// text("p5 and MediaPipe Hands Demo", 20, 20);
// noFill();
// stroke(0);
// strokeWeight(5);
// rect(0, 0, width, height);
// circle(mouseX, mouseY, 20);
}
var videoElement;
var hands;
function keyPressed() {
if (key == ' ') {
//console.log(g_angles);
trail_mode.toggle();
}
}
function toggleFingerGesture(_enable_mouse) {
is_enable_mouse = _enable_mouse;
}
function toggleMediaPipeHands(_deviceId) {
if (document.querySelector('video')) {
stopMediaPipeHands();
}
else {
startMediaPipeHands(_deviceId);
}
}
function startMediaPipeHands(_deviceId) {
videoElement = document.createElement('video');// document.getElementsByClassName('input_video')[0];
document.querySelector('body').appendChild(videoElement);
videoElement.hidden = true;
hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}
});
hands.setOptions({
maxNumHands: 2,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
hands.onResults(onResults);
camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({ image: videoElement });
},
audio: false,
width: 1280,
height: 720,
deviceId: _deviceId
});
stream = camera.start();
}
function stopMediaPipeHands() {
if (document.querySelector('video')) {
let stream = videoElement.srcObject;
window.cancelAnimationFrame(id_callback_camera_utils);
stream.getTracks().forEach(track => track.stop())
hands.close();
videoElement.remove();
}
g_landmarks = [];
draw();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function showDevices() {
navigator.mediaDevices.enumerateDevices()
.then(function (devices) {
devices.forEach(function (device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
});
return devices;
})
.catch(function (err) {
console.log(err.name + ": " + err.message);
});
}