|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import gradio as gr |
|
|
from keras.preprocessing.image import img_to_array |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_image(img_pil): |
|
|
"""Preprocess the PIL image to fit your model's input requirements.""" |
|
|
|
|
|
img = img_pil.convert('L') |
|
|
|
|
|
img = img.resize((224, 224)) |
|
|
|
|
|
img_array = img_to_array(img) |
|
|
|
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
img_array /= 255.0 |
|
|
return img_array |
|
|
|
|
|
|
|
|
def predict_emotion(image): |
|
|
|
|
|
processed_image = prepare_image(image) |
|
|
|
|
|
prediction = model.predict(processed_image) |
|
|
|
|
|
predicted_class = np.argmax(prediction, axis=1) |
|
|
predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion") |
|
|
return predicted_emotion |
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=predict_emotion, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs="text", |
|
|
title="Emotion Detection", |
|
|
description="Upload an image and see the predicted emotion." |
|
|
) |
|
|
|
|
|
|
|
|
interface.launch() |
|
|
|