Spaces:
Sleeping
Sleeping
Download templates/index.html from prashantscripts/gesture: direct link, hf CLI and curl.
- Browser
- Download file 4.71 kB
-
https://huggingface.co/spaces/prashantscripts/gesture/resolve/main/templates/index.html
- Command line
-
hf download hf://spaces/prashantscripts/gesture/templates/index.html
-
curl -L -o index.html https://huggingface.co/spaces/prashantscripts/gesture/resolve/main/templates/index.html
4.71 kB
| <html> | |
| <head> | |
| <title>Live Gesture Recognition</title> | |
| <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js"></script> | |
| <style> | |
| body { font-family: Arial, sans-serif; text-align: center; background: #f4f4f4; } | |
| h2 { color: #333; } | |
| button { padding: 10px 20px; margin: 10px; border: none; cursor: pointer; background: #007BFF; color: white; font-size: 16px; border-radius: 5px; } | |
| button:hover { background: #0056b3; } | |
| .container { max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); } | |
| canvas { position: absolute; top: 0; left: 0; } | |
| video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } | |
| .video-container { position: relative; width: 400px; height: 300px; margin: auto; } | |
| #prediction-box { margin-top: 10px; padding: 10px; background: lightgray; border-radius: 5px; display: inline-block; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h2>Live Gesture Recognition</h2> | |
| <button onclick="startCamera()">Start Camera</button> | |
| <button id="modeButton" onclick="toggleMode()">Switch to Train Mode</button> | |
| <br> | |
| <div class="video-container"> | |
| <video id="video" autoplay></video> | |
| <canvas id="canvas"></canvas> | |
| </div> | |
| <br> | |
| <button onclick="captureImage()">Capture Image</button> | |
| <br> | |
| <img id="capturedImage" /> | |
| <h3 id="prediction-box">Prediction: </h3> | |
| </div> | |
| <script> | |
| let videoStream; | |
| let isTraining = false; | |
| let videoElement = document.getElementById('video'); | |
| let canvasElement = document.getElementById('canvas'); | |
| let canvasCtx = canvasElement.getContext('2d'); | |
| async function startCamera() { | |
| videoStream = await navigator.mediaDevices.getUserMedia({ video: true }); | |
| videoElement.srcObject = videoStream; | |
| videoElement.play(); | |
| camera = new Camera(videoElement, { | |
| onFrame: async () => { | |
| await hands.send({ image: videoElement }); | |
| }, | |
| width: 400, | |
| height: 300 | |
| }); | |
| camera.start(); | |
| } | |
| function toggleMode() { | |
| isTraining = !isTraining; | |
| document.getElementById('modeButton').innerText = isTraining ? "Switch to Test Mode" : "Switch to Train Mode"; | |
| } | |
| async function captureImage() { | |
| let canvas = document.createElement('canvas'); | |
| canvas.width = videoElement.videoWidth; | |
| canvas.height = videoElement.videoHeight; | |
| canvas.getContext('2d').drawImage(videoElement, 0, 0); | |
| let dataURL = canvas.toDataURL('image/jpeg'); | |
| document.getElementById('capturedImage').src = dataURL; | |
| let blob = await fetch(dataURL).then(res => res.blob()); | |
| let formData = new FormData(); | |
| formData.append("file", blob, "capture.jpg"); | |
| if (isTraining) { | |
| let label = prompt("Enter gesture name:"); | |
| formData.append("label", label); | |
| await fetch("/train", { method: "POST", body: formData }); | |
| } else { | |
| let response = await fetch("/predict", { method: "POST", body: formData }); | |
| let result = await response.json(); | |
| document.getElementById('prediction-box').innerText = "Prediction: " + result.prediction; | |
| } | |
| } | |
| const hands = new Hands({ locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}` }); | |
| hands.setOptions({ | |
| maxNumHands: 2, | |
| modelComplexity: 1, | |
| minDetectionConfidence: 0.5, | |
| minTrackingConfidence: 0.5 | |
| }); | |
| hands.onResults(results => { | |
| canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height); | |
| canvasCtx.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height); | |
| if (results.multiHandLandmarks) { | |
| for (const landmarks of results.multiHandLandmarks) { | |
| drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, { color: '#00FF00', lineWidth: 2 }); | |
| drawLandmarks(canvasCtx, landmarks, { color: '#FF0000', radius: 4 }); | |
| } | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> | |