Spaces:
Sleeping
Sleeping
Update app.py
Browse files1123424 unmodified lines293031323332333634353836373839404142454344455 unmodified lines5152535754555960565758596021 unmodified lines82838485868788899293949596979899100101102103104105106107108109110111112113114115116909192939495969798991001011 unmodified line103104105124106107108109110111130131132133134112113114115116140141142143144145146147148149117151118from fastapi import FastAPI, WebSocket, UploadFile, File, Formfrom fastapi import FastAPI, WebSocket, UploadFile, Filefrom fastapi.responses import HTMLResponse, JSONResponseimport numpy as npimport tensorflow as tf24 unmodified lines <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); } .video-container { position: relative; display: flex; justify-content: center; align-items: center; width: 400px; height: 400px; margin: auto; } video { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); } video { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; } canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } #prediction-box { margin-top: 10px; padding: 10px; background: lightgray; border-radius: 5px; display: inline-block; } #prediction-box { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: rgba(0, 0, 0, 0.7); color: white; padding: 5px 10px; border-radius: 5px; } </style> <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/@tensorflow/tfjs"></script> <script> let videoStream; let isTraining = false; let hands;
async function startCamera() {5 unmodified lines function setupHandTracking() { hands = new Hands({locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`}); hands.setOptions({ maxNumHands: 2, maxNumHands: 1, modelComplexity: 1, minDetectionConfidence: 0.5, minTrackingConfidence: 0.5 minDetectionConfidence: 0.7, minTrackingConfidence: 0.7 }); hands.onResults(drawHandLandmarks); detectHands();21 unmodified lines results.multiHandLandmarks.forEach((landmarks) => { drawConnectors(ctx, landmarks, HAND_CONNECTIONS, {color: 'blue', lineWidth: 2}); drawLandmarks(ctx, landmarks, {color: 'red', radius: 5}); drawBoundingBox(ctx, landmarks); }); } }
async function captureImage() { let canvas = document.createElement('canvas'); let video = document.getElementById('video'); canvas.width = video.videoWidth; canvas.height = video.videoHeight; canvas.getContext('2d').drawImage(video, 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; } }
function toggleMode() { isTraining = !isTraining; document.getElementById('modeButton').innerText = isTraining ? "Switch to Test Mode" : "Switch to Train Mode"; function drawBoundingBox(ctx, landmarks) { let xMin = Math.min(...landmarks.map(p => p.x)) * ctx.canvas.width; let yMin = Math.min(...landmarks.map(p => p.y)) * ctx.canvas.height; let xMax = Math.max(...landmarks.map(p => p.x)) * ctx.canvas.width; let yMax = Math.max(...landmarks.map(p => p.y)) * ctx.canvas.height; ctx.strokeStyle = "yellow"; ctx.lineWidth = 3; ctx.strokeRect(xMin, yMin, xMax - xMin, yMax - yMin); document.getElementById('prediction-box').innerText = "Gesture: Recognizing..."; } </script> </head>1 unmodified line <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 id="prediction-box">Gesture: </div> </div> <br> <button onclick="captureImage()">Capture Image</button> <br> <img id="capturedImage" /> <h3 id="prediction-box">Prediction: </h3> </div> </body> </html> """
@app.post("/predict")async def predict(file: UploadFile = File(...)): if model is None: return JSONResponse(content={"error": "Model not trained yet."}, status_code=400) image_bytes = await file.read() image = preprocess_image(image_bytes) prediction = model.predict(image) predicted_class = np.argmax(prediction) return {"prediction": int(predicted_class)}
if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860) uvicorn.run(app, host="0.0.0.0", port=7860)app.py+18-51