Spaces:
Sleeping
Sleeping
Update app.py
Browse files33 unmodified lines343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979827 unmodified lines1261271281297813013113213313413514 unmodified lines1501511529910010110210310410510610710810911011111211311411511611711811912012115315433 unmodified lines video { width: 50%; max-width: 400px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); } .container { max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); } #prediction-box { margin-top: 10px; padding: 10px; background: lightgray; border-radius: 5px; display: inline-block; } canvas { position: absolute; top: 0; left: 0; } </style> <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js"></script> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <script> let videoStream; let isTraining = false; let hands;
async function startCamera() { videoStream = await navigator.mediaDevices.getUserMedia({ video: true }); document.getElementById('video').srcObject = videoStream; setupHandTracking(); }
function 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(); }
async function detectHands() { let video = document.getElementById('video'); let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d'); canvas.width = video.videoWidth; canvas.height = video.videoHeight; async function processFrame() { ctx.clearRect(0, 0, canvas.width, canvas.height); await hands.send({image: video}); requestAnimationFrame(processFrame); } processFrame(); }
function drawHandLandmarks(results) { let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "red"; ctx.lineWidth = 2; results.multiHandLandmarks.forEach(landmarks => { for (let i = 0; i < landmarks.length; i++) { let x = landmarks[i].x * canvas.width; let y = landmarks[i].y * canvas.height; ctx.beginPath(); ctx.arc(x, y, 5, 0, 2 * Math.PI); ctx.fill(); } }); }
async function captureImage() { let canvas = document.createElement('canvas'); let video = document.getElementById('video');27 unmodified lines <button onclick="startCamera()">Start Camera</button> <button id="modeButton" onclick="toggleMode()">Switch to Train Mode</button> <br> <div style="position: relative; display: inline-block;"> <video id="video" autoplay></video> <video id="video" autoplay></video> <canvas id="canvas"></canvas> </div> <br> <button onclick="captureImage()">Capture Image</button> <br>14 unmodified lines predicted_class = np.argmax(prediction) return {"prediction": int(predicted_class)}
@app.post("/train")async def train_model(file: UploadFile = File(...), label: str = Form(...)): global model image_bytes = await file.read() image = preprocess_image(image_bytes)[0] label_index = hash(label) % 100 # Simple label hashing to index if model is None: model = keras.Sequential([ keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(128,128,3)), keras.layers.MaxPooling2D(2,2), keras.layers.Flatten(), keras.layers.Dense(64, activation='relu'), keras.layers.Dense(100, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) labels = np.array([label_index]) images = np.expand_dims(image, axis=0) model.fit(images, labels, epochs=3, batch_size=1) model.save(MODEL_PATH) return {"message": "Gesture trained successfully!"}
if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)app.py+55-24