Spaces:
Runtime error
Runtime error
File size: 1,193 Bytes
fbcdb4e daaea1b | 1 2 3 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 |
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
# Load the trained model
model = tf.keras.models.load_model("my_model.h5")
# Class labels (modify according to your dataset)
class_labels = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
# Function to make predictions
def predict_emotion(image):
# Resize image to match model's expected input
image = image.resize((224, 224))
img_array = img_to_array(image) / 255.0 # Normalize
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
# Make prediction
prediction = model.predict(img_array)
predicted_class = class_labels[np.argmax(prediction)]
confidence = np.max(prediction)
return f"Emotion: {predicted_class} (Confidence: {confidence:.2f})"
# Create Gradio interface
interface = gr.Interface(
fn=predict_emotion,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(label="Prediction"),
title="Facial Emotion Detection",
description="Upload an image, and the model will predict the emotion.",
)
# Run the app
if __name__ == "__main__":
interface.launch()
|