import gradio as gr import tensorflow as tf import numpy as np from PIL import ImageOps # Load your trained model model = tf.keras.models.load_model("hand_sign_model.h5") # Sign Language MNIST class names (A-Z except J and Z) class_names = [chr(i) for i in range(65, 91) if i not in [74, 90]] def preprocess_image(image, target_size=(28, 28)): """ Preprocess input PIL image: - Convert to grayscale - Resize to target_size - Normalize pixel values to [0,1] - Add batch and channel dimensions for model input """ # Convert to grayscale image = ImageOps.grayscale(image) # Resize to target size expected by the model image = image.resize(target_size) # Convert to numpy array and normalize img_array = np.array(image) / 255.0 # Add batch and channel dimensions img_array = np.expand_dims(img_array, axis=(0, -1)) return img_array def predict(image): if image is None: return "Please upload an image." # Preprocess the image to model input shape img_array = preprocess_image(image, target_size=(28, 28)) # Predict probabilities for each class prediction = model.predict(img_array)[0] # Get predicted class label and confidence predicted_label = class_names[np.argmax(prediction)] confidence = np.max(prediction) * 100 return f"Predicted Sign: {predicted_label} ({confidence:.2f}%)" # Build Gradio interface demo = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs="text", title="Hand Sign Recognition", description="Upload any hand sign image of any size, and the model will predict the sign." ) # Launch the app demo.launch()