Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.models import load_model | |
| from PIL import Image | |
| # Load the model | |
| model = load_model("model.h5") | |
| # Your class labels (update as per your model) | |
| class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'] | |
| # Prediction function | |
| def predict_expression(image): | |
| try: | |
| image = image.convert("RGB") # Ensure 3 channels | |
| image = image.resize((224, 224)) # Resize to model's expected input | |
| img_array = np.array(image).astype("float32") / 255.0 | |
| img_array = img_array.reshape(1, 224, 224, 3) | |
| prediction = model.predict(img_array) | |
| class_idx = int(np.argmax(prediction)) | |
| confidence = float(np.max(prediction)) | |
| return f"Expression: {class_names[class_idx]} ({confidence:.2%})" | |
| except Exception as e: | |
| return f"⚠️ Error: {str(e)}" | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_expression, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Facial Expression Classifier", | |
| description="Upload a face image and get the predicted emotion" | |
| ) | |
| iface.launch() | |