Spaces:
Running
Running
srivatsavdamaraju
commited on
Update index.html
Browse files- index.html +97 -18
index.html
CHANGED
@@ -1,19 +1,98 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</html>
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>MediaPipe Face Mesh</title>
|
7 |
+
|
8 |
+
|
9 |
+
<style>
|
10 |
+
body {
|
11 |
+
display: flex;
|
12 |
+
justify-content: center;
|
13 |
+
align-items: center;
|
14 |
+
height: 100vh;
|
15 |
+
margin: 0;
|
16 |
+
background-color: #000;
|
17 |
+
}
|
18 |
+
video {
|
19 |
+
display: none;
|
20 |
+
}
|
21 |
+
canvas {
|
22 |
+
position: absolute;
|
23 |
+
top: 0;
|
24 |
+
left: 0;
|
25 |
+
z-index: 1;
|
26 |
+
}
|
27 |
+
</style>
|
28 |
+
</head>
|
29 |
+
<body>
|
30 |
+
<video id="video" autoplay playsinline></video>
|
31 |
+
<canvas id="canvas"></canvas>
|
32 |
+
|
33 |
+
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js"></script>
|
34 |
+
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/face_mesh.js"></script>
|
35 |
+
<script>
|
36 |
+
const videoElement = document.getElementById('video');
|
37 |
+
const canvasElement = document.getElementById('canvas');
|
38 |
+
const canvasCtx = canvasElement.getContext('2d');
|
39 |
+
|
40 |
+
// Create a FaceMesh instance
|
41 |
+
const faceMesh = new FaceMesh({
|
42 |
+
locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${file}`
|
43 |
+
});
|
44 |
+
|
45 |
+
faceMesh.onResults(onFaceMeshResults);
|
46 |
+
|
47 |
+
// Initialize camera
|
48 |
+
async function initCamera() {
|
49 |
+
try {
|
50 |
+
const stream = await navigator.mediaDevices.getUserMedia({
|
51 |
+
video: true
|
52 |
+
});
|
53 |
+
videoElement.srcObject = stream;
|
54 |
+
|
55 |
+
const camera = new Camera(videoElement, {
|
56 |
+
onFrame: async () => {
|
57 |
+
await faceMesh.send({ image: videoElement });
|
58 |
+
},
|
59 |
+
width: 640,
|
60 |
+
height: 480
|
61 |
+
});
|
62 |
+
camera.start();
|
63 |
+
} catch (error) {
|
64 |
+
console.error('Error accessing the camera:', error);
|
65 |
+
}
|
66 |
+
}
|
67 |
+
|
68 |
+
initCamera();
|
69 |
+
|
70 |
+
function onFaceMeshResults(results) {
|
71 |
+
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
72 |
+
canvasElement.width = videoElement.videoWidth;
|
73 |
+
canvasElement.height = videoElement.videoHeight;
|
74 |
+
|
75 |
+
// Draw the video
|
76 |
+
canvasCtx.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
|
77 |
+
|
78 |
+
// Draw face mesh
|
79 |
+
if (results.multiFaceLandmarks) {
|
80 |
+
results.multiFaceLandmarks.forEach(landmarks => {
|
81 |
+
const keypoints = landmarks.map(landmark => ({
|
82 |
+
x: landmark.x * canvasElement.width,
|
83 |
+
y: landmark.y * canvasElement.height
|
84 |
+
}));
|
85 |
+
|
86 |
+
// Draw keypoints
|
87 |
+
canvasCtx.fillStyle = 'red';
|
88 |
+
keypoints.forEach(point => {
|
89 |
+
canvasCtx.beginPath();
|
90 |
+
canvasCtx.arc(point.x, point.y, 1, 0, 2 * Math.PI);
|
91 |
+
canvasCtx.fill();
|
92 |
+
});
|
93 |
+
});
|
94 |
+
}
|
95 |
+
}
|
96 |
+
</script>
|
97 |
+
</body>
|
98 |
</html>
|