Spaces:
Sleeping
Sleeping
Update app.py
Browse files30 unmodified lines31323334353637383940419 unmodified lines5152535455565758591 unmodified line6162636465666768697071727310 unmodified lines8485868788899091929394959697989910010110210310410510610710810911011111211330 unmodified linesh2 { 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; }.container { max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); }</style><script>let videoStream;async function startCamera() {videoStream = await navigator.mediaDevices.getUserMedia({ video: true });document.getElementById('video').srcObject = videoStream;9 unmodified lineslet blob = await fetch(dataURL).then(res => res.blob());let formData = new FormData();formData.append("file", blob, "capture.jpg");let response = await fetch("/predict", { method: "POST", body: formData });let result = await response.json();document.getElementById('prediction').innerText = "Prediction: " + result.prediction;}</script></head>1 unmodified line<div class="container"><h2>Live Gesture Recognition</h2><button onclick="startCamera()">Start Camera</button><br><video id="video" autoplay></video><br><button onclick="captureImage()">Capture Image</button><br><img id="capturedImage" /><h3 id="prediction">Prediction: </h3></div></body></html>10 unmodified linesreturn {"prediction": int(predicted_class)}@app.post("/train")async def train_model(files: list[UploadFile] = File(...), labels: list[int] = Form(...)):global modelimages = []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')])model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])model.fit(images_np, labels_np, epochs=5, batch_size=4)model.save(MODEL_PATH)return {"message": "Model trained successfully!"}if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)30 unmodified lines313233343536373839404142439 unmodified lines535455565758596061626364656667686970711 unmodified line737475767778798081828384858610 unmodified lines97989910010110210310410510610710810911011111211311411511611711811912012112212330 unmodified linesh2 { 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 { 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 lineslet 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><video id="video" autoplay></video><br><button onclick="captureImage()">Capture Image</button><br><img id="capturedImage" /><h3 id="prediction-box">Prediction: </h3></div></body></html>10 unmodified linesreturn {"prediction": int(predicted_class)}@app.post("/train")async def train_model(file: UploadFile = File(...), label: str = Form(...)):global modelimage_bytes = await file.read()image = preprocess_image(image_bytes)[0]label_index = hash(label) % 100 # Simple label hashing to indexif 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+35-25