Spaces:
Sleeping
Sleeping
File size: 4,711 Bytes
8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 222c02e 8449ac9 | 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 | <!DOCTYPE html>
<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>
|