-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
executable file
·253 lines (222 loc) · 8.09 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
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
import vision from "https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]";
const { FaceLandmarker, FilesetResolver, DrawingUtils, GestureRecognizer } = vision;
import { GestureTracker } from './gesture_tracker.js';
let faceLandmarker;
let gestureRecognizer;
let runningMode = "VIDEO";
let enableWebcamButton;
let webcamRunning = false;
const videoWidth = 800;
const color_map = Object.freeze({
fucsia: '#CA2C92',
teal: '#008080',
red: '#FF0000',
green: '#00FF00',
light_grey: '#C0C0C070',
grey: '#E0E0E0',
solid_black: '#000'
});
async function createFaceLandmarker() {
const filesetResolver = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/wasm"
);
faceLandmarker = await FaceLandmarker.createFromOptions(filesetResolver, {
baseOptions: {
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task`,
delegate: "GPU"
},
outputFaceBlendshapes: true,
runningMode,
numFaces: 1
});
gestureRecognizer = await GestureRecognizer.createFromOptions(filesetResolver, {
baseOptions: {
modelAssetPath: "https://storage.googleapis.com/mediapipe-tasks/gesture_recognizer/gesture_recognizer.task"
},
delegate: "GPU",
runningMode,
numHands: 2
});
}
createFaceLandmarker();
const video = document.getElementById("webcam");
const canvasElement = document.getElementById("output_canvas");
const canvasCtx = canvasElement.getContext("2d");
function hasGetUserMedia() {
return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}
if (hasGetUserMedia()) {
enableWebcamButton = document.getElementById("webcamButton");
enableWebcamButton.addEventListener("click", enableCam);
} else {
console.warn("getUserMedia() is not supported by your browser");
}
function enableCam(event) {
if (!faceLandmarker) {
console.log("Wait! faceLandmarker not loaded yet.");
return;
}
const button = document.getElementById("webcamButton");
const buttonIcon = button.querySelector('[data-lucide="camera"]') ||
button.querySelector('[data-lucide="camera-off"]');
const buttonText = button.querySelector('span');
if (webcamRunning === true) {
webcamRunning = false;
// Update button state
buttonIcon.setAttribute('data-lucide', 'camera');
buttonText.textContent = 'Start Camera';
// Stop video tracks
if (video.srcObject) {
video.srcObject.getTracks().forEach(track => track.stop());
}
video.srcObject = null;
lucide.createIcons();
return;
}
webcamRunning = true;
// Update button state
buttonIcon.setAttribute('data-lucide', 'camera-off');
buttonText.textContent = 'Stop Camera';
lucide.createIcons();
const constraints = {
video: {
facingMode: 'environment'
}
};
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
video.srcObject = stream;
video.addEventListener("loadedmetadata", () => {
// Set canvas dimensions once video metadata is loaded
const ratio = video.videoHeight / video.videoWidth;
canvasElement.width = video.videoWidth;
canvasElement.height = video.videoHeight;
canvasElement.style.width = videoWidth + "px";
canvasElement.style.height = (videoWidth * ratio) + "px";
video.style.width = videoWidth + "px";
video.style.height = (videoWidth * ratio) + "px";
});
video.addEventListener("loadeddata", predictWebcam);
});
}
let lastVideoTime = -1;
let results = undefined;
let h_results = null;
const drawingUtils = new DrawingUtils(canvasCtx);
const gestureTracker = new GestureTracker();
async function predictWebcam() {
// Ensure canvas dimensions are valid
if (!video.videoWidth || !video.videoHeight) {
if (webcamRunning) {
window.requestAnimationFrame(predictWebcam);
}
return;
}
// Update canvas dimensions if they don't match video
if (canvasElement.width !== video.videoWidth ||
canvasElement.height !== video.videoHeight) {
const ratio = video.videoHeight / video.videoWidth;
canvasElement.width = video.videoWidth;
canvasElement.height = video.videoHeight;
canvasElement.style.width = videoWidth + "px";
canvasElement.style.height = (videoWidth * ratio) + "px";
}
if (runningMode !== 'VIDEO') {
runningMode = 'VIDEO';
await faceLandmarker.setOptions({ runningMode: runningMode });
await gestureRecognizer.setOptions({ runningMode: runningMode });
}
let startTimeMs = performance.now();
if (lastVideoTime !== video.currentTime) {
lastVideoTime = video.currentTime;
results = faceLandmarker.detectForVideo(video, startTimeMs);
h_results = gestureRecognizer.recognizeForVideo(video, startTimeMs);
}
// Clear the canvas before drawing
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.fillStyle = color_map.solid_black;
canvasCtx.fillRect(0, 0, canvasElement.width, canvasElement.height);
if (results.faceLandmarks) {
for (const landmarks of results.faceLandmarks) {
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_TESSELATION,
{ color: color_map.light_grey, lineWidth: 1 }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_RIGHT_EYE,
{ color: color_map.fucsia }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_RIGHT_EYEBROW,
{ color: color_map.fucsia }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_LEFT_EYE,
{ color: color_map.teal }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_LEFT_EYEBROW,
{ color: color_map.teal }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_FACE_OVAL,
{ color: color_map.grey }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_LIPS,
{ color: color_map.grey }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_RIGHT_IRIS,
{ color: color_map.fucsia }
);
drawingUtils.drawConnectors(
landmarks,
FaceLandmarker.FACE_LANDMARKS_LEFT_IRIS,
{ color: color_map.teal }
);
}
if (h_results.landmarks) {
for (const landmarks of h_results.landmarks) {
drawingUtils.drawConnectors(
landmarks,
GestureRecognizer.HAND_CONNECTIONS,
{
color: color_map.green,
lineWidth: 5
}
);
drawingUtils.drawLandmarks(landmarks, {
color: color_map.red,
lineWidth: 1
});
}
}
if (results.faceLandmarks.length > 0) {
gestureTracker.updateFaceData(results.faceLandmarks);
gestureTracker.trackHeadShakeGesture(performance.now());
if (h_results.landmarks?.length > 0) {
gestureTracker.trackThinkingGesture(h_results.landmarks, performance.now());
gestureTracker.trackSilenceGesture(h_results.landmarks, performance.now());
gestureTracker.trackMindBlownGesture(h_results.landmarks, performance.now());
gestureTracker.trackThumbsUpGesture(h_results);
gestureTracker.trackHeadShakeGesture(performance.now());
gestureTracker.trackASLYesGesture(h_results.landmarks, performance.now());
gestureTracker.trackASLHelloGesture(h_results.landmarks, performance.now());
gestureTracker.trackASLNoGesture(h_results.landmarks, performance.now());
gestureTracker.trackASLThankYouGesture(h_results.landmarks, performance.now());
}
gestureTracker.drawGestureText(canvasCtx);
}
}
if (webcamRunning === true) {
window.requestAnimationFrame(predictWebcam);
}
}