Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model = tf.keras.models.load_model("keras_model.h5")
|
| 9 |
+
|
| 10 |
+
# Replace this with your actual label list if you have it
|
| 11 |
+
labels = ["Good Posture", "Bad Posture"]
|
| 12 |
+
|
| 13 |
+
def predict_from_webcam(frame):
|
| 14 |
+
# Resize frame to match model input size (usually 224x224 for Teachable Machine)
|
| 15 |
+
img = cv2.resize(frame, (224, 224))
|
| 16 |
+
img = img / 255.0 # normalize
|
| 17 |
+
img = np.expand_dims(img, axis=0) # batch dimension
|
| 18 |
+
|
| 19 |
+
# Predict
|
| 20 |
+
prediction = model.predict(img)[0]
|
| 21 |
+
top_label = labels[np.argmax(prediction)]
|
| 22 |
+
confidence = np.max(prediction)
|
| 23 |
+
|
| 24 |
+
return f"{top_label} ({confidence*100:.2f}%)"
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=predict_from_webcam,
|
| 29 |
+
inputs=gr.Image(source="webcam", streaming=True),
|
| 30 |
+
outputs=gr.Text(),
|
| 31 |
+
live=True,
|
| 32 |
+
title="Posture Detector",
|
| 33 |
+
description="Detects if your posture is good or bad using a webcam model trained with Teachable Machine."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|