-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoseProcessor.js
277 lines (231 loc) · 10 KB
/
PoseProcessor.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
// PoseProcessor.js
import { PoseLandmarker, DrawingUtils, FilesetResolver } from "https://cdn.skypack.dev/@mediapipe/tasks-vision@0.10.18";
import { drawShinai } from './drawShinai.js';
export class PoseProcessor {
constructor(canvasElement, videoElement, messageElement) {
this.canvasElement = canvasElement;
this.canvasCtx = canvasElement.getContext("2d");
this.videoElement = videoElement;
this.messageElement = messageElement;
this.poseLandmarker = null;
this.currentStage = this.STAGES.IDLE;
this.stableStartTime = null;
this.initialHandPositions = null;
this.webcamRunning = false;
this.processingVideoFile = false;
// Adjust thresholds based on pixel measurements
this.HAND_STABILITY_THRESHOLD = 10; // pixels
this.SWING_START_THRESHOLD = 30; // pixels
// Initialize pose landmarker
this.createPoseLandmarker();
}
// Define stages as constants
STAGES = {
IDLE: 0,
WEBCAM_ON: 1,
READY_CHECK: 2,
READY: 3,
SWING: 4,
SHOW_RESULTS: 5,
};
async createPoseLandmarker() {
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm"
);
this.poseLandmarker = await PoseLandmarker.createFromOptions(vision, {
baseOptions: {
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_full/float16/1/pose_landmarker_full.task`,
delegate: "GPU"
},
runningMode: "VIDEO",
numPoses: 1
});
}
updateMessage(text, color = 'black') {
this.messageElement.style.color = color;
this.messageElement.innerHTML = text;
}
startWebcamStream() {
const constraints = { video: true };
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
this.videoElement.srcObject = stream;
this.videoElement.play();
this.webcamRunning = true;
this.processingVideoFile = false;
this.currentStage = this.STAGES.WEBCAM_ON;
this.updateMessage("Webcam enabled. Click 'Start Analysis' when ready", "blue");
this.videoElement.addEventListener("loadeddata", () => this.processVideo());
});
}
stopWebcamStream() {
if (this.videoElement.srcObject) {
this.videoElement.srcObject.getTracks().forEach(track => track.stop());
}
this.webcamRunning = false;
this.currentStage = this.STAGES.IDLE;
this.updateMessage("");
}
startVideoFileProcessing(file) {
const fileURL = URL.createObjectURL(file);
this.videoElement.src = fileURL;
this.videoElement.play();
this.webcamRunning = true;
this.processingVideoFile = true;
this.currentStage = this.STAGES.WEBCAM_ON;
this.updateMessage("Video loaded. Click 'Start Analysis' when ready", "blue");
this.videoElement.addEventListener("loadeddata", () => this.processVideo());
}
stopVideoFileProcessing() {
this.videoElement.pause();
this.videoElement.src = '';
this.webcamRunning = false;
this.processingVideoFile = false;
this.currentStage = this.STAGES.IDLE;
this.updateMessage("");
}
startAnalysis() {
if (this.currentStage === this.STAGES.WEBCAM_ON || this.currentStage === this.STAGES.SHOW_RESULTS) {
this.currentStage = this.STAGES.READY_CHECK;
this.stableStartTime = null;
this.initialHandPositions = null;
this.updateMessage("Analysis started. Please hold still.", "blue");
}
}
async processVideo() {
if (!this.webcamRunning) return;
// Check if the video has ended when processing a video file
if (this.processingVideoFile && this.videoElement.ended) {
this.updateMessage("Video playback ended.", "blue");
this.stopVideoFileProcessing();
return;
}
const results = await this.poseLandmarker.detectForVideo(this.videoElement, performance.now());
this.canvasCtx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
this.canvasCtx.drawImage(this.videoElement, 0, 0, this.canvasElement.width, this.canvasElement.height);
if (results.landmarks && results.landmarks[0]) {
const landmarks = results.landmarks[0];
// Draw pose landmarks
const drawingUtils = new DrawingUtils(this.canvasCtx);
drawingUtils.drawLandmarks(landmarks, { radius: 3 });
drawingUtils.drawConnectors(landmarks, PoseLandmarker.POSE_CONNECTIONS);
// Draw shinai and get hand centers
const handCenters = drawShinai(landmarks, this.canvasElement, this.canvasCtx);
// Handle stages
this.handleStages(handCenters);
// Display current stage on canvas
this.displayStageOnCanvas();
} else {
this.updateMessage("No pose detected", "red");
}
requestAnimationFrame(() => this.processVideo());
}
handleStages(handCenters) {
if (!handCenters) {
this.updateMessage("Hands not detected", "red");
return;
}
const { leftHandCenter, rightHandCenter } = handCenters;
switch (this.currentStage) {
case this.STAGES.WEBCAM_ON:
this.updateMessage("Webcam/Video is on. Click 'Start Analysis' to begin.", "blue");
break;
case this.STAGES.READY_CHECK:
this.performReadyCheck(leftHandCenter, rightHandCenter);
break;
case this.STAGES.READY:
this.detectSwingStart(leftHandCenter, rightHandCenter);
break;
case this.STAGES.SWING:
this.calculateSwingAngle(handCenters);
break;
case this.STAGES.SHOW_RESULTS:
// Results have been shown; waiting for next action
break;
default:
break;
}
}
performReadyCheck(leftHandCenter, rightHandCenter) {
if (!this.initialHandPositions) {
this.initialHandPositions = { left: leftHandCenter, right: rightHandCenter };
this.stableStartTime = performance.now();
this.updateMessage("Please hold still", "blue");
} else {
const leftMovement = this.distanceBetweenPoints(leftHandCenter, this.initialHandPositions.left);
const rightMovement = this.distanceBetweenPoints(rightHandCenter, this.initialHandPositions.right);
if (leftMovement < this.HAND_STABILITY_THRESHOLD && rightMovement < this.HAND_STABILITY_THRESHOLD) {
const elapsedTime = performance.now() - this.stableStartTime;
this.updateMessage(`Hold still... ${Math.max(0, (2 - (elapsedTime / 1000))).toFixed(1)}s`, "blue");
if (elapsedTime >= 2000) {
this.currentStage = this.STAGES.READY;
this.updateMessage("Ready! Make your swing!", "green");
}
} else {
this.stableStartTime = performance.now();
this.initialHandPositions = { left: leftHandCenter, right: rightHandCenter };
this.updateMessage("Too much movement, please hold still", "red");
}
}
}
detectSwingStart(leftHandCenter, rightHandCenter) {
const leftMovement = this.distanceBetweenPoints(leftHandCenter, this.initialHandPositions.left);
const rightMovement = this.distanceBetweenPoints(rightHandCenter, this.initialHandPositions.right);
if (leftMovement >= this.SWING_START_THRESHOLD || rightMovement >= this.SWING_START_THRESHOLD) {
this.currentStage = this.STAGES.SWING;
this.updateMessage("Swing detected!", "green");
}
}
calculateSwingAngle(handCenters) {
const { leftHandCenter, rightHandCenter } = handCenters;
const shinaiVector = {
x: leftHandCenter.x - rightHandCenter.x,
y: leftHandCenter.y - rightHandCenter.y
};
const magnitude = Math.sqrt(shinaiVector.x ** 2 + shinaiVector.y ** 2);
const shinaiDirection = {
x: shinaiVector.x / magnitude,
y: shinaiVector.y / magnitude
};
const referenceVector = { x: 0, y: -1 }; // Upward vector
const dotProduct = shinaiDirection.x * referenceVector.x + shinaiDirection.y * referenceVector.y;
const angleRadians = Math.acos(dotProduct);
const angleDegrees = (angleRadians * 180) / Math.PI;
const cutType = angleDegrees > 45 ? 'Big Cut' : 'Small Cut';
this.currentStage = this.STAGES.SHOW_RESULTS;
this.updateMessage(`Swing Angle: ${angleDegrees.toFixed(2)}°<br>Cut Type: ${cutType}<br><br>Click 'Start Analysis' to try again`, "blue");
}
distanceBetweenPoints(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
displayStageOnCanvas() {
this.canvasCtx.font = "20px Arial";
this.canvasCtx.fillStyle = "yellow";
let stageText = "";
switch (this.currentStage) {
case this.STAGES.IDLE:
stageText = "Idle";
break;
case this.STAGES.WEBCAM_ON:
stageText = "Webcam/Video On";
break;
case this.STAGES.READY_CHECK:
stageText = "Ready Check";
break;
case this.STAGES.READY:
stageText = "Ready";
break;
case this.STAGES.SWING:
stageText = "Swing";
break;
case this.STAGES.SHOW_RESULTS:
stageText = "Show Results";
break;
default:
stageText = "Unknown Stage";
break;
}
this.canvasCtx.fillText(`Stage: ${stageText}`, 10, 30);
}
}