forked from Ultimecia1463/GTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
91 lines (80 loc) · 3.01 KB
/
script.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
//const URL = "https://teachablemachine.withgoogle.com/models/g0WhTEM-Q/";
const URL = "https://teachablemachine.withgoogle.com/models/96Tk9csO0/";
let model, webcam,ctx, maxPredictions;
document.addEventListener('DOMContentLoaded', () => {
redSquare = document.querySelector('.red-square');
});
let x = 50;
let y = 50;
let speedX = 0.3;
let speedY = 0.3;
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmPose.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
const flip = false;
webcam = new tmPose.Webcam(300, 300, flip);
await webcam.setup();
await webcam.play();
window.requestAnimationFrame(loop);
const canvas = document.getElementById("canvas");
canvas.width = 375;
canvas.height = 360;
ctx = canvas.getContext("2d");
}
async function loop() {
webcam.update();
//await predict();
const { pose, posenetOutput } = await model.estimatePose(webcam.canvas);
const prediction = await model.predict(posenetOutput);
const maxProbabilityIndex = prediction.findIndex((item) => item.probability === Math.max(...prediction.map((item) => item.probability)));
if (prediction[maxProbabilityIndex].probability > 0.8) {
move(prediction, maxProbabilityIndex);
}
//console.log(prediction);
drawPose(pose);
window.requestAnimationFrame(loop);
}
async function move(prediction, i) {
if (prediction[i].className == 'fist') {
// Move right
x += speedX;
if (x > 300) {
x = 300;
}
redSquare.style.left = `${x}%`;
} else if (prediction[i].className == 'palm') {
// Move left
x -= speedX;
if (x < 0) {
x = 0;
}
redSquare.style.left = `${x}%`;
} else if (prediction[i].className == 'tup') {
// Move up
y -= speedY;
if (y < 0) {
y = 0;
}
redSquare.style.top = `${y}%`;
} else if (prediction[i].className == 'tdon') {
// Move down
y += speedY;
if (y > 300) {
y = 300;
}
redSquare.style.top = `${y}%`;
}
}
async function drawPose(pose) {
if (webcam.canvas) {
ctx.drawImage(webcam.canvas, 0, 0);
// draw the keypoints and skeleton
if (pose) {
const minPartConfidence = 0.5;
tmPose.drawKeypoints(pose.keypoints, minPartConfidence, ctx);
tmPose.drawSkeleton(pose.keypoints, minPartConfidence, ctx);
}
}
}