Spaces:
Sleeping
Sleeping
Update app.py
Browse files30 unmodified lines31323334343536373839404142439 unmodified lines535455565758596054555661626364656667686970711 unmodified line73747576777879808182708384858610 unmodified lines97989987100101899091929394959697989910010110210310410210310410510610710610710810911011111211311411511611711811911012012112212330 unmodified lines 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; } video, img { width: 50%; max-width: 400px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); margin: 10px; } 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; } </style> <script> let videoStream; let isTraining = false; async function startCamera() { videoStream = await navigator.mediaDevices.getUserMedia({ video: true }); document.getElementById('video').srcObject = videoStream;9 unmodified lines 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').innerText = "Prediction: " + result.prediction; 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> <video id="video" autoplay></video> <br> <button onclick="captureImage()">Capture Image</button> <br> <img id="capturedImage" /> <h3 id="prediction">Prediction: </h3> <h3 id="prediction-box">Prediction: </h3> </div> </body> </html>10 unmodified lines return {"prediction": int(predicted_class)}
@app.post("/train")async def train_model(files: list[UploadFile] = File(...), labels: list[int] = Form(...)):async def train_model(file: UploadFile = File(...), label: str = Form(...)): global model images = [] labels_np = np.array(labels) for file in files: image_bytes = await file.read() images.append(preprocess_image(image_bytes)[0]) images_np = np.array(images) 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(len(set(labels)), activation='softmax') ]) image_bytes = await file.read() image = preprocess_image(image_bytes)[0] label_index = hash(label) % 100 # Simple label hashing to index model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(images_np, labels_np, epochs=5, batch_size=4) 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": "Model trained successfully!"} return {"message": "Gesture trained successfully!"}
if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)app.py+35-25