Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# Load the model from Hugging Face Model Hub
|
| 8 |
+
model_path = hf_hub_download(repo_id="sign_language", filename="saved_model.pb", repo_type="model")
|
| 9 |
+
model = tf.saved_model.load(model_path)
|
| 10 |
+
infer = model.signatures["serving_default"]
|
| 11 |
+
|
| 12 |
+
# Define class labels (modify as per your dataset)
|
| 13 |
+
class_labels = ['Hello', 'Yes', 'No', 'Thank You', 'Please']
|
| 14 |
+
|
| 15 |
+
def predict_sign(frame):
|
| 16 |
+
# Preprocess the frame
|
| 17 |
+
img = cv2.resize(frame, (224, 224)) # Resize to match model input
|
| 18 |
+
img = img / 255.0 # Normalize
|
| 19 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
| 20 |
+
img = tf.convert_to_tensor(img, dtype=tf.float32)
|
| 21 |
+
|
| 22 |
+
# Make prediction
|
| 23 |
+
predictions = infer(tf.constant(img))
|
| 24 |
+
output_tensor_name = list(predictions.keys())[0] # Get the output tensor name
|
| 25 |
+
predictions = predictions[output_tensor_name].numpy()
|
| 26 |
+
predicted_class = class_labels[np.argmax(predictions)]
|
| 27 |
+
confidence = np.max(predictions)
|
| 28 |
+
|
| 29 |
+
return predicted_class, confidence
|
| 30 |
+
|
| 31 |
+
def process_frame(frame):
|
| 32 |
+
pred, conf = predict_sign(frame)
|
| 33 |
+
cv2.putText(frame, f"{pred} ({conf:.2f})", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 34 |
+
return frame
|
| 35 |
+
|
| 36 |
+
# Gradio Live Webcam Interface
|
| 37 |
+
def webcam_feed():
|
| 38 |
+
return gr.Video(source="webcam", streaming=True, mirror=True)
|
| 39 |
+
|
| 40 |
+
gui = gr.Interface(fn=process_frame, inputs=webcam_feed(), outputs="image", live=True)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
gui.launch(server_name="0.0.0.0", server_port=7860)
|