Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| from tensorflow.keras.models import load_model | |
| import numpy as np | |
| from PIL import Image | |
| # Load the pre-trained model (assumed trained on Sign Language MNIST) | |
| model = load_model('sign_language_mnist_cnn.h5') | |
| # Define class names (A-Z, skipping J=9 and Z=25 due to dataset constraints) | |
| CLASS_NAMES = list('ABCDEFGHIKLMNOPQRSTUVWXY') # 24 classes (0-8, 10-24) | |
| # Preprocessing function for Sign Language MNIST | |
| def preprocess_image(image: Image.Image): | |
| # Convert to grayscale | |
| image = image.convert('L') # 'L' mode for grayscale | |
| # Resize to 28x28 (matching dataset) | |
| image = image.resize((28, 28)) | |
| # Convert to numpy array and normalize to 0-255 range (as in dataset) | |
| image_array = np.array(image) | |
| # Normalize to 0-1 range (common for model input) | |
| image_array = image_array / 255.0 | |
| # Add batch and channel dimensions (1, 28, 28, 1) | |
| image_array = np.expand_dims(image_array, axis=(0, -1)) | |
| return image_array | |
| # Prediction function | |
| def predict_sign(image): | |
| processed_image = preprocess_image(image) | |
| # Get model predictions (logits) | |
| predictions = model.predict(processed_image) | |
| probability=np.max(predictions) | |
| target=['A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y'] | |
| arg=np.argmax(predictions) | |
| result={'prediction':target[arg],'probability':probability} | |
| return result | |
| # Gradio interface | |
| interface = gr.Interface( | |
| fn=predict_sign, | |
| inputs=gr.Image(type="pil", label="Upload a Hand Gesture Image"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="Sign Language MNIST Classifier", | |
| description="Upload an image of a hand gesture to classify it as a letter (A-Z, excluding J and Z)." | |
| ) | |
| # Launch the app | |
| interface.launch() |