File size: 1,426 Bytes
8bc5b59 0f685dd 8bc5b59 |
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 40 41 42 43 44 45 46 47 48 49 |
import gradio as gr
from fastai.learner import load_learner
from fastai.vision.all import *
import matplotlib.pyplot as plt
# Load the trained model
learn = load_learner('emotion_model.pth')
# Define function to preprocess image
def preprocess_image(image):
# Convert image to PILImage
image_pil = PILImage.create(image)
# Apply same transformations as done during training
image_resized = Resize(224)(image_pil)
return image_resized
# Define function to predict emotion and generate confidence bar chart
def predict_emotion(image):
# Preprocess the image
processed_image = preprocess_image(image)
# Predict the emotion
pred_class, pred_idx, outputs = learn.predict(processed_image)
# Get class names
class_names = learn.dls.vocab
# Generate confidence values
confidences = [outputs[idx].item() for idx in range(len(outputs))]
# Generate bar chart
plt.bar(class_names, confidences)
plt.xlabel('Emotion')
plt.ylabel('Confidence')
plt.title('Confidence of Predicted Emotions')
plt.xticks(rotation=45)
plt.tight_layout()
return plt
# Define Gradio interface
iface = gr.Interface(
fn=predict_emotion,
inputs="image",
outputs="plot",
title="Emotion Prediction",
description="Upload an image and get the confidence of predicted emotions",
interpretation="default",
live=True
)
# Launch the interface
iface.launch()
|