Spaces:
Sleeping
Sleeping
Update app.py
Browse files123424 unmodified lines29303132333435363738394041424344454647485 unmodified lines5455565758596061626321 unmodified lines8586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191 unmodified line121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151from fastapi import FastAPI, WebSocket, UploadFile, File, Formfrom 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); }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; }</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 linesfunction setupHandTracking() {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(drawHandLandmarks);detectHands();21 unmodified linesresults.multiHandLandmarks.forEach((landmarks) => {drawConnectors(ctx, landmarks, HAND_CONNECTIONS, {color: 'blue', lineWidth: 2});drawLandmarks(ctx, landmarks, {color: 'red', radius: 5});});}}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";}</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><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)123424 unmodified lines29303132333435363738394041424344455 unmodified lines5152535455565758596021 unmodified lines8283848586878889909192939495969798991001011 unmodified line103104105106107108109110111112113114115116117118from 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; }.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; }canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }#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 hands;async function startCamera() {5 unmodified linesfunction setupHandTracking() {hands = new Hands({locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`});hands.setOptions({maxNumHands: 1,modelComplexity: 1,minDetectionConfidence: 0.7,minTrackingConfidence: 0.7});hands.onResults(drawHandLandmarks);detectHands();21 unmodified linesresults.multiHandLandmarks.forEach((landmarks) => {drawConnectors(ctx, landmarks, HAND_CONNECTIONS, {color: 'blue', lineWidth: 2});drawLandmarks(ctx, landmarks, {color: 'red', radius: 5});drawBoundingBox(ctx, landmarks);});}}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><br><div class="video-container"><video id="video" autoplay></video><canvas id="canvas"></canvas><div id="prediction-box">Gesture: </div></div></div></body></html>"""if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)
app.py+18-51