-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
151 lines (133 loc) · 3.96 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
let isLoaded = false;
let currentPosition = "UP";
let pushUps = 0;
const canvas = utils.createElement("canvas", {
id: "canvas",
});
const ctx = canvas.getContext("2d");
const video = document.getElementById("videoElement");
const snd = new Audio("a-tone.wav");
const getPose = async () => {
const poses = await Detector.estimatePoses(video);
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
if (!isLoaded) {
document.querySelector(".container p").remove();
isLoaded = true;
}
const drawPoint = (y, x, r, name) => {
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI);
ctx.fillStyle = "#ff0000";
ctx.fill();
ctx.font = "7px Arial";
ctx.fillText(name, x + 7, y + 2);
};
const drawSegment = (pair1, pair2, color, scale) => {
ctx.beginPath();
ctx.moveTo(pair1.x * scale, pair1.y * scale);
ctx.lineTo(pair2.x * scale, pair2.y * scale);
ctx.lineWidth = 2;
ctx.strokeStyle = color;
ctx.stroke();
};
const drawKeypoints = (keypoints) => {
for (let i = 0; i < keypoints.length; i++) {
const keypoint = keypoints[i];
const { y, x } = keypoint;
drawPoint(y, x, 5, keypoint.name);
}
};
if (poses.length) {
const { keypoints } = poses[0];
let i = 0;
while (i != keypoints.length) {
if (keypoints[i]['score'] < 0.3) {
keypoints.splice(i, 1)
i--;
}
i++;
}
// console.log(keypoints)
drawKeypoints(keypoints);
try{
let elbowPositions = keypoints.filter((k) => {
return k.name === "left_elbow" || k.name === "right_elbow";
});
let leftShoulder = keypoints.filter((k) => {
return k.name === "left_shoulder";
});
let rightShoulder = keypoints.filter((k) => {
return k.name === "right_shoulder";
});
// console.log(elbowPositions['score'])
// console.log(elbowPositions)
// draw segment between left_elbow and right_elbow
drawSegment(
{ x: elbowPositions[0].x, y: elbowPositions[0].y },
{ x: elbowPositions[1].x, y: elbowPositions[1].y },
"red",
canvas.width / video.videoWidth
);
let leftShoulderDistance = utils.getPointToLine(
leftShoulder[0].x,
leftShoulder[0].y,
elbowPositions[0].x,
elbowPositions[0].y,
elbowPositions[1].x,
elbowPositions[1].y
);
let rightShoulderDistance = utils.getPointToLine(
rightShoulder[0].x,
rightShoulder[0].y,
elbowPositions[0].x,
elbowPositions[0].y,
elbowPositions[1].x,
elbowPositions[1].y
);
if (leftShoulderDistance + rightShoulderDistance < 120) {
currentPosition = "DOWN";
document.getElementById("indicator_position").innerText =
"Position: DOWN";
} else {
if (currentPosition === "DOWN") {
pushUps++;
snd.currentTime=0;
snd.play();
document.getElementById(
"indicator_pushups"
).innerText = `Pushups: ${pushUps}`;
}
currentPosition = "UP";
document.getElementById("indicator_position").innerText = "Position: UP";
}
// if (currentPosition === "DOWN") {
// pushUps++;
// document.getElementById(
// "indicator_pushups"
// ).innerText = `Pushups: ${pushUps}`;
// }
}
catch {}
}
};
navigator.mediaDevices
.getUserMedia({ video: true, audio: false })
.then((stream) => {
window.stream = stream;
video.srcObject = stream;
video.addEventListener("loadeddata", (event) => {
document.querySelector(".container").appendChild(canvas);
poseDetection
.createDetector(poseDetection.SupportedModels.MoveNet)
.then((detector) => {
window.Detector = detector;
setInterval(getPose, 10);
});
});
})
.catch((error) => {
console.log(error)
console.log("Camera blocked");
});