|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
import os |
|
|
|
|
|
|
|
|
interpreter = None |
|
|
model_loaded = False |
|
|
input_details = None |
|
|
output_details = None |
|
|
MODEL_PATH = '/tmp/sign_language_model_lite.tflite' |
|
|
|
|
|
try: |
|
|
if os.path.exists(MODEL_PATH): |
|
|
|
|
|
interpreter = tf.lite.Interpreter(model_path=MODEL_PATH) |
|
|
interpreter.allocate_tensors() |
|
|
|
|
|
input_details = interpreter.get_input_details() |
|
|
output_details = interpreter.get_output_details() |
|
|
model_loaded = True |
|
|
print("SUCCESS: TFLite Model loaded successfully.") |
|
|
else: |
|
|
print(f"ERROR: Model file not found at {MODEL_PATH}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"FATAL ERROR: Failed to initialize TFLite interpreter: {e}") |
|
|
model_loaded = False |
|
|
|
|
|
|
|
|
SIGN_CLASSES = ["HELLO", "GOOD BYE", "THANKYOU", "PLEASE", "YES", "NO", "SEE YOU", "LOOK", "FOOD", "SORRY", "HELP", "LOVE", "FRIEND", "NAME", "ME"] |
|
|
|
|
|
|
|
|
def classify_sign(input_image_data): |
|
|
"""Processes a single frame from the live webcam feed using the TFLite interpreter.""" |
|
|
|
|
|
if not model_loaded or input_image_data is None: |
|
|
return "Model Loading Error or Camera Feed Not Active..." |
|
|
|
|
|
|
|
|
image_resized = tf.image.resize(input_image_data, (64, 64)) |
|
|
image_normalized = image_resized / 255.0 |
|
|
|
|
|
|
|
|
if image_normalized.shape[-1] == 3: |
|
|
image_normalized = tf.image.rgb_to_grayscale(image_normalized) |
|
|
|
|
|
|
|
|
input_tensor = np.expand_dims(image_normalized, axis=0) |
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], input_tensor.numpy().astype(np.float32)) |
|
|
|
|
|
|
|
|
interpreter.invoke() |
|
|
|
|
|
|
|
|
predictions = interpreter.get_tensor(output_details[0]['index'])[0] |
|
|
|
|
|
|
|
|
predicted_index = np.argmax(predictions) |
|
|
predicted_sign = SIGN_CLASSES[predicted_index] |
|
|
confidence = predictions[predicted_index] * 100 |
|
|
|
|
|
return f"PREDICTED SIGN: {predicted_sign} | Confidence: {confidence:.2f}%" |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
return f"Prediction Runtime Error: {e}" |
|
|
|
|
|
|
|
|
|
|
|
gr.Interface( |
|
|
fn=classify_sign, |
|
|
inputs=gr.Image( |
|
|
sources=['webcam'], |
|
|
type="numpy", |
|
|
shape=(300, 300), |
|
|
label="Live Sign Camera" |
|
|
), |
|
|
outputs=gr.Textbox(label="Real-Time Translation"), |
|
|
live=True, |
|
|
title="Real-Time Sign Language Translator", |
|
|
description="Show your sign in front of the camera, and the prediction will update instantly.", |
|
|
theme="soft", |
|
|
|
|
|
allow_flagging=False |
|
|
).launch(server_name="0.0.0.0", server_port=7860) |
|
|
|