Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,90 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# --- 1. Model Loading and Classes Configuration ---
|
| 7 |
+
interpreter = None
|
| 8 |
+
model_loaded = False
|
| 9 |
+
input_details = None
|
| 10 |
+
output_details = None
|
| 11 |
+
MODEL_PATH = '/tmp/sign_language_model_lite.tflite'
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
if os.path.exists(MODEL_PATH):
|
| 15 |
+
# Load the TFLite model file instead of the heavy H5 file
|
| 16 |
+
interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
|
| 17 |
+
interpreter.allocate_tensors()
|
| 18 |
+
|
| 19 |
+
input_details = interpreter.get_input_details()
|
| 20 |
+
output_details = interpreter.get_output_details()
|
| 21 |
+
model_loaded = True
|
| 22 |
+
print("SUCCESS: TFLite Model loaded successfully.")
|
| 23 |
+
else:
|
| 24 |
+
print(f"ERROR: Model file not found at {MODEL_PATH}")
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"FATAL ERROR: Failed to initialize TFLite interpreter: {e}")
|
| 28 |
+
model_loaded = False
|
| 29 |
+
|
| 30 |
+
# Your Specific Sign Language Classes (Order MUST match your training labels!)
|
| 31 |
+
SIGN_CLASSES = ["HELLO", "GOOD BYE", "THANKYOU", "PLEASE", "YES", "NO", "SEE YOU", "LOOK", "FOOD", "SORRY", "HELP", "LOVE", "FRIEND", "NAME", "ME"]
|
| 32 |
+
|
| 33 |
+
# --- 2. The Real-Time Prediction Function (Updated for TFLite) ---
|
| 34 |
+
def classify_sign(input_image_data):
|
| 35 |
+
"""Processes a single frame from the live webcam feed using the TFLite interpreter."""
|
| 36 |
+
|
| 37 |
+
if not model_loaded or input_image_data is None:
|
| 38 |
+
return "Model Loading Error or Camera Feed Not Active..."
|
| 39 |
+
|
| 40 |
+
# 1. Preprocessing (adjust to model's input: 64x64 grayscale, required for the model)
|
| 41 |
+
image_resized = tf.image.resize(input_image_data, (64, 64))
|
| 42 |
+
image_normalized = image_resized / 255.0
|
| 43 |
+
|
| 44 |
+
# Convert to grayscale if the input is color (shape[3] == 3)
|
| 45 |
+
if image_normalized.shape[-1] == 3:
|
| 46 |
+
image_normalized = tf.image.rgb_to_grayscale(image_normalized)
|
| 47 |
+
|
| 48 |
+
# Add batch dimension (1, 64, 64, 1)
|
| 49 |
+
input_tensor = np.expand_dims(image_normalized, axis=0)
|
| 50 |
+
|
| 51 |
+
# 2. TFLite Prediction Logic
|
| 52 |
+
try:
|
| 53 |
+
# Set the input tensor
|
| 54 |
+
interpreter.set_tensor(input_details[0]['index'], input_tensor.numpy().astype(np.float32))
|
| 55 |
+
|
| 56 |
+
# Invoke the model
|
| 57 |
+
interpreter.invoke()
|
| 58 |
+
|
| 59 |
+
# Get prediction results
|
| 60 |
+
predictions = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 61 |
+
|
| 62 |
+
# 3. Post-processing
|
| 63 |
+
predicted_index = np.argmax(predictions)
|
| 64 |
+
predicted_sign = SIGN_CLASSES[predicted_index]
|
| 65 |
+
confidence = predictions[predicted_index] * 100
|
| 66 |
+
|
| 67 |
+
return f"PREDICTED SIGN: {predicted_sign} | Confidence: {confidence:.2f}%"
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
# This catches runtime errors during invocation
|
| 71 |
+
return f"Prediction Runtime Error: {e}"
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# --- 3. The Gradio Interface for Continuous Streaming ---
|
| 75 |
+
gr.Interface(
|
| 76 |
+
fn=classify_sign,
|
| 77 |
+
inputs=gr.Image(
|
| 78 |
+
sources=['webcam'],
|
| 79 |
+
type="numpy",
|
| 80 |
+
shape=(300, 300),
|
| 81 |
+
label="Live Sign Camera"
|
| 82 |
+
),
|
| 83 |
+
outputs=gr.Textbox(label="Real-Time Translation"),
|
| 84 |
+
live=True,
|
| 85 |
+
title="Real-Time Sign Language Translator",
|
| 86 |
+
description="Show your sign in front of the camera, and the prediction will update instantly.",
|
| 87 |
+
theme="soft",
|
| 88 |
+
# FIX for PermissionError and general stability
|
| 89 |
+
allow_flagging=False
|
| 90 |
+
).launch(server_name="0.0.0.0", server_port=7860)
|