-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (117 loc) · 4.21 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
import {
HandLandmarker,
FilesetResolver,
} from "https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]";
const demosSection = document.getElementById("demos");
let handLandmarker = undefined;
let runningMode = "IMAGE";
let enableWebcamButton;
let webcamRunning = false;
// Before we can use HandLandmarker class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
const createHandLandmarker = async () => {
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/wasm"
);
handLandmarker = await HandLandmarker.createFromOptions(vision, {
baseOptions: {
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task`,
delegate: "GPU",
},
runningMode: runningMode,
numHands: 2,
});
demosSection.classList.remove("invisible");
};
createHandLandmarker();
/********************************************************************
// Demo 2: Continuously grab image from webcam stream and detect it.
********************************************************************/
const video = document.getElementById("webcam");
const canvasElement = document.getElementById("output_canvas");
const canvasCtx = canvasElement.getContext("2d");
// Check if webcam access is supported.
const hasGetUserMedia = () => {
var _a;
return !!((_a = navigator.mediaDevices) === null || _a === void 0
? void 0
: _a.getUserMedia);
};
// If webcam supported, add event listener to button for when user
// wants to activate it.
if (hasGetUserMedia()) {
enableWebcamButton = document.getElementById("webcamButton");
enableWebcamButton.addEventListener("click", enableCam);
} else {
console.warn("getUserMedia() is not supported by your browser");
}
// Enable the live webcam view and start detection.
function enableCam(event) {
if (!handLandmarker) {
console.log("Wait! objectDetector not loaded yet.");
return;
}
if (webcamRunning === true) {
webcamRunning = false;
enableWebcamButton.innerText = "ENABLE PREDICTIONS";
} else {
webcamRunning = true;
enableWebcamButton.disabled = true;
enableWebcamButton.innerText = "NOW, START DRAWING!";
}
// getUsermedia parameters.
const constraints = {
video: {
width: { ideal: 1280 },
height: { ideal: 720 },
},
};
// Activate the webcam stream.
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
video.srcObject = stream;
video.addEventListener("loadeddata", predictWebcam);
});
}
let lastVideoTime = -1;
let results = undefined;
async function predictWebcam() {
canvasElement.style.width = video.videoWidth;
canvasElement.style.height = video.videoHeight;
canvasElement.width = video.videoWidth;
canvasElement.height = video.videoHeight;
// Now let's start detecting the stream.
if (runningMode === "IMAGE") {
runningMode = "VIDEO";
await handLandmarker.setOptions({ runningMode: "VIDEO" });
}
let startTimeMs = performance.now();
if (lastVideoTime !== video.currentTime) {
lastVideoTime = video.currentTime;
results = handLandmarker.detectForVideo(video, startTimeMs);
}
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
let handPoints = [];
let handPointsOtherHand = [];
if (results.landmarks) {
for(let h = 0; h < results.landmarks.length; h++){
let landmarks = results.landmarks[h];
for (let i = 0; i < landmarks.length; i++) {
let point = landmarks[i];
if(h == 0 && (i==0 || i == 1 || i==4 || i==8 || i ==12 || i==16 || i == 17 || i==20)){
handPoints.push([point.x, point.y]);
} else if(h == 1 && (i==0 || i == 1 || i==4 || i==8 || i ==12 || i==16 || i == 17 || i==20)) {
handPointsOtherHand.push([point.x, point.y]);
}
}
}
setHandPoints(handPoints); //Guardar puntos primera mano
setHandPointsOtherHand(handPointsOtherHand); // Guardar puntos segunda mano
canvasCtx.restore();
// Call this function again to keep predicting when the browser is ready.
if (webcamRunning === true) {
window.requestAnimationFrame(predictWebcam);
}
}
}