Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files1232345678910111213141516171819202122232425262728293031323334353637383940414243444510111213141516174647484950515253545556572021222324255859606162272829303132333435366364656667686970383940414271727374447576777879808182838485868788899091929394959697989910010110210310410510647484950515253545556575859107108<!DOCTYPE html><html><head> <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands"></script> <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils"></script> <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 }); document.getElementById('video').srcObject = videoStream;
const video = document.getElementById("video"); const canvas = document.getElementById("outputCanvas"); const ctx = canvas.getContext("2d");
const hands = new Hands({ locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}` videoElement.srcObject = videoStream; videoElement.play(); camera = new Camera(videoElement, { onFrame: async () => { await hands.send({ image: videoElement }); }, width: 400, height: 300 }); camera.start(); }
hands.setOptions({ maxNumHands: 2, modelComplexity: 1, minDetectionConfidence: 0.5, minTrackingConfidence: 0.5 }); function toggleMode() { isTraining = !isTraining; document.getElementById('modeButton').innerText = isTraining ? "Switch to Test Mode" : "Switch to Train Mode"; }
hands.onResults((results) => { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(video, 0, 0, canvas.width, canvas.height); if (results.multiHandLandmarks) { for (const landmarks of results.multiHandLandmarks) { drawConnectors(ctx, landmarks, HAND_CONNECTIONS, { color: "#00FF00", lineWidth: 2 }); drawLandmarks(ctx, landmarks, { color: "#FF0000", radius: 4 }); } } }); 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;
const camera = new Camera(video, { onFrame: async () => { await hands.send({ image: video }); }, width: 640, height: 480 }); let blob = await fetch(dataURL).then(res => res.blob()); let formData = new FormData(); formData.append("file", blob, "capture.jpg");
camera.start(); 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></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> <video id="video" autoplay></video> <canvas id="outputCanvas" width="640" height="480"></canvas> <br> <button onclick="captureImage()">Capture Image</button> <h3 id="prediction-box">Prediction: </h3> </div></body></html>templates/index.html+93-45